Решение на Картинки от Иван Божилов

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

Към профила на Иван Божилов

Резултати

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

Код

package main
import (
"fmt"
"strings"
)
type Pixel struct {
colors [4]float32
}
type Color struct {
Red, Green, Blue, Alpha byte
}
type Image struct {
pixels []Pixel
LineWidth int
}
type Header struct {
format string
LineWidth int
}
func ParseImage(data []byte, h Header) Image {
var picture Image
numberOfColors := len(h.format)
picture.LineWidth = h.LineWidth
i := 0
var p Pixel
var alpha float32
for i < len(data) {
if strings.Index(h.format, "A") != -1 {
p.colors[strings.Index(h.format, "A")] = float32(data[i+3])
alpha = float32(data[i+3]) / 255.0
} else {
alpha = 1
}
p.colors[strings.Index(h.format, "R")] = float32(data[i]) * alpha
p.colors[strings.Index(h.format, "G")] = float32(data[i+1]) * alpha
p.colors[strings.Index(h.format, "B")] = float32(data[i+2]) * alpha
picture.pixels = append(picture.pixels, p)
i = i + numberOfColors
}
return picture
}
func (picture *Image) InspectPixel(x int, y int) Pixel {
return picture.pixels[x+y*picture.LineWidth]
}
func (p *Pixel) Color() Color {
var c Color
c.Alpha = byte(p.colors[3])
c.Blue = byte(p.colors[2])
c.Green = byte(p.colors[1])
c.Red = byte(p.colors[0])
return c
}
func main() {
data := []byte{0, 12, 244, 128, 14, 26, 52, 127, 31, 33, 41, 255, 36, 133, 241, 255}
header := Header{"RGBA", 4}
picture := ParseImage(data, header)
fmt.Println(picture)
}

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

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

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

Иван обнови решението на 11.11.2013 13:26 (преди над 4 години)

+package main
+
+import (
+ "fmt"
+ "strings"
+)
+
+type Pixel struct {
+ colors [4]float32
+}
+
+type Color struct {
+ Red, Green, Blue, Alpha byte
+}
+
+type Image struct {
+ pixels []Pixel
+ LineWidth int
+}
+
+type Header struct {
+ format string
+ LineWidth int
+}
+
+func ParseImage(data []byte, h Header) Image {
+ var picture Image
+ numberOfColors := len(h.format)
+ picture.LineWidth = h.LineWidth
+ i := 0
+ var p Pixel
+ var alpha float32
+ for i < len(data) {
+ if strings.Index(h.format, "A") != -1 {
+ p.colors[strings.Index(h.format, "A")] = float32(data[i+3])
+ alpha = float32(data[i+3]) / 255.0
+ } else {
+ alpha = 1
+ }
+ p.colors[strings.Index(h.format, "R")] = float32(data[i]) * alpha
+ p.colors[strings.Index(h.format, "G")] = float32(data[i+1]) * alpha
+ p.colors[strings.Index(h.format, "B")] = float32(data[i+2]) * alpha
+ picture.pixels = append(picture.pixels, p)
+ i = i + numberOfColors
+ }
+ return picture
+}
+
+func (picture *Image) InspectPixel(x int, y int) Pixel {
+ return picture.pixels[x+y*picture.LineWidth]
+}
+
+func (p *Pixel) Color() Color {
+ var c Color
+ c.Alpha = byte(p.colors[3])
+ c.Blue = byte(p.colors[2])
+ c.Green = byte(p.colors[1])
+ c.Red = byte(p.colors[0])
+ return c
+}
+
+func main() {
+ data := []byte{0, 12, 244, 128, 14, 26, 52, 127, 31, 33, 41, 255, 36, 133, 241, 255}
+ header := Header{"RGBA", 4}
+ picture := ParseImage(data, header)
+ fmt.Println(picture)
+}