Programmer Reference : Common Graphics : Device-independent images : Creating and manipulating images
Creating and manipulating images
To create a blank image, use the width:height:depth:palette: class method of CgDeviceIndependentImage. The image data is initialized to all zeros. The following example shows how to create a 100-by-50 pixel gray-scale image with 8 bits per pixel. Because the palette specifies black for pixel value 0, the image is initially all black.
| colors color palette image line gc |
colors := Array new: 256.
0 to: 255 do: [:i |
color := CgRGBColor intensity: i * 65535 // 255.
colors at: i + 1 put: color].
palette := CgIndexedPalette colors: colors.
image := CgDeviceIndependentImage
width: 100
height: 50
depth: 8
palette: palette.
The image attributes can be retrieved using the width, height, depth, palette, and data methods of CgDeviceIndependentImage. The extent method answers the width and height as a point.
To modify a single pixel value, use the putPixel:y:pixelValue: method. This method takes the x- and y- coordinates of the pixel and stores the new pixel value at that location in the image.
As with the drawing methods described in previous sections, the x-and y-coordinates within images start at 0. The point 0@0 is at the top left corner of the image, with the x-coordinate increasing from left to right, and the y-coordinate increasing from top to bottom.
Continuing the previous example, the following code modifies the image data one pixel at a time. The resulting image has adjacent vertical stripes increasing in intensity from black at the left to white at the right.
0 to: 99 do: [:x |
intensity := x * 255 // 99.
0 to: 49 do: [:y |
image putPixel: x y: y pixelValue: intensity]].
A more efficient way of modifying image data is to use the putPixels:y:width:pixels:startIndex: method, which modifies a horizontal line of pixels in a given scanline. The following code modifies the image data with the same result as the previous code, but is more efficient.
line := ByteArray new: 100.
0 to: 99 do: [:x |
line at: x + 1 put: x * 255 // 99].
0 to: 49 do: [:y |
image putPixels: 0 y: y width: 100 pixels: line startIndex: 1].
Image data can be retrieved one pixel at a time using the getPixel:y: method. A horizontal line of pixels can be retrieved using getPixels:y:width:pixels:startIndex:. A new image can be copied from an area of an existing image using the getSubImage: method and passing it a rectangle. The getPixel: and putPixel: methods have getColor: and putColor: equivalents that answer and accept CgRGBColor objects rather than pixel values and use the image's palette to convert between colors and pixel values.
Last modified date: 01/29/2015