Димитър обнови решението на 14.11.2013 16:41 (преди над 4 години)
+package main
+
+import (
+ "fmt";
+ "strings"
+)
+
+type Pixel struct {
+ red byte
+ green byte
+ blue byte
+}
+
+func NewPixel(red, green, blue byte) *Pixel {
+ p := new(Pixel)
+ p.red = red
+ p.green = green
+ p.blue = blue
+ return p
+}
+
+type Header struct {
+ Format string
+ LineWidth int
+}
+
+type ParsedImage struct {
+ parsedData [10][10][4]byte
+}
+
+func NewParsedImage(data[10][10][4]byte) *ParsedImage {
+ pd := new(ParsedImage)
+ pd.parsedData = data
+ return pd
+}
+
+func (parI *ParsedImage) InspectPixel(x int, y int) Pixel {
+ var pixl = NewPixel(parI.parsedData[x][y][0], parI.parsedData[x][y][1], parI.parsedData[x][y][2])
+ return *pixl
+}
+
+func ParseImage(data []byte, header Header) ParsedImage {
+
+ var dataToReturn [10][10][4]byte
+ var counter byte
+ counter = 0
+ for i := 0; i < ((len(data)/3)/header.LineWidth); i++ {
+ fmt.Println("FIRST")
+ for j := 0; j < header.LineWidth; j++ {
+ fmt.Println("SECOND")
+ for k := 0; k < len(header.Format); k++ {
+ fmt.Println("THIRD")
+ dataToReturn[i][j][k] = data[counter]
+ counter++
+ }
+
+ if header.Format == "RGBA" {
+ for z := 0; z < 3; z++ {
+ var result float32
+ result = float32(float32(dataToReturn[i][j][z]) * (float32(dataToReturn[i][j][3]) / float32(100)))
+ dataToReturn[i][j][z] = byte(result)
+ }
+ } else {
+ indexOfR := strings.Index(header.Format, "R")
+ indexOfG := strings.Index(header.Format, "G")
+ indexOfB := strings.Index(header.Format, "B")
+
+ if len(header.Format) == 3 {
+ var dataProcessed [3]byte
+ dataProcessed[0] = dataToReturn[i][j][indexOfR]
+ dataProcessed[1] = dataToReturn[i][j][indexOfG]
+ dataProcessed[2] = dataToReturn[i][j][indexOfB]
+ for q := 0; q < 3; q++ {
+ dataToReturn[i][j][q] = dataProcessed[q]
+ }
+ } else {
+ indexOfAlpha := strings.Index(header.Format, "A")
+ var dataProcessed [4]byte
+ dataProcessed[0] = byte(float32(float32(dataToReturn[i][j][indexOfR])*(float32(dataToReturn[i][j][indexOfAlpha])/float32(100))))
+ dataProcessed[1] = byte(float32(float32(dataToReturn[i][j][indexOfG])*(float32(dataToReturn[i][j][indexOfAlpha])/float32(100))))
+ dataProcessed[2] = byte(float32(float32(dataToReturn[i][j][indexOfB])*(float32(dataToReturn[i][j][indexOfAlpha])/float32(100))))
+ for w := 0; w < 3; w++ {
+ dataToReturn[i][j][w] = dataProcessed[w]
+ }
+ }
+ }
+ }
+ }
+ return *NewParsedImage(dataToReturn)
+}
+func main() {
+
+ data := []byte{0, 12, 244, 75, 13, 26, 52, 50, 31, 33, 41, 85}
+ header := Header{"GBAR", 3}
+ var PI ParsedImage = ParseImage(data, header)
+ fmt.Println(PI)
+ pix := PI.InspectPixel(0,0)
+ fmt.Println(pix)
+}