func GenerateFractal(im *image.Gray) { z := &cn{} c := &cn{} g := color.Gray{} for x := 0; x < 1000; x++ { for y := 0; y < 1000; y++ { c.r = (*width * float64(x) / float64(1000)) - *width/float64(2) c.i = (*height * float64(y) / float64(1000)) - *height/float64(2) c.r += *centerx c.i += *centery z.r = 0 z.i = 0 zrsqr := z.r * z.r zisqr := z.i * z.i var i uint8 = 0 for zrsqr+zisqr < 4.0 && i < 255 { i++ z.i = square(z.r+z.i) - zrsqr - zisqr z.i += c.i z.r = zrsqr - zisqr + c.r zrsqr = square(z.r) zisqr = square(z.i) } g.Y = i im.SetGray(x, y, g) } } }
func drawRect(img *image.Gray, c color.Gray, xPos, yPos, width, height float64) { for h := 0.0; h < height; h += 1.0 { for w := 0.0; w < width; w += 1.0 { img.SetGray(int(xPos+w), int(yPos+h), c) } } }
// Transforms gray input image color depth to the palette defined in p // The algorithm simply assigns the nearest palette color to each pixel, // without distributing error in any way, so expect color banding func ditheringAverage(img *image.Gray, p *color.Palette) { size := img.Bounds() for y := size.Min.Y; y < size.Max.Y; y++ { for x := size.Min.X; x < size.Max.X; x++ { c1 := img.GrayAt(x, y) c2 := p.Convert(c1).(color.Gray) img.SetGray(x, y, c2) } } }
func drawRect(img *image.Gray, r image.Rectangle, c color.Gray) { // Draw top and bottom lines for x := r.Min.X - 1; x <= r.Max.X; x++ { img.SetGray(x, r.Min.Y-1, c) img.SetGray(x, r.Max.Y+1, c) } // Draw side lines for y := r.Min.Y - 1; y <= r.Max.Y; y++ { img.SetGray(r.Min.X-1, y, c) img.SetGray(r.Max.X+1, y, c) } }
func draw_line(start_x, start_y, end_x, end_y int, col color.Gray, img *image.Gray) { // Bresenham's var cx int = start_x var cy int = start_y var dx int = end_x - cx var dy int = end_y - cy if dx < 0 { dx = 0 - dx } if dy < 0 { dy = 0 - dy } var sx int var sy int if cx < end_x { sx = 1 } else { sx = -1 } if cy < end_y { sy = 1 } else { sy = -1 } var err int = dx - dy var n int for n = 0; n < 1000; n++ { img.SetGray(cx, cy, col) if (cx == end_x) && (cy == end_y) { return } var e2 int = 2 * err if e2 > (0 - dy) { err = err - dy cx = cx + sx } if e2 < dx { err = err + dx cy = cy + sy } } }
func setBlock(x, y, blockSize, t int, img image.Image, g *image.Gray) { // Loop through all the pixels in the block for cy := y; cy < y+blockSize; cy++ { if cy >= img.Bounds().Max.Y { break } for cx := x; cx < x+blockSize; cx++ { if cx >= img.Bounds().Max.X { continue } l := int(lumAt(cx, cy, img)) if l > t { g.SetGray(cx, cy, white) } else { g.SetGray(cx, cy, black) } } } }
// kf3 is a generic convolution 3x3 kernel filter that operatates on // images of type image.Gray from the Go standard image library. func kf3(k *[9]float64, src, dst *image.Gray) { for y := src.Rect.Min.Y; y < src.Rect.Max.Y; y++ { for x := src.Rect.Min.X; x < src.Rect.Max.X; x++ { var sum float64 var i int for yo := y - 1; yo <= y+1; yo++ { for xo := x - 1; xo <= x+1; xo++ { if (image.Point{xo, yo}).In(src.Rect) { sum += k[i] * float64(src.At(xo, yo).(color.Gray).Y) } else { sum += k[i] * float64(src.At(x, y).(color.Gray).Y) } i++ } } dst.SetGray(x, y, color.Gray{uint8(math.Min(255, math.Max(0, sum)))}) } } }
func drawRect(img *image.Gray, r image.Rectangle, c color.Gray) { for x := r.Min.X; x < r.Max.X; x++ { img.SetGray(x, r.Min.Y, c) } for x := r.Min.X; x < r.Max.X; x++ { img.SetGray(x, r.Max.Y, c) } for y := r.Min.Y; y < r.Max.Y; y++ { img.SetGray(r.Min.X, y, c) } for y := r.Min.Y; y < r.Max.Y; y++ { img.SetGray(r.Max.X, y, c) } }
// copies and resizes src into dst's r func scaleDownTo(dst *image.Gray, r image.Rectangle, src *image.Gray) error { if src.Bounds().Dx()%r.Dx() != 0 || src.Bounds().Dy()%r.Dy() != 0 { return errors.New("Source image size not multiple of rectagle size") } dx := src.Bounds().Dx() / r.Dx() dy := src.Bounds().Dy() / r.Dy() area := dx * dy for y := r.Min.Y; y < r.Max.Y; y++ { for x := r.Min.X; x < r.Max.X; x++ { col := 0 for v := 0; v < dy; v++ { for u := 0; u < dx; u++ { offset := src.PixOffset((x-r.Min.X)*dx+u, (y-r.Min.Y)*dy+v) col += int(src.Pix[offset]) } } dst.SetGray(x, y, color.Gray{uint8(col / area)}) } } return nil }
// RecogniseDigit takes 28x28 gray image and tries to recognise a digit // panics if image has wrong size func RecogniseDigit(img image.Gray, threshold uint8) (int, float64) { if img.Bounds().Max.X != 28 || img.Bounds().Max.Y != 28 { panic("Image size is invalid, use 28x28.") } input := make([]float64, 28*28, 28*28) pos := 0 for x := 0; x < 28; x++ { for y := 0; y < 28; y++ { val := img.GrayAt(x, y).Y if val < threshold { val = 255 - val } else { val = 0 } img.SetGray(x, y, color.Gray{Y: val}) input[pos] = float64(val) / 255 pos++ } } digit, confidence := argmax(nn.Evaluate(input)) return digit, confidence }
func drawDroplet(img *image.Gray, x, y, r, h float64) { ri := int(r + 1) xi := int(x + 0.5) yi := int(y + 0.5) for i := yi - ri; i <= yi+ri; i++ { for j := xi - ri; j <= xi+ri; j++ { if i < 0 || i >= img.Rect.Dy() || j < 0 || j >= img.Rect.Dx() { continue } di := float64(i) - y dj := float64(j) - x if rr := di*di + dj*dj; rr <= r*r { vprev := img.GrayAt(j, i).Y v := 100*h/r*math.Pow(1-rr/r/r, 1.0)/math.Sqrt(1-di/r) + float64(vprev) if v > 255 { v = 255 } img.SetGray(j, i, color.Gray{uint8(v)}) } } } }
func sdfize(img *image.Gray) { w := img.Bounds().Size().X h := img.Bounds().Size().Y g1 := make([]xys, w*h) g2 := make([]xys, w*h) for y := 0; y < h; y++ { for x := 0; x < w; x++ { a := img.Pix[img.PixOffset(x, y)] if a < 0x7F { g2[y*w+x] = infinity } else { g1[y*w+x] = infinity } } } generateSDF(int16(w), int16(h), g1) generateSDF(int16(w), int16(h), g2) for y := 0; y < h; y++ { for x := 0; x < w; x++ { pos := y*w + x dist1 := distance(g1[pos].x, g1[pos].y) dist2 := distance(g2[pos].x, g2[pos].y) dist := 2 * 128 * (dist1 - dist2) / float64(w) if dist < -128 { dist = -128 } else if dist > 127 { dist = 127 } c := uint8(128 + dist) img.SetGray(x, y, color.Gray{c}) } } }
func drawBubble(img *image.Gray, xPos, yPos, width, height float64, content string, filled bool) { gapBeg := xPos + width/4.0 gapEnd := xPos + width - width/4.0 if filled { drawRect(img, black, xPos, yPos, width, height) return } ftContext.DrawString(content, freetype.Pt(int(gapBeg+width/6), int(yPos+height))) for w := xPos; w < xPos+width; w += 1.0 { if w >= gapBeg && w <= gapEnd { continue } img.SetGray(int(w), int(yPos), color.Gray{0x22}) img.SetGray(int(w), int(yPos+height), color.Gray{0x22}) } for h := yPos; h < yPos+height; h += 1.0 { img.SetGray(int(xPos), int(h), color.Gray{0x22}) img.SetGray(int(xPos+width), int(h), color.Gray{0x22}) } }
// Dithering algorithms // Dithering is the process of reducing the colorspace of an image to a more // restricted palette. Since the Game Boy Printer only has a 2bit colorspace // available, it's important that some dithering algorithms are implemented to // deal with the fact. Depending on the properties of the dithering algorithm // detail may or may not be preserved as well. Using simple techniques like // average dithering will yield results with lots of color banding, while employing // error diffusion techniques such as Floyd Steinberg will produce smoother // results (color banding is replaced by the introduction of noise) // The wikipedia article (http://en.wikipedia.org/wiki/Dither) is quite interesting // and talks about some dithering algorithms, some of which are implemented here. // Floyd Steinberg is an error diffusion dithering algorithm that adds the error // of each color conversion to the neighbours of each pixel. This way it's possible // to achieve a finer graded dithering func ditheringFloydSteinberg(img *image.Gray, p *color.Palette) { size := img.Bounds() for y := size.Min.Y; y < size.Max.Y; y++ { for x := size.Min.X; x < size.Max.X; x++ { c1 := img.GrayAt(x, y) c2 := p.Convert(c1).(color.Gray) delta := int(c1.Y) - int(c2.Y) switch { case x+1 < size.Max.X && y < size.Max.Y: img.SetGray(x+1, y, color.Gray{uint8(int(img.GrayAt(x+1, y).Y) + delta*7/16)}) fallthrough case x < size.Max.X && y+1 < size.Max.Y: img.SetGray(x, y+1, color.Gray{uint8(int(img.GrayAt(x, y+1).Y) + delta*5/16)}) fallthrough case x-1 >= size.Min.X && y+1 < size.Max.Y: img.SetGray(x-1, y+1, color.Gray{uint8(int(img.GrayAt(x-1, y+1).Y) + delta*3/16)}) fallthrough case x+1 < size.Max.X && y+1 < size.Max.Y: img.SetGray(x+1, y+1, color.Gray{uint8(int(img.GrayAt(x+1, y+1).Y) + delta*1/16)}) } img.Set(x, y, c2) } } }
// decode decodes the IDAT data into an image. func (d *decoder) decode() (image.Image, error) { r, err := zlib.NewReader(d) if err != nil { return nil, err } defer r.Close() bitsPerPixel := 0 pixOffset := 0 var ( gray *image.Gray rgba *image.RGBA paletted *image.Paletted nrgba *image.NRGBA gray16 *image.Gray16 rgba64 *image.RGBA64 nrgba64 *image.NRGBA64 img image.Image ) switch d.cb { case cbG1, cbG2, cbG4, cbG8: bitsPerPixel = d.depth gray = image.NewGray(image.Rect(0, 0, d.width, d.height)) img = gray case cbGA8: bitsPerPixel = 16 nrgba = image.NewNRGBA(image.Rect(0, 0, d.width, d.height)) img = nrgba case cbTC8: bitsPerPixel = 24 rgba = image.NewRGBA(image.Rect(0, 0, d.width, d.height)) img = rgba case cbP1, cbP2, cbP4, cbP8: bitsPerPixel = d.depth paletted = image.NewPaletted(image.Rect(0, 0, d.width, d.height), d.palette) img = paletted case cbTCA8: bitsPerPixel = 32 nrgba = image.NewNRGBA(image.Rect(0, 0, d.width, d.height)) img = nrgba case cbG16: bitsPerPixel = 16 gray16 = image.NewGray16(image.Rect(0, 0, d.width, d.height)) img = gray16 case cbGA16: bitsPerPixel = 32 nrgba64 = image.NewNRGBA64(image.Rect(0, 0, d.width, d.height)) img = nrgba64 case cbTC16: bitsPerPixel = 48 rgba64 = image.NewRGBA64(image.Rect(0, 0, d.width, d.height)) img = rgba64 case cbTCA16: bitsPerPixel = 64 nrgba64 = image.NewNRGBA64(image.Rect(0, 0, d.width, d.height)) img = nrgba64 } bytesPerPixel := (bitsPerPixel + 7) / 8 // cr and pr are the bytes for the current and previous row. // The +1 is for the per-row filter type, which is at cr[0]. cr := make([]uint8, 1+(bitsPerPixel*d.width+7)/8) pr := make([]uint8, 1+(bitsPerPixel*d.width+7)/8) for y := 0; y < d.height; y++ { // Read the decompressed bytes. _, err := io.ReadFull(r, cr) if err != nil { return nil, err } // Apply the filter. cdat := cr[1:] pdat := pr[1:] switch cr[0] { case ftNone: // No-op. case ftSub: for i := bytesPerPixel; i < len(cdat); i++ { cdat[i] += cdat[i-bytesPerPixel] } case ftUp: for i, p := range pdat { cdat[i] += p } case ftAverage: for i := 0; i < bytesPerPixel; i++ { cdat[i] += pdat[i] / 2 } for i := bytesPerPixel; i < len(cdat); i++ { cdat[i] += uint8((int(cdat[i-bytesPerPixel]) + int(pdat[i])) / 2) } case ftPaeth: filterPaeth(cdat, pdat, bytesPerPixel) default: return nil, FormatError("bad filter type") } // Convert from bytes to colors. switch d.cb { case cbG1: for x := 0; x < d.width; x += 8 { b := cdat[x/8] for x2 := 0; x2 < 8 && x+x2 < d.width; x2++ { gray.SetGray(x+x2, y, color.Gray{(b >> 7) * 0xff}) b <<= 1 } } case cbG2: for x := 0; x < d.width; x += 4 { b := cdat[x/4] for x2 := 0; x2 < 4 && x+x2 < d.width; x2++ { gray.SetGray(x+x2, y, color.Gray{(b >> 6) * 0x55}) b <<= 2 } } case cbG4: for x := 0; x < d.width; x += 2 { b := cdat[x/2] for x2 := 0; x2 < 2 && x+x2 < d.width; x2++ { gray.SetGray(x+x2, y, color.Gray{(b >> 4) * 0x11}) b <<= 4 } } case cbG8: copy(gray.Pix[pixOffset:], cdat) pixOffset += gray.Stride case cbGA8: for x := 0; x < d.width; x++ { ycol := cdat[2*x+0] nrgba.SetNRGBA(x, y, color.NRGBA{ycol, ycol, ycol, cdat[2*x+1]}) } case cbTC8: pix, i, j := rgba.Pix, pixOffset, 0 for x := 0; x < d.width; x++ { pix[i+0] = cdat[j+0] pix[i+1] = cdat[j+1] pix[i+2] = cdat[j+2] pix[i+3] = 0xff i += 4 j += 3 } pixOffset += rgba.Stride case cbP1: for x := 0; x < d.width; x += 8 { b := cdat[x/8] for x2 := 0; x2 < 8 && x+x2 < d.width; x2++ { idx := b >> 7 if len(paletted.Palette) <= int(idx) { paletted.Palette = paletted.Palette[:int(idx)+1] } paletted.SetColorIndex(x+x2, y, idx) b <<= 1 } } case cbP2: for x := 0; x < d.width; x += 4 { b := cdat[x/4] for x2 := 0; x2 < 4 && x+x2 < d.width; x2++ { idx := b >> 6 if len(paletted.Palette) <= int(idx) { paletted.Palette = paletted.Palette[:int(idx)+1] } paletted.SetColorIndex(x+x2, y, idx) b <<= 2 } } case cbP4: for x := 0; x < d.width; x += 2 { b := cdat[x/2] for x2 := 0; x2 < 2 && x+x2 < d.width; x2++ { idx := b >> 4 if len(paletted.Palette) <= int(idx) { paletted.Palette = paletted.Palette[:int(idx)+1] } paletted.SetColorIndex(x+x2, y, idx) b <<= 4 } } case cbP8: if len(paletted.Palette) != 255 { for x := 0; x < d.width; x++ { if len(paletted.Palette) <= int(cdat[x]) { paletted.Palette = paletted.Palette[:int(cdat[x])+1] } } } copy(paletted.Pix[pixOffset:], cdat) pixOffset += paletted.Stride case cbTCA8: copy(nrgba.Pix[pixOffset:], cdat) pixOffset += nrgba.Stride case cbG16: for x := 0; x < d.width; x++ { ycol := uint16(cdat[2*x+0])<<8 | uint16(cdat[2*x+1]) gray16.SetGray16(x, y, color.Gray16{ycol}) } case cbGA16: for x := 0; x < d.width; x++ { ycol := uint16(cdat[4*x+0])<<8 | uint16(cdat[4*x+1]) acol := uint16(cdat[4*x+2])<<8 | uint16(cdat[4*x+3]) nrgba64.SetNRGBA64(x, y, color.NRGBA64{ycol, ycol, ycol, acol}) } case cbTC16: for x := 0; x < d.width; x++ { rcol := uint16(cdat[6*x+0])<<8 | uint16(cdat[6*x+1]) gcol := uint16(cdat[6*x+2])<<8 | uint16(cdat[6*x+3]) bcol := uint16(cdat[6*x+4])<<8 | uint16(cdat[6*x+5]) rgba64.SetRGBA64(x, y, color.RGBA64{rcol, gcol, bcol, 0xffff}) } case cbTCA16: for x := 0; x < d.width; x++ { rcol := uint16(cdat[8*x+0])<<8 | uint16(cdat[8*x+1]) gcol := uint16(cdat[8*x+2])<<8 | uint16(cdat[8*x+3]) bcol := uint16(cdat[8*x+4])<<8 | uint16(cdat[8*x+5]) acol := uint16(cdat[8*x+6])<<8 | uint16(cdat[8*x+7]) nrgba64.SetNRGBA64(x, y, color.NRGBA64{rcol, gcol, bcol, acol}) } } // The current row for y is the previous row for y+1. pr, cr = cr, pr } // Check for EOF, to verify the zlib checksum. n, err := r.Read(pr[:1]) if err != io.EOF { return nil, FormatError(err.Error()) } if n != 0 || d.idatLength != 0 { return nil, FormatError("too much pixel data") } return img, nil }
// readImagePass reads a single image pass, sized according to the pass number. func (d *decoder) readImagePass(r io.Reader, pass int, allocateOnly bool) (image.Image, error) { var bitsPerPixel int = 0 pixOffset := 0 var ( gray *image.Gray rgba *image.RGBA paletted *image.Paletted nrgba *image.NRGBA gray16 *image.Gray16 rgba64 *image.RGBA64 nrgba64 *image.NRGBA64 img image.Image ) width, height := d.width, d.height if d.interlace == itAdam7 && !allocateOnly { p := interlacing[pass] // Add the multiplication factor and subtract one, effectively rounding up. width = (width - p.xOffset + p.xFactor - 1) / p.xFactor height = (height - p.yOffset + p.yFactor - 1) / p.yFactor // A PNG image can't have zero width or height, but for an interlaced // image, an individual pass might have zero width or height. If so, we // shouldn't even read a per-row filter type byte, so return early. if width == 0 || height == 0 { return nil, nil } } switch d.cb { case cbG1, cbG2, cbG4, cbG8: bitsPerPixel = d.depth gray = image.NewGray(image.Rect(0, 0, width, height)) img = gray case cbGA8: bitsPerPixel = 16 nrgba = image.NewNRGBA(image.Rect(0, 0, width, height)) img = nrgba case cbTC8: bitsPerPixel = 24 rgba = image.NewRGBA(image.Rect(0, 0, width, height)) img = rgba case cbP1, cbP2, cbP4, cbP8: bitsPerPixel = d.depth paletted = image.NewPaletted(image.Rect(0, 0, width, height), d.palette) img = paletted case cbTCA8: bitsPerPixel = 32 nrgba = image.NewNRGBA(image.Rect(0, 0, width, height)) img = nrgba case cbG16: bitsPerPixel = 16 gray16 = image.NewGray16(image.Rect(0, 0, width, height)) img = gray16 case cbGA16: bitsPerPixel = 32 nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height)) img = nrgba64 case cbTC16: bitsPerPixel = 48 rgba64 = image.NewRGBA64(image.Rect(0, 0, width, height)) img = rgba64 case cbTCA16: bitsPerPixel = 64 nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height)) img = nrgba64 } if allocateOnly { return img, nil } bytesPerPixel := (bitsPerPixel + 7) / 8 // The +1 is for the per-row filter type, which is at cr[0]. rowSize := 1 + (bitsPerPixel*width+7)/8 // cr and pr are the bytes for the current and previous row. cr := make([]uint8, rowSize) pr := make([]uint8, rowSize) for y := 0; y < height; y++ { // Read the decompressed bytes. _, err := io.ReadFull(r, cr) if err != nil { if err == io.EOF || err == io.ErrUnexpectedEOF { return nil, FormatError("not enough pixel data") } return nil, err } // Apply the filter. cdat := cr[1:] pdat := pr[1:] switch cr[0] { case ftNone: // No-op. case ftSub: for i := bytesPerPixel; i < len(cdat); i++ { cdat[i] += cdat[i-bytesPerPixel] } case ftUp: for i, p := range pdat { cdat[i] += p } case ftAverage: // The first column has no column to the left of it, so it is a // special case. We know that the first column exists because we // check above that width != 0, and so len(cdat) != 0. for i := 0; i < bytesPerPixel; i++ { cdat[i] += pdat[i] / 2 } for i := bytesPerPixel; i < len(cdat); i++ { cdat[i] += uint8((int(cdat[i-bytesPerPixel]) + int(pdat[i])) / 2) } case ftPaeth: filterPaeth(cdat, pdat, bytesPerPixel) default: return nil, FormatError("bad filter type") } // Convert from bytes to colors. switch d.cb { case cbG1: for x := 0; x < width; x += 8 { b := cdat[x/8] for x2 := 0; x2 < 8 && x+x2 < width; x2++ { gray.SetGray(x+x2, y, color.Gray{(b >> 7) * 0xff}) b <<= 1 } } case cbG2: for x := 0; x < width; x += 4 { b := cdat[x/4] for x2 := 0; x2 < 4 && x+x2 < width; x2++ { gray.SetGray(x+x2, y, color.Gray{(b >> 6) * 0x55}) b <<= 2 } } case cbG4: for x := 0; x < width; x += 2 { b := cdat[x/2] for x2 := 0; x2 < 2 && x+x2 < width; x2++ { gray.SetGray(x+x2, y, color.Gray{(b >> 4) * 0x11}) b <<= 4 } } case cbG8: copy(gray.Pix[pixOffset:], cdat) pixOffset += gray.Stride case cbGA8: for x := 0; x < width; x++ { ycol := cdat[2*x+0] nrgba.SetNRGBA(x, y, color.NRGBA{ycol, ycol, ycol, cdat[2*x+1]}) } case cbTC8: pix, i, j := rgba.Pix, pixOffset, 0 for x := 0; x < width; x++ { pix[i+0] = cdat[j+0] pix[i+1] = cdat[j+1] pix[i+2] = cdat[j+2] pix[i+3] = 0xff i += 4 j += 3 } pixOffset += rgba.Stride case cbP1: for x := 0; x < width; x += 8 { b := cdat[x/8] for x2 := 0; x2 < 8 && x+x2 < width; x2++ { idx := b >> 7 if len(paletted.Palette) <= int(idx) { paletted.Palette = paletted.Palette[:int(idx)+1] } paletted.SetColorIndex(x+x2, y, idx) b <<= 1 } } case cbP2: for x := 0; x < width; x += 4 { b := cdat[x/4] for x2 := 0; x2 < 4 && x+x2 < width; x2++ { idx := b >> 6 if len(paletted.Palette) <= int(idx) { paletted.Palette = paletted.Palette[:int(idx)+1] } paletted.SetColorIndex(x+x2, y, idx) b <<= 2 } } case cbP4: for x := 0; x < width; x += 2 { b := cdat[x/2] for x2 := 0; x2 < 2 && x+x2 < width; x2++ { idx := b >> 4 if len(paletted.Palette) <= int(idx) { paletted.Palette = paletted.Palette[:int(idx)+1] } paletted.SetColorIndex(x+x2, y, idx) b <<= 4 } } case cbP8: if len(paletted.Palette) != 255 { for x := 0; x < width; x++ { if len(paletted.Palette) <= int(cdat[x]) { paletted.Palette = paletted.Palette[:int(cdat[x])+1] } } } copy(paletted.Pix[pixOffset:], cdat) pixOffset += paletted.Stride case cbTCA8: copy(nrgba.Pix[pixOffset:], cdat) pixOffset += nrgba.Stride case cbG16: for x := 0; x < width; x++ { ycol := uint16(cdat[2*x+0])<<8 | uint16(cdat[2*x+1]) gray16.SetGray16(x, y, color.Gray16{ycol}) } case cbGA16: for x := 0; x < width; x++ { ycol := uint16(cdat[4*x+0])<<8 | uint16(cdat[4*x+1]) acol := uint16(cdat[4*x+2])<<8 | uint16(cdat[4*x+3]) nrgba64.SetNRGBA64(x, y, color.NRGBA64{ycol, ycol, ycol, acol}) } case cbTC16: for x := 0; x < width; x++ { rcol := uint16(cdat[6*x+0])<<8 | uint16(cdat[6*x+1]) gcol := uint16(cdat[6*x+2])<<8 | uint16(cdat[6*x+3]) bcol := uint16(cdat[6*x+4])<<8 | uint16(cdat[6*x+5]) rgba64.SetRGBA64(x, y, color.RGBA64{rcol, gcol, bcol, 0xffff}) } case cbTCA16: for x := 0; x < width; x++ { rcol := uint16(cdat[8*x+0])<<8 | uint16(cdat[8*x+1]) gcol := uint16(cdat[8*x+2])<<8 | uint16(cdat[8*x+3]) bcol := uint16(cdat[8*x+4])<<8 | uint16(cdat[8*x+5]) acol := uint16(cdat[8*x+6])<<8 | uint16(cdat[8*x+7]) nrgba64.SetNRGBA64(x, y, color.NRGBA64{rcol, gcol, bcol, acol}) } } // The current row for y is the previous row for y+1. pr, cr = cr, pr } return img, nil }
func (d *decoder) idatReader(idat io.Reader) (image.Image, os.Error) { r, err := zlib.NewReader(idat) if err != nil { return nil, err } defer r.Close() bitsPerPixel := 0 maxPalette := uint8(0) var ( gray *image.Gray rgba *image.RGBA paletted *image.Paletted nrgba *image.NRGBA gray16 *image.Gray16 rgba64 *image.RGBA64 nrgba64 *image.NRGBA64 img image.Image ) switch d.cb { case cbG1, cbG2, cbG4, cbG8: bitsPerPixel = d.depth gray = image.NewGray(d.width, d.height) img = gray case cbGA8: bitsPerPixel = 16 nrgba = image.NewNRGBA(d.width, d.height) img = nrgba case cbTC8: bitsPerPixel = 24 rgba = image.NewRGBA(d.width, d.height) img = rgba case cbP1, cbP2, cbP4, cbP8: bitsPerPixel = d.depth paletted = image.NewPaletted(d.width, d.height, d.palette) img = paletted maxPalette = uint8(len(d.palette) - 1) case cbTCA8: bitsPerPixel = 32 nrgba = image.NewNRGBA(d.width, d.height) img = nrgba case cbG16: bitsPerPixel = 16 gray16 = image.NewGray16(d.width, d.height) img = gray16 case cbGA16: bitsPerPixel = 32 nrgba64 = image.NewNRGBA64(d.width, d.height) img = nrgba64 case cbTC16: bitsPerPixel = 48 rgba64 = image.NewRGBA64(d.width, d.height) img = rgba64 case cbTCA16: bitsPerPixel = 64 nrgba64 = image.NewNRGBA64(d.width, d.height) img = nrgba64 } bytesPerPixel := (bitsPerPixel + 7) / 8 // cr and pr are the bytes for the current and previous row. // The +1 is for the per-row filter type, which is at cr[0]. cr := make([]uint8, 1+(bitsPerPixel*d.width+7)/8) pr := make([]uint8, 1+(bitsPerPixel*d.width+7)/8) for y := 0; y < d.height; y++ { // Read the decompressed bytes. _, err := io.ReadFull(r, cr) if err != nil { return nil, err } // Apply the filter. cdat := cr[1:] pdat := pr[1:] switch cr[0] { case ftNone: // No-op. case ftSub: for i := bytesPerPixel; i < len(cdat); i++ { cdat[i] += cdat[i-bytesPerPixel] } case ftUp: for i := 0; i < len(cdat); i++ { cdat[i] += pdat[i] } case ftAverage: for i := 0; i < bytesPerPixel; i++ { cdat[i] += pdat[i] / 2 } for i := bytesPerPixel; i < len(cdat); i++ { cdat[i] += uint8((int(cdat[i-bytesPerPixel]) + int(pdat[i])) / 2) } case ftPaeth: for i := 0; i < bytesPerPixel; i++ { cdat[i] += paeth(0, pdat[i], 0) } for i := bytesPerPixel; i < len(cdat); i++ { cdat[i] += paeth(cdat[i-bytesPerPixel], pdat[i], pdat[i-bytesPerPixel]) } default: return nil, FormatError("bad filter type") } // Convert from bytes to colors. switch d.cb { case cbG1: for x := 0; x < d.width; x += 8 { b := cdat[x/8] for x2 := 0; x2 < 8 && x+x2 < d.width; x2++ { gray.SetGray(x+x2, y, image.GrayColor{(b >> 7) * 0xff}) b <<= 1 } } case cbG2: for x := 0; x < d.width; x += 4 { b := cdat[x/4] for x2 := 0; x2 < 4 && x+x2 < d.width; x2++ { gray.SetGray(x+x2, y, image.GrayColor{(b >> 6) * 0x55}) b <<= 2 } } case cbG4: for x := 0; x < d.width; x += 2 { b := cdat[x/2] for x2 := 0; x2 < 2 && x+x2 < d.width; x2++ { gray.SetGray(x+x2, y, image.GrayColor{(b >> 4) * 0x11}) b <<= 4 } } case cbG8: for x := 0; x < d.width; x++ { gray.SetGray(x, y, image.GrayColor{cdat[x]}) } case cbGA8: for x := 0; x < d.width; x++ { ycol := cdat[2*x+0] nrgba.SetNRGBA(x, y, image.NRGBAColor{ycol, ycol, ycol, cdat[2*x+1]}) } case cbTC8: for x := 0; x < d.width; x++ { rgba.SetRGBA(x, y, image.RGBAColor{cdat[3*x+0], cdat[3*x+1], cdat[3*x+2], 0xff}) } case cbP1: for x := 0; x < d.width; x += 8 { b := cdat[x/8] for x2 := 0; x2 < 8 && x+x2 < d.width; x2++ { idx := b >> 7 if idx > maxPalette { return nil, FormatError("palette index out of range") } paletted.SetColorIndex(x+x2, y, idx) b <<= 1 } } case cbP2: for x := 0; x < d.width; x += 4 { b := cdat[x/4] for x2 := 0; x2 < 4 && x+x2 < d.width; x2++ { idx := b >> 6 if idx > maxPalette { return nil, FormatError("palette index out of range") } paletted.SetColorIndex(x+x2, y, idx) b <<= 2 } } case cbP4: for x := 0; x < d.width; x += 2 { b := cdat[x/2] for x2 := 0; x2 < 2 && x+x2 < d.width; x2++ { idx := b >> 4 if idx > maxPalette { return nil, FormatError("palette index out of range") } paletted.SetColorIndex(x+x2, y, idx) b <<= 4 } } case cbP8: for x := 0; x < d.width; x++ { if cdat[x] > maxPalette { return nil, FormatError("palette index out of range") } paletted.SetColorIndex(x, y, cdat[x]) } case cbTCA8: for x := 0; x < d.width; x++ { nrgba.SetNRGBA(x, y, image.NRGBAColor{cdat[4*x+0], cdat[4*x+1], cdat[4*x+2], cdat[4*x+3]}) } case cbG16: for x := 0; x < d.width; x++ { ycol := uint16(cdat[2*x+0])<<8 | uint16(cdat[2*x+1]) gray16.SetGray16(x, y, image.Gray16Color{ycol}) } case cbGA16: for x := 0; x < d.width; x++ { ycol := uint16(cdat[4*x+0])<<8 | uint16(cdat[4*x+1]) acol := uint16(cdat[4*x+2])<<8 | uint16(cdat[4*x+3]) nrgba64.SetNRGBA64(x, y, image.NRGBA64Color{ycol, ycol, ycol, acol}) } case cbTC16: for x := 0; x < d.width; x++ { rcol := uint16(cdat[6*x+0])<<8 | uint16(cdat[6*x+1]) gcol := uint16(cdat[6*x+2])<<8 | uint16(cdat[6*x+3]) bcol := uint16(cdat[6*x+4])<<8 | uint16(cdat[6*x+5]) rgba64.SetRGBA64(x, y, image.RGBA64Color{rcol, gcol, bcol, 0xffff}) } case cbTCA16: for x := 0; x < d.width; x++ { rcol := uint16(cdat[8*x+0])<<8 | uint16(cdat[8*x+1]) gcol := uint16(cdat[8*x+2])<<8 | uint16(cdat[8*x+3]) bcol := uint16(cdat[8*x+4])<<8 | uint16(cdat[8*x+5]) acol := uint16(cdat[8*x+6])<<8 | uint16(cdat[8*x+7]) nrgba64.SetNRGBA64(x, y, image.NRGBA64Color{rcol, gcol, bcol, acol}) } } // The current row for y is the previous row for y+1. pr, cr = cr, pr } return img, nil }
func drawLine(img *image.Gray, x, y, h int, c color.Gray) { for cy := y; cy <= y+h; cy++ { img.SetGray(x, cy, c) } }