Решение на Картинки от Михаил Стойков

Обратно към всички решения

Към профила на Михаил Стойков

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 10 успешни тест(а)
  • 0 неуспешни тест(а)

Код

package main
import (
"fmt"
)
type Header struct {
Format string
LineWidth int
}
type Image struct {
header Header
data []byte
makePixel func(format string, data []byte) *Pixel
}
type Pixel struct {
color Color
}
type Color struct {
Red byte
Blue byte
Green byte
}
func ParseImage(data []byte, header Header) (image Image) {
image = *(new(Image))
image.header = header
image.data = data
image.makePixel = func(format string, data []byte) *Pixel {
pixel := new(Pixel)
color := new(Color)
var alpha byte = 255
for index, colorCode := range format {
switch colorCode {
case 'R':
color.Red = data[index]
case 'G':
color.Green = data[index]
case 'B':
color.Blue = data[index]
case 'A':
alpha = data[index]
default:
panic("You are an idiot")
}
}
if alpha != 255 {
color.Red = blend(color.Red, alpha)
color.Blue = blend(color.Blue, alpha)
color.Green = blend(color.Green, alpha)
}
pixel.color = *color
return pixel
}
return
}
func blend(color byte, alpha byte) byte {
return byte(int64(color) * int64(alpha) / 255)
}
func (i *Image) InspectPixel(row, column int) (pixel Pixel) {
colorComponentsPerPixel := len(i.header.Format)
begining := colorComponentsPerPixel*row + column*i.header.LineWidth*colorComponentsPerPixel
pixel = *i.makePixel(i.header.Format, i.data[begining:])
return
}
func (p *Pixel) Color() (result Color) {
return p.color
}
func (c Color) String() string {
return fmt.Sprintf("Red: %d, Green: %d, Blue: %d", c.Red, c.Green, c.Blue)
}

Лог от изпълнението

PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.011s
PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.012s
PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.011s
PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.011s
PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.012s
PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.012s
PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.011s
PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.011s
PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.011s
PASS
ok  	_/tmp/d20131125-20161-1m0kh52	0.011s

История (2 версии и 1 коментар)

Михаил обнови решението на 12.11.2013 09:19 (преди над 4 години)

+package main
+
+import (
+ "fmt"
+ "math"
+)
+
+type Header struct {
+ Format string
+ LineWidth int
+}
+
+type Image struct {
+ header Header
+ data []byte
+ makePixel func(format string, data []byte) Pixel
+}
+
+type Pixel struct {
+ color Color
+}
+
+type Color struct {
+ Red byte
+ Blue byte
+ Green byte
+}
+
+func ParseImage(data []byte, header Header) (image Image) {
+ image = *(new(Image))
+ image.header = header
+ image.data = data
+ image.makePixel = func(format string, data []byte) Pixel {
+ pixel := *(new(Pixel))
+ color := new(Color)
+ alpha := 1.0
+ for index, colorCode := range format {
+ switch colorCode {
+ case 'R':
+ color.Red = data[index]
+ case 'G':
+ color.Green = data[index]
+ case 'B':
+ color.Blue = data[index]
+ case 'A':
+ alpha = float64(data[index]) / 255.0
+
+ default:
+ panic("You are an idiot")
+ }
+ }
+ color.Red = byte(round(float64(color.Red) * alpha))
+ color.Blue = byte(round(float64(color.Blue) * alpha))
+ color.Green = byte(round(float64(color.Green) * alpha))
+ pixel.color = *color
+ return pixel
+ }
+ return
+}
+
+func round(float float64) int64 {
+ floor := math.Floor(float)
+ diff := float - floor
+ if diff < 0.5 {
+ return int64(floor)
+ } else {
+ return int64(floor + 1)
+ }
+}
+
+func (i *Image) InspectPixel(row, column int) (pixel Pixel) {
+ colorComponentsPerPixel := len(i.header.Format)
+ begining := colorComponentsPerPixel*row + column*i.header.LineWidth*colorComponentsPerPixel
+ pixel = i.makePixel(i.header.Format, i.data[begining:])
+ return
+}
+
+func (p *Pixel) Color() (result Color) {
+ return p.color
+}
+
+func (c *Color) String() string {
+ return fmt.Sprintf("Red: %d, Green: %d, Blue: %d", c.Red, c.Green, c.Blue)
+}

