Решение на Картинки от Димитър Дишев

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

Към профила на Димитър Дишев

Резултати

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

Код

package main
import (
"math"
"strings"
)
type Header struct {
format string
lineWidth int
}
type Colors struct {
Red byte
Green byte
Blue byte
}
type Pixel struct {
Red float64
Green float64
Blue float64
Alpha float64
byteColorData []byte
}
func (p *Pixel) Color() Colors {
clrs := new(Colors)
clrs.Red = p.byteColorData[0]
clrs.Green = p.byteColorData[1]
clrs.Blue = p.byteColorData[2]
return *clrs
}
func (p *Pixel) precomputing() {
p.Red = p.Red * p.Alpha
p.Green = p.Green * p.Alpha
p.Blue = p.Blue * p.Alpha
red := byte(int32(math.Floor(p.Red)))
green := byte(int32(math.Floor(p.Green)))
blue := byte(int32(math.Floor(p.Blue)))
p.byteColorData = []byte{red, green, blue}
}
type Image struct {
data []Pixel
header Header
}
func (i Image) InspectPixel(x int, y int) Pixel {
return i.data[y*i.header.lineWidth+x]
}
func calculateAlpha(a float64) float64 {
var temp float64 = a / 255
return temp
}
func ParseImage(data []byte, header Header) Image {
pixelSize := len(header.format)
pixelsCount := len(data) / pixelSize
pImage := new(Image)
pImage.header.lineWidth = header.lineWidth
for i := 0; i < pixelsCount; i++ {
test := data[:pixelSize]
data = data[pixelSize:]
pixel := new(Pixel)
for j := 0; j < pixelSize; j++ {
switch header.format[j] {
case 'R':
pixel.Red = float64(test[j])
case 'G':
pixel.Green = float64(test[j])
case 'B':
pixel.Blue = float64(test[j])
case 'A':
pixel.Alpha = float64(test[j])
}
}
if strings.Index(header.format, "A") == -1 {
pixel.Alpha = 1.0
} else {
pixel.Alpha = calculateAlpha(pixel.Alpha)
}
pixel.precomputing()
pImage.data = append(pImage.data, *pixel)
}
return *pImage
}

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

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

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

Димитър обнови решението на 12.11.2013 00:52 (преди над 4 години)

+package main
+
+import (
+ "math"
+ "strings"
+)
+
+type Header struct {
+ format string
+ lineWidth int
+}
+
+type Colors struct {
+ Red byte
+ Green byte
+ Blue byte
+}
+type Pixel struct {
+ Red float64
+ Green float64
+ Blue float64
+ Alpha float64
+ byteColorData []byte
+}
+
+func (p *Pixel) Color() Colors {
+
+ clrs := new(Colors)
+ clrs.Red = p.byteColorData[0]
+ clrs.Green = p.byteColorData[1]
+ clrs.Blue = p.byteColorData[2]
+ return *clrs
+}
+
+func (p *Pixel) precomputing() {
+ p.Red = p.Red * p.Alpha
+ p.Green = p.Green * p.Alpha
+ p.Blue = p.Blue * p.Alpha
+
+ red := byte(int32(math.Floor(p.Red)))
+ green := byte(int32(math.Floor(p.Green)))
+ blue := byte(int32(math.Floor(p.Blue)))
+ p.byteColorData = []byte{red, green, blue}
+}
+
+type Image struct {
+ data []Pixel
+ header Header
+}
+
+func (i Image) InspectPixel(x int, y int) Pixel {
+ return i.data[y*i.header.lineWidth+x]
+}
+
+func calculateAlpha(a float64) float64 {
+ var temp float64 = a / 255
+ return temp
+}
+
+func ParseImage(data []byte, header Header) Image {
+ pixelSize := len(header.format)
+ pixelsCount := len(data) / pixelSize
+ pImage := new(Image)
+ pImage.header.lineWidth = header.lineWidth
+ for i := 0; i < pixelsCount; i++ {
+ test := data[:pixelSize]
+ data = data[pixelSize:]
+ pixel := new(Pixel)
+ for j := 0; j < pixelSize; j++ {
+ switch header.format[j] {
+ case 'R':
+ pixel.Red = float64(test[j])
+ case 'G':
+ pixel.Green = float64(test[j])
+ case 'B':
+ pixel.Blue = float64(test[j])
+ case 'A':
+ pixel.Alpha = float64(test[j])
+ }
+ }
+ if strings.Index(header.format, "A") == -1 {
+ pixel.Alpha = 1.0
+ } else {
+ pixel.Alpha = calculateAlpha(pixel.Alpha)
+ }
+ pixel.precomputing()
+ pImage.data = append(pImage.data, *pixel)
+ }
+ return *pImage
+}