Решение на Картинки от Ива Тотева

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

Към профила на Ива Тотева

Резултати

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

Код

package main
import (
"strconv"
)
type Header struct {
Format string
LineWidth int
}
type Color struct {
Red byte
Green byte
Blue byte
}
type Image struct {
pixels [][]Pixel
}
func (image *Image) InspectPixel(x int, y int) Pixel {
return image.pixels[x][y]
}
type Pixel struct {
ColorOfPixel Color
}
func (pixel *Pixel) Color() Color {
return pixel.ColorOfPixel
}
func (color Color) String() string {
return "Red: " + strconv.Itoa(int(color.Red)) + ", Green: " + strconv.Itoa(int(color.Green)) + ", Blue: " + strconv.Itoa(int(color.Blue))
}
func ParseImage(data []byte, header Header) *Image {
image := new(Image)
bandWidth := len(header.Format)
size := len(data) / header.LineWidth / bandWidth
r, g, b, a := 0, 0, 0, 0
for i, c := range header.Format {
switch c {
case 'R':
r = i
case 'G':
g = i
case 'B':
b = i
case 'A':
a = i
}
}
image.pixels = make([][]Pixel, size)
for i := 0; i < size; i++ {
image.pixels[i] = make([]Pixel, header.LineWidth)
for j := 0; j < header.LineWidth; j++ {
image.pixels[i][j] = *new(Pixel)
currentIndex := (i*header.LineWidth + j) * bandWidth
alpha := float32(1)
if a != 0 {
alpha = float32(data[currentIndex+a]) / 255
}
image.pixels[i][j].ColorOfPixel.Red = byte(float32(data[currentIndex+r]) * alpha)
image.pixels[i][j].ColorOfPixel.Green = byte(float32(data[currentIndex+g]) * alpha)
image.pixels[i][j].ColorOfPixel.Blue = byte(float32(data[currentIndex+b]) * alpha)
}
}
return image
}

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

PASS
ok  	_/tmp/d20131125-20161-1j4p1yx	0.012s
--- FAIL: TestBasicRGBCall (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20131125-20161-1j4p1yx.TestBasicRGBCall(0x18255120)
	/tmp/d20131125-20161-1j4p1yx/solution_test.go:43 +0x1c6
testing.tRunner(0x18255120, 0x81af0ec)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x81b2540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131125-20161-1j4p1yx/_test/_testmain.go:61 +0x81
exit status 2
FAIL	_/tmp/d20131125-20161-1j4p1yx	0.012s
--- FAIL: TestBasicRGBRowsCall (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20131125-20161-1j4p1yx.TestBasicRGBRowsCall(0x182561e0)
	/tmp/d20131125-20161-1j4p1yx/solution_test.go:55 +0x356
testing.tRunner(0x182561e0, 0x81af0f8)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x81b2540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131125-20161-1j4p1yx/_test/_testmain.go:61 +0x81
exit status 2
FAIL	_/tmp/d20131125-20161-1j4p1yx	0.012s
PASS
ok  	_/tmp/d20131125-20161-1j4p1yx	0.011s
--- FAIL: TestBasicRGBACall (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20131125-20161-1j4p1yx.TestBasicRGBACall(0x18256120)
	/tmp/d20131125-20161-1j4p1yx/solution_test.go:89 +0x375
testing.tRunner(0x18256120, 0x81af110)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x81b2540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131125-20161-1j4p1yx/_test/_testmain.go:61 +0x81
exit status 2
FAIL	_/tmp/d20131125-20161-1j4p1yx	0.012s
PASS
ok  	_/tmp/d20131125-20161-1j4p1yx	0.011s
PASS
ok  	_/tmp/d20131125-20161-1j4p1yx	0.011s
--- FAIL: TestBasicBGRACall (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20131125-20161-1j4p1yx.TestBasicBGRACall(0x18256120)
	/tmp/d20131125-20161-1j4p1yx/solution_test.go:134 +0x375
testing.tRunner(0x18256120, 0x81af134)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x81b2540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131125-20161-1j4p1yx/_test/_testmain.go:61 +0x81
exit status 2
FAIL	_/tmp/d20131125-20161-1j4p1yx	0.012s
PASS
ok  	_/tmp/d20131125-20161-1j4p1yx	0.011s
--- FAIL: TestMultirowRGBACall (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20131125-20161-1j4p1yx.TestMultirowRGBACall(0x182561e0)
	/tmp/d20131125-20161-1j4p1yx/solution_test.go:169 +0x36e
testing.tRunner(0x182561e0, 0x81af14c)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813bcd8, 0x81af0e0, 0xa, 0xa, 0x81b2540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131125-20161-1j4p1yx/_test/_testmain.go:61 +0x81
exit status 2
FAIL	_/tmp/d20131125-20161-1j4p1yx	0.013s

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

Ива обнови решението на 06.11.2013 00:44 (преди над 4 години)

+package main
+
+import (
+ "strconv"
+)
+
+type Header struct {
+ Format string
+ LineWidth int
+}
+
+type Color struct {
+ Red byte
+ Green byte
+ Blue byte
+}
+
+type Image struct {
+ pixels [][]Pixel
+}
+
+func (image *Image) InspectPixel(x int, y int) Pixel {
+ return image.pixels[x][y]
+}
+
+type Pixel struct {
+ ColorOfPixel Color
+}
+
+func (pixel *Pixel) Color() Color {
+ return pixel.ColorOfPixel
+}
+
+func (color Color) String() string {
+ return "Red: " + strconv.Itoa(int(color.Red)) + ", Green: " + strconv.Itoa(int(color.Green)) + ", Blue: " + strconv.Itoa(int(color.Blue))
+}
+
+func ParseImage(data []byte, header Header) *Image {
+ image := new(Image)
+ bandWidth := len(header.Format)
+ size := len(data) / header.LineWidth / bandWidth
+ r, g, b, a := 0, 0, 0, 0
+ for i, c := range header.Format {
+ switch c {
+ case 'R':
+ r = i
+ case 'G':
+ g = i
+ case 'B':
+ b = i
+ case 'A':
+ a = i
+ }
+ }
+
+ image.pixels = make([][]Pixel, size)
+ for i := 0; i < size; i++ {
+ image.pixels[i] = make([]Pixel, header.LineWidth)
+ for j := 0; j < header.LineWidth; j++ {
+ image.pixels[i][j] = *new(Pixel)
+ currentIndex := (i*header.LineWidth + j) * bandWidth
+ alpha := float32(1)
+ if a != 0 {
+ alpha = float32(data[currentIndex+a]) / 255
+ }
+ image.pixels[i][j].ColorOfPixel.Red = byte(float32(data[currentIndex+r]) * alpha)
+ image.pixels[i][j].ColorOfPixel.Green = byte(float32(data[currentIndex+g]) * alpha)
+ image.pixels[i][j].ColorOfPixel.Blue = byte(float32(data[currentIndex+b]) * alpha)
+ }
+ }
+ return image
+}