Михаил обнови решението на 14.11.2013 11:37 (преди над 4 години)

package main
import (
- "fmt"
- "math"
+ "fmt"
)
type Header struct {
- Format string
- LineWidth int
+ Format string
+ LineWidth int
}
type Image struct {
- header Header
- data []byte
- makePixel func(format string, data []byte) Pixel
+ header Header
+ data []byte
+ makePixel func(format string, data []byte) *Pixel
}
type Pixel struct {
- color Color
+ color Color
}
type Color struct {
- Red byte
- Blue byte
- Green byte
+ Red byte
+ Blue byte
+ Green byte
}
func ParseImage(data []byte, header Header) (image Image) {
- image = *(new(Image))
- image.header = header
- image.data = data
- image.makePixel = func(format string, data []byte) Pixel {
- pixel := *(new(Pixel))
- color := new(Color)
- alpha := 1.0
- for index, colorCode := range format {
- switch colorCode {
- case 'R':
- color.Red = data[index]
- case 'G':
- color.Green = data[index]
- case 'B':
- color.Blue = data[index]
- case 'A':
- alpha = float64(data[index]) / 255.0
+ image = *(new(Image))
+ image.header = header
+ image.data = data
+ image.makePixel = func(format string, data []byte) *Pixel {
+ pixel := new(Pixel)
+ color := new(Color)
+ var alpha byte = 255
+ for index, colorCode := range format {
+ switch colorCode {
+ case 'R':
+ color.Red = data[index]
+ case 'G':
+ color.Green = data[index]
+ case 'B':
+ color.Blue = data[index]
+ case 'A':
+ alpha = data[index]
+ default:
+ panic("You are an idiot")
+ }
+ }
+ if alpha != 255 {
+ color.Red = blend(color.Red, alpha)
+ color.Blue = blend(color.Blue, alpha)
+ color.Green = blend(color.Green, alpha)
+ }
- default:
- panic("You are an idiot")
- }
- }
- color.Red = byte(round(float64(color.Red) * alpha))
- color.Blue = byte(round(float64(color.Blue) * alpha))
- color.Green = byte(round(float64(color.Green) * alpha))
- pixel.color = *color
- return pixel
- }
- return
+ pixel.color = *color
+ return pixel
+ }
+ return
}
-func round(float float64) int64 {
- floor := math.Floor(float)
- diff := float - floor
- if diff < 0.5 {
- return int64(floor)
- } else {
- return int64(floor + 1)
- }
+func blend(color byte, alpha byte) byte {
+ return byte(int64(color) * int64(alpha) / 255)
}
func (i *Image) InspectPixel(row, column int) (pixel Pixel) {
- colorComponentsPerPixel := len(i.header.Format)
- begining := colorComponentsPerPixel*row + column*i.header.LineWidth*colorComponentsPerPixel
- pixel = i.makePixel(i.header.Format, i.data[begining:])
- return
+ colorComponentsPerPixel := len(i.header.Format)
+ begining := colorComponentsPerPixel*row + column*i.header.LineWidth*colorComponentsPerPixel
+ pixel = *i.makePixel(i.header.Format, i.data[begining:])
+ return
}
func (p *Pixel) Color() (result Color) {
- return p.color
+ return p.color
}
-func (c *Color) String() string {
- return fmt.Sprintf("Red: %d, Green: %d, Blue: %d", c.Red, c.Green, c.Blue)
+func (c Color) String() string {
+ return fmt.Sprintf("Red: %d, Green: %d, Blue: %d", c.Red, c.Green, c.Blue)
}