Skip to content

Commit

Permalink
More image array tests, more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pcantrell committed Nov 2, 2024
1 parent 005f162 commit 30622ed
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/edu/macalester/graphics/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public Image(double x, double y, String path) {
* (see {@link PixelFormat}.)
* <p>
* For example, the array <code>{ 1, 0.5f, 0, 0, 0, 0.5f }</code> with the RGB pixel format
* specifies one orange pixel (R=1, G=0.5, B=0), then one dark blue pixel (R=0, G=0, B=0.5).
* specifies one orange pixel (R=100%, G=50%, B=0%), then one dark blue pixel (R=0%, G=0%, B=50%).
*
* @param width Image width in pixels
* @param height Image height in pixels
Expand All @@ -146,7 +146,7 @@ public Image(int width, int height, float[] pixels, PixelFormat format) {
* (see {@link PixelFormat}.)
* <p>
* For example, the array <code>{ -1, 127, 0, 0, 0, 127 }</code> with the RGB pixel format
* specifies one orange pixel (R=1, G=0.5, B=0), then one dark blue pixel (R=0, G=0, B=0.5).
* specifies one orange pixel (R=100%, G=50%, B=0%), then one dark blue pixel (R=0%, G=0%, B=50%).
*
* @param width Image width in pixels
* @param height Image height in pixels
Expand Down Expand Up @@ -273,10 +273,32 @@ private double getScaleToFit() {
maxHeight / getImageHeight()));
}

/**
* Returns the pixels in this image as an array of bytes, one byte per color channel per pixel,
* interleaved in the order specified by <code>format</code>. Zero is minimum intensity, and
* 255 (or -1, since bytes are signed) is full intensity.
* <p>
* Note that when requesting a grayscale image, this method averages the color channels, which
* produces results that correspond poorly to perceived brightness.
*
* @see #Image(int,int,byte[],PixelFormat)
* @see #toFloatArray(PixelFormat)
*/
public byte[] toByteArray(PixelFormat format) {
return format.makeByteArray(img);
}

/**
* Returns the pixels in this image as an array of floats, one number per color channel per
* pixel, interleaved in the order specified by <code>format</code>. Zero is minimum intensity,
* and 1 is full intensity.
* <p>
* Note that when requesting a grayscale image, this method averages the color channels, which
* produces results that correspond poorly to perceived brightness.
*
* @see #Image(int,int,float[],PixelFormat)
* @see #toByteArray(PixelFormat)
*/
public float[] toFloatArray(PixelFormat format) {
var bytes = toByteArray(format);
var floats = new float[bytes.length];
Expand Down
16 changes: 16 additions & 0 deletions test/edu/macalester/graphics/ImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ private float[] generateFloatData(int w, int h, int chans) {
return pixels;
}

@RenderingTest(width=200, height=200, modes = { PLAIN })
void imageFileProcessing() {
Image original = new Image(FOXFLOWER_IMAGE);
float[] pixels = original.toFloatArray(Image.PixelFormat.RGB);

for(int i = 0; i < pixels.length; i++) {
pixels[i] = (float) (Math.sin(pixels[i] * Math.PI * 2) + 1) / 2;
}

image = new Image(
original.getImageWidth(),
original.getImageHeight(),
pixels,
Image.PixelFormat.RGB);
}

@RenderingTest(height=200, modes = { PLAIN })
void colorPixelConversionToGrayscaleArray() {
Image colorImage = new Image(FOXBOT_IMAGE);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 30622ed

Please sign in to comment.