Решение на Картинки от Недялко Дяков

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

Към профила на Недялко Дяков

Резултати

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

Код

package main
type Header struct {
Format string
LineWidth int
}
type Image struct {
Data []byte
Header Header
}
// Load whole image data.
// Return new Image object.
func ParseImage(data []byte, header Header) (img Image) {
img.Data = data
img.Header = header
return
}
// Get the pixel data out of the image data.
// Return new Pixel object.
func (i *Image) InspectPixel(positionX, positionY int) (pixel Pixel) {
pixel.Img = i
pixel.Column = positionX
pixel.Row = positionY
start := pixel.Row * i.Header.LineWidth * len(i.Header.Format)
start += pixel.Column * len(i.Header.Format)
end := start + len(i.Header.Format)
pixel.Data = i.Data[start:end]
pixel.PixelColor = new(Color)
return
}
type Pixel struct {
Column int
Row int
Data []byte
Img *Image
PixelColor *Color
}
// Parse the pixel color levels.
// Return new Color object.
func (p *Pixel) Color() Color {
p.PixelColor.Alpha = 255
for index, char := range p.Img.Header.Format {
switch char {
case 'R':
p.PixelColor.Red = p.Data[index]
case 'B':
p.PixelColor.Blue = p.Data[index]
case 'G':
p.PixelColor.Green = p.Data[index]
case 'A':
p.PixelColor.Alpha = p.Data[index]
}
}
_ = p.PixelColor.AlphaBlend()
return *(p.PixelColor)
}
type Color struct {
Red byte
Green byte
Blue byte
Alpha byte
}
// Compute new Color levels based on the Alpha level.
// Returns ImageError on error otherwise nil.
func (c *Color) AlphaBlend() *ImageError {
if c.Alpha == 255 {
return nil
}
alpha := float64(float64(c.Alpha) / 255.0)
if alpha < 0.0 || alpha > 1.0 {
err := new(ImageError)
err.ErrorMessage = "Wrong alpha level!"
return err
}
c.Red = byte(int(float64(c.Red) * alpha))
c.Green = byte(int(float64(c.Green) * alpha))
c.Blue = byte(int(float64(c.Blue) * alpha))
c.Alpha = 255
return nil
}
// This is just for testing, don't mind it.
type ImageError struct {
ErrorMessage string
}
func (ie *ImageError) Error() string {
return ie.ErrorMessage
}

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

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

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

Недялко обнови решението на 11.11.2013 23:49 (преди над 4 години)

+package main
+
+type Header struct {
+ Format string
+ LineWidth int
+}
+
+type Image struct {
+ Data []byte
+ Header Header
+}
+
+// Load whole image data.
+// Return new Image object.
+func ParseImage(data []byte, header Header) (img Image) {
+ img.Data = data
+ img.Header = header
+ return
+}
+
+// Get the pixel data out of the image data.
+// Return new Pixel object.
+func (i *Image) InspectPixel(positionX, positionY int) (pixel Pixel) {
+ pixel.Img = i
+ pixel.Column = positionX
+ pixel.Row = positionY
+ start := pixel.Row * i.Header.LineWidth * len(i.Header.Format)
+ start += pixel.Column * len(i.Header.Format)
+ end := start + len(i.Header.Format)
+ pixel.Data = i.Data[start:end]
+ pixel.PixelColor = new(Color)
+ return
+}
+
+type Pixel struct {
+ Column int
+ Row int
+ Data []byte
+ Img *Image
+ PixelColor *Color
+}
+
+// Parse the pixel color levels.
+// Return new Color object.
+func (p *Pixel) Color() Color {
+ p.PixelColor.Alpha = 255
+ for index, char := range p.Img.Header.Format {
+ switch char {
+ case 'R':
+ p.PixelColor.Red = p.Data[index]
+ case 'B':
+ p.PixelColor.Blue = p.Data[index]
+ case 'G':
+ p.PixelColor.Green = p.Data[index]
+ case 'A':
+ p.PixelColor.Alpha = p.Data[index]
+ }
+ }
+ _ = p.PixelColor.AlphaBlend()
+ return *(p.PixelColor)
+}
+
+type Color struct {
+ Red byte
+ Green byte
+ Blue byte
+ Alpha byte
+}
+
+// Compute new Color levels based on the Alpha level.
+// Returns ImageError on error otherwise nil.
+func (c *Color) AlphaBlend() *ImageError {
+ if c.Alpha == 255 {
+ return nil
+ }
+ alpha := float64(float64(c.Alpha) / 255.0)
+ if alpha < 0.0 || alpha > 1.0 {
+ err := new(ImageError)
+ err.ErrorMessage = "Wrong alpha level!"
+ return err
+ }
+ c.Red = byte(int(float64(c.Red) * alpha))
+ c.Green = byte(int(float64(c.Green) * alpha))
+ c.Blue = byte(int(float64(c.Blue) * alpha))
+ c.Alpha = 255
+ return nil
+}
+
+// This is just for testing, don't mind it.
+type ImageError struct {
+ ErrorMessage string
+}
+
+func (ie *ImageError) Error() string {
+ return ie.ErrorMessage
+}