Решение на Картинки от Йордан Пулов

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

Към профила на Йордан Пулов

Резултати

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

Код

package main
/*import (
"fmt"
)*/
type Header struct {
Format string
LineWidth int
}
type Image struct {
data []byte
header Header
}
type Pixel struct {
data []byte
header Header
}
type colors struct {
Red byte
Green byte
Blue byte
}
func ParseImage(data []byte, header Header) Image {
img := Image {data , header }
return img
}
func (i Image) InspectPixel(col int , row int) Pixel {
// fmt.Println(i.data[row * col : row * col + i.header.num])
start := row * i.header.LineWidth * len(i.header.Format) + col * len(i.header.Format)
end :=0
if start + i.header.LineWidth + 2 > len(i.data) {
end = len(i.data)
} else {
end = start + i.header.LineWidth +2
}
px := Pixel { i.data[start : end ] , i.header }
return px
}
func (i Pixel) Color() colors {
//fmt.Println((i.data))
switch i.header.Format {
case "RGB" :
return colors{ i.data[0], i.data[1], i.data[2] }
case "BGR" :
return colors{ i.data[2], i.data[1], i.data[0] }
case "GRB" :
return colors{ i.data[1], i.data[0], i.data[2] }
case "RGBA" :
return colors{ byte(int(i.data[0]) * int(i.data[3]) / 255), byte(int(i.data[1]) * int(i.data[3]) / 255), byte(int(i.data[2]) * int(i.data[3]) / 255) }
case "BGRA" :
return colors{ byte(int(i.data[2]) * int(i.data[3]) / 255), byte(int(i.data[1]) * int(i.data[3]) / 255), byte(int(i.data[0]) * int(i.data[3]) / 255) }
}
color := colors{ i.data[2], i.data[1], i.data[0] }
return color
}

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

PASS
ok  	_/tmp/d20131125-20161-j42h11	0.011s
PASS
ok  	_/tmp/d20131125-20161-j42h11	0.011s
PASS
ok  	_/tmp/d20131125-20161-j42h11	0.012s
--- FAIL: TestSinglePixelRGBACall (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-j42h11.Pixel.Color(0xb7b29ec0, 0x3, 0x4, 0x811a4f8, 0x4, ...)
	/tmp/d20131125-20161-j42h11/solution.go:55 +0x534
_/tmp/d20131125-20161-j42h11.assertColor(0xb7b29ec0, 0x3, 0x4, 0x811a4f8, 0x4, ...)
	/tmp/d20131125-20161-j42h11/solution_test.go:9 +0x31
_/tmp/d20131125-20161-j42h11.TestSinglePixelRGBACall(0x18356120)
	/tmp/d20131125-20161-j42h11/solution_test.go:72 +0x1ac
testing.tRunner(0x18356120, 0x81b0104)
	/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(0x813c408, 0x81b00e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813c408, 0x81b00e0, 0xa, 0xa, 0x81b3540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131125-20161-j42h11/_test/_testmain.go:61 +0x81
exit status 2
FAIL	_/tmp/d20131125-20161-j42h11	0.014s
PASS
ok  	_/tmp/d20131125-20161-j42h11	0.011s
PASS
ok  	_/tmp/d20131125-20161-j42h11	0.011s
--- FAIL: TestSinglePixelBGRACall (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-j42h11.Pixel.Color(0xb7b67ec0, 0x3, 0x4, 0x81199f8, 0x4, ...)
	/tmp/d20131125-20161-j42h11/solution.go:57 +0x23e
_/tmp/d20131125-20161-j42h11.assertColor(0xb7b67ec0, 0x3, 0x4, 0x81199f8, 0x4, ...)
	/tmp/d20131125-20161-j42h11/solution_test.go:9 +0x31
_/tmp/d20131125-20161-j42h11.TestSinglePixelBGRACall(0x18356120)
	/tmp/d20131125-20161-j42h11/solution_test.go:117 +0x1ac
testing.tRunner(0x18356120, 0x81b0128)
	/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(0x813c408, 0x81b00e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813c408, 0x81b00e0, 0xa, 0xa, 0x81b3540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131125-20161-j42h11/_test/_testmain.go:61 +0x81
exit status 2
FAIL	_/tmp/d20131125-20161-j42h11	0.013s
PASS
ok  	_/tmp/d20131125-20161-j42h11	0.011s
PASS
ok  	_/tmp/d20131125-20161-j42h11	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-j42h11.Pixel.Color(0xb7b5ce51, 0x3, 0x10, 0x811a4f8, 0x4, ...)
	/tmp/d20131125-20161-j42h11/solution.go:55 +0x534
_/tmp/d20131125-20161-j42h11.assertColor(0xb7b5ce51, 0x3, 0x10, 0x811a4f8, 0x4, ...)
	/tmp/d20131125-20161-j42h11/solution_test.go:9 +0x31
_/tmp/d20131125-20161-j42h11.TestMultirowRGBACall(0x183561e0)
	/tmp/d20131125-20161-j42h11/solution_test.go:165 +0x1be
testing.tRunner(0x183561e0, 0x81b014c)
	/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(0x813c408, 0x81b00e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813c408, 0x81b00e0, 0xa, 0xa, 0x81b3540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20131125-20161-j42h11/_test/_testmain.go:61 +0x81
exit status 2
FAIL	_/tmp/d20131125-20161-j42h11	0.012s

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

Йордан обнови решението на 12.11.2013 01:58 (преди над 4 години)

+package main
+
+
+/*import (
+ "fmt"
+
+
+)*/
+type Header struct {
+ Format string
+ LineWidth int
+ }
+type Image struct {
+ data []byte
+ header Header
+ }
+type Pixel struct {
+ data []byte
+ header Header
+ }
+type colors struct {
+ Red byte
+ Green byte
+ Blue byte
+ }
+func ParseImage(data []byte, header Header) Image {
+ img := Image {data , header }
+ return img
+}
+func (i Image) InspectPixel(col int , row int) Pixel {
+ // fmt.Println(i.data[row * col : row * col + i.header.num])
+ start := row * i.header.LineWidth * len(i.header.Format) + col * len(i.header.Format)
+
+ end :=0
+ if start + i.header.LineWidth + 2 > len(i.data) {
+ end = len(i.data)
+ } else {
+ end = start + i.header.LineWidth +2
+ }
+
+ px := Pixel { i.data[start : end ] , i.header }
+ return px
+}
+func (i Pixel) Color() colors {
+
+ //fmt.Println((i.data))
+ switch i.header.Format {
+ case "RGB" :
+ return colors{ i.data[0], i.data[1], i.data[2] }
+ case "BGR" :
+ return colors{ i.data[2], i.data[1], i.data[0] }
+ case "GRB" :
+ return colors{ i.data[1], i.data[0], i.data[2] }
+ case "RGBA" :
+ return colors{ byte(int(i.data[0]) * int(i.data[3]) / 255), byte(int(i.data[1]) * int(i.data[3]) / 255), byte(int(i.data[2]) * int(i.data[3]) / 255) }
+ case "BGRA" :
+ return colors{ byte(int(i.data[0]) * int(i.data[3]) / 255), byte(int(i.data[1]) * int(i.data[3]) / 255), byte(int(i.data[2]) * int(i.data[3]) / 255) }
+
+ }
+ color := colors{ i.data[2], i.data[1], i.data[0] }
+ return color
+}
+
+
+
+func main() {
+ /*
+ data := []byte{
+ 0, 12, 244, 127, 14, 26, 52, 127,
+ 31, 33, 41, 255, 36, 133, 241, 255,
+ }
+ header := Header{"BGRA", 2}
+
+ fmt.Println(ParseImage(data,header).InspectPixel(1,0).Color())*/
+
+
+
+}

Йордан обнови решението на 12.11.2013 02:03 (преди над 4 години)

package main
/*import (
"fmt"
)*/
type Header struct {
Format string
LineWidth int
}
type Image struct {
data []byte
header Header
}
type Pixel struct {
data []byte
header Header
}
type colors struct {
Red byte
Green byte
Blue byte
}
func ParseImage(data []byte, header Header) Image {
img := Image {data , header }
return img
}
func (i Image) InspectPixel(col int , row int) Pixel {
// fmt.Println(i.data[row * col : row * col + i.header.num])
start := row * i.header.LineWidth * len(i.header.Format) + col * len(i.header.Format)
end :=0
if start + i.header.LineWidth + 2 > len(i.data) {
end = len(i.data)
} else {
end = start + i.header.LineWidth +2
}
px := Pixel { i.data[start : end ] , i.header }
return px
}
func (i Pixel) Color() colors {
//fmt.Println((i.data))
switch i.header.Format {
case "RGB" :
return colors{ i.data[0], i.data[1], i.data[2] }
case "BGR" :
return colors{ i.data[2], i.data[1], i.data[0] }
case "GRB" :
return colors{ i.data[1], i.data[0], i.data[2] }
case "RGBA" :
return colors{ byte(int(i.data[0]) * int(i.data[3]) / 255), byte(int(i.data[1]) * int(i.data[3]) / 255), byte(int(i.data[2]) * int(i.data[3]) / 255) }
case "BGRA" :
- return colors{ byte(int(i.data[0]) * int(i.data[3]) / 255), byte(int(i.data[1]) * int(i.data[3]) / 255), byte(int(i.data[2]) * int(i.data[3]) / 255) }
+ return colors{ byte(int(i.data[2]) * int(i.data[3]) / 255), byte(int(i.data[1]) * int(i.data[3]) / 255), byte(int(i.data[0]) * int(i.data[3]) / 255) }
}
color := colors{ i.data[2], i.data[1], i.data[0] }
return color
}
func main() {
/*
data := []byte{
0, 12, 244, 127, 14, 26, 52, 127,
31, 33, 41, 255, 36, 133, 241, 255,
}
header := Header{"BGRA", 2}
fmt.Println(ParseImage(data,header).InspectPixel(1,0).Color())*/
}

Йордан обнови решението на 12.11.2013 09:49 (преди над 4 години)

package main
/*import (
"fmt"
)*/
type Header struct {
Format string
LineWidth int
}
type Image struct {
data []byte
header Header
}
type Pixel struct {
data []byte
header Header
}
type colors struct {
Red byte
Green byte
Blue byte
}
func ParseImage(data []byte, header Header) Image {
img := Image {data , header }
return img
}
func (i Image) InspectPixel(col int , row int) Pixel {
// fmt.Println(i.data[row * col : row * col + i.header.num])
start := row * i.header.LineWidth * len(i.header.Format) + col * len(i.header.Format)
end :=0
if start + i.header.LineWidth + 2 > len(i.data) {
end = len(i.data)
} else {
end = start + i.header.LineWidth +2
}
px := Pixel { i.data[start : end ] , i.header }
return px
}
func (i Pixel) Color() colors {
//fmt.Println((i.data))
switch i.header.Format {
case "RGB" :
return colors{ i.data[0], i.data[1], i.data[2] }
case "BGR" :
return colors{ i.data[2], i.data[1], i.data[0] }
case "GRB" :
return colors{ i.data[1], i.data[0], i.data[2] }
case "RGBA" :
return colors{ byte(int(i.data[0]) * int(i.data[3]) / 255), byte(int(i.data[1]) * int(i.data[3]) / 255), byte(int(i.data[2]) * int(i.data[3]) / 255) }
case "BGRA" :
return colors{ byte(int(i.data[2]) * int(i.data[3]) / 255), byte(int(i.data[1]) * int(i.data[3]) / 255), byte(int(i.data[0]) * int(i.data[3]) / 255) }
}
color := colors{ i.data[2], i.data[1], i.data[0] }
return color
}
-
-
-func main() {
- /*
- data := []byte{
- 0, 12, 244, 127, 14, 26, 52, 127,
- 31, 33, 41, 255, 36, 133, 241, 255,
- }
- header := Header{"BGRA", 2}
-
- fmt.Println(ParseImage(data,header).InspectPixel(1,0).Color())*/
-
-
-
-}