Решение на Картинки от Мария Терзиева

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

Към профила на Мария Терзиева

Резултати

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

Код

package main
import "fmt"
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
}
func (p Pixel) String() string {
return fmt.Sprintf("Red: %d, Green: %d, Blue: %d", p.Red, p.Green, p.Blue)
}

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

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

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

Мария обнови решението на 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
+}

Мария обнови решението на 12.11.2013 16:16 (преди над 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 {
+func (p Pixel) Color() Pixel {
return p
}

Мария обнови решението на 12.11.2013 16:38 (преди над 4 години)

package main
+import "fmt"
+
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
+}
+
+func (p Pixel) String() string {
+ return fmt.Sprintf("Red: %d, Green: %d, Blue: %d", p.Red, p.Green, p.Blue)
}