Мария обнови решението на 12.11.2013 00:48 (преди над 4 години)
+package main
+
+type Header struct {
+ Format string
+ LineWidth int
+}
+
+type Image struct {
+ Data []byte
+ Information Header
+}
+
+type Pixel struct {
+ Red byte
+ Green byte
+ Blue byte
+}
+
+func ParseImage(data []byte, header Header) Image {
+ if len(header.Format) == 4 {
+ length := len(data)
+ var floatData = make([]float32, length)
+ for index := range data {
+ floatData[index] = float32(data[index]) / 255
+ }
+ var parsedData = make([]byte, length)
+ for i := 3; i < length; i += 4 {
+ for j := 1; j <= 3; j++ {
+ parsedData[i-j] = byte(255 * floatData[i] * floatData[i-j])
+ }
+ parsedData[i] = byte(255 * floatData[i])
+ }
+ return Image{parsedData, header}
+ } else {
+ return Image{data, header}
+ }
+}
+
+func (i Image) InspectPixel(column int, row int) Pixel {
+ var pixel Pixel
+ switch {
+ case len(i.Information.Format) == 4:
+ index := 4 * (i.Information.LineWidth*row + column)
+ if i.Information.Format == "RGBA" {
+ pixel = Pixel{i.Data[index], i.Data[index+1], i.Data[index+2]}
+ } else {
+ pixel = Pixel{i.Data[index+2], i.Data[index+1], i.Data[index]}
+ }
+ default:
+ index := 3 * (i.Information.LineWidth*row + column)
+ if i.Information.Format == "RGB" {
+ pixel = Pixel{i.Data[index], i.Data[index+1], i.Data[index+2]}
+ } else {
+ pixel = Pixel{i.Data[index+2], i.Data[index+1], i.Data[index]}
+ }
+ }
+ return pixel
+}
+
+func (p *Pixel) Color() *Pixel {
+ return p
+}