...
Tawesoft Logo

Source file src/tawesoft.co.uk/go/ximage/red.go

Documentation: src/tawesoft.co.uk/go/ximage/red.go

     1  // Based on https://golang.org/src/image/image.go
     2  // Copyright 2009 The Go Authors. All rights reserved.
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package ximage
     7  
     8  import (
     9      "image"
    10      "image/color"
    11      "tawesoft.co.uk/go/ximage/xcolor"
    12  )
    13  
    14  // Red is an in-memory image whose At method returns color.Red values.
    15  type Red struct {
    16  	// Pix holds the image's pixels, as Red values. The pixel at
    17  	// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*1].
    18  	Pix []uint8
    19  	// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
    20  	Stride int
    21  	// Rect is the image's bounds.
    22  	Rect image.Rectangle
    23  }
    24  
    25  func (p *Red) ColorModel() color.Model { return xcolor.RedModel }
    26  
    27  func (p *Red) Bounds() image.Rectangle { return p.Rect }
    28  
    29  func (p *Red) At(x, y int) color.Color {
    30  	return p.RedAt(x, y)
    31  }
    32  
    33  func (p *Red) RedAt(x, y int) xcolor.Red {
    34  	if !(image.Point{x, y}.In(p.Rect)) {
    35  		return xcolor.Red{}
    36  	}
    37  	i := p.PixOffset(x, y)
    38  	return xcolor.Red{R: p.Pix[i]}
    39  }
    40  
    41  // PixOffset returns the index of the first element of Pix that corresponds to
    42  // the pixel at (x, y).
    43  func (p *Red) PixOffset(x, y int) int {
    44  	return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*1
    45  }
    46  
    47  func (p *Red) Set(x, y int, c color.Color) {
    48  	if !(image.Point{x, y}.In(p.Rect)) {
    49  		return
    50  	}
    51  	i := p.PixOffset(x, y)
    52  	p.Pix[i] = xcolor.RedModel.Convert(c).(color.RGBA).R
    53  }
    54  
    55  func (p *Red) SetRed(x, y int, c xcolor.Red) {
    56  	if !(image.Point{x, y}.In(p.Rect)) {
    57  		return
    58  	}
    59  	i := p.PixOffset(x, y)
    60  	p.Pix[i] = c.R
    61  }
    62  
    63  // SubImage returns an image representing the portion of the image p visible
    64  // through r. The returned value shares pixels with the original image.
    65  func (p *Red) SubImage(r image.Rectangle) image.Image {
    66  	r = r.Intersect(p.Rect)
    67  	// If r1 and r2 are Rectangles, r1.Intersect(r2) is not guaranteed to be inside
    68  	// either r1 or r2 if the intersection is empty. Without explicitly checking for
    69  	// this, the Pix[i:] expression below can panic.
    70  	if r.Empty() {
    71  		return &Red{}
    72  	}
    73  	i := p.PixOffset(r.Min.X, r.Min.Y)
    74  	return &Red{
    75  		Pix:    p.Pix[i:],
    76  		Stride: p.Stride,
    77  		Rect:   r,
    78  	}
    79  }
    80  
    81  // Opaque scans the entire image and reports whether it is fully opaque.
    82  func (p *Red) Opaque() bool {
    83  	return true
    84  }
    85  
    86  // NewRed returns a new Red image with the given bounds.
    87  func NewRed(r image.Rectangle) *Red {
    88  	w, h := r.Dx(), r.Dy()
    89  	pix := make([]uint8, 1*w*h)
    90  	return &Red{pix, 1 * w, r}
    91  }
    92  

View as plain text