Skip to content

Commit

Permalink
LDEV-5286 add function ImageGetPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Jan 29, 2025
1 parent cc28fb2 commit b8dc043
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
31 changes: 31 additions & 0 deletions source/fld/function.fld
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,37 @@ Value must be greater than or equal to 3 and less than or equal to 10. The defau
<type>number</type>
</return>
</function>
<function>
<name>ImageGetPoint</name>
<class bundle-name="{bundle-name}" bundle-version="{bundle-version}">org.lucee.extension.image.functions.ImageGetPoint</class>
<member-name>getPoint</member-name>
<member-type>image</member-type>
<member-chaining>false</member-chaining>
<keywords>image</keywords>
<description>Get the pixel color at the specified (x,y) coordinate.</description>
<argument>
<name>image</name>
<alias>name</alias>
<type>any</type>
<required>Yes</required>
<description>The image on which this operation is performed.</description>
</argument>
<argument>
<name>x</name>
<type>number</type>
<required>Yes</required>
<description>The x coordinate of the point.</description>
</argument>
<argument>
<name>y</name>
<type>number</type>
<required>Yes</required>
<description>The y coordinate of the point.</description>
</argument>
<return>
<type>void</type>
</return>
</function>
<function>
<name>ImageGetIPTCTag</name>
<class bundle-name="{bundle-name}" bundle-version="{bundle-version}">org.lucee.extension.image.functions.ImageGetIPTCTag</class>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.lucee.extension.image.functions;

import java.awt.Color;
import java.awt.image.BufferedImage;

import lucee.runtime.PageContext;
import lucee.runtime.exp.PageException;
import lucee.runtime.type.Struct;

import org.lucee.extension.image.Image;

public class ImageGetPoint extends FunctionSupport {

public static Struct call(PageContext pc, Object name, double x, double y) throws PageException {
Image img = Image.toImage(pc,name);
BufferedImage bi = img.getBufferedImage();

Struct pixel = eng.getCreationUtil().createStruct();
Color color = new Color( bi.getRGB( (int)x, (int)y ) );
pixel.setEL("x", x);
pixel.setEL("y", y);
pixel.setEL("red", color.getRed());
pixel.setEL("blue", color.getBlue());
pixel.setEL("green", color.getGreen());
pixel.setEL("alpha", color.getAlpha());
pixel.setEL("hex", "#"+Integer.toHexString(color.getRGB()).substring(2));
return pixel;
}

@Override
public Object invoke(PageContext pc, Object[] args) throws PageException {
if(args.length==3) return call(pc, args[0],cast.toDoubleValue(args[1]),cast.toDoubleValue(args[2]));
throw exp.createFunctionException(pc, "ImageGetPoint", 3, 3, args.length);
}
}
40 changes: 40 additions & 0 deletions tests/ImageGetPoint.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
component extends="org.lucee.cfml.test.LuceeTestCase" labels="image2" {

function run( testResults, testBox ){
describe( "test case for ImageDrawPoint", function() {

it(title = "Checking with ImageDrawOval", body = function( currentSpec ){
var img = imageNew("",200,200,"rgb","red");
imageSetDrawingColor(img, "white");
checkPoint( img, 90, 90, "red" );

imageDrawPoint( img, 90, 90 );
checkPoint( img, 90, 90, "white" );
});

});
}

private function checkPoint( img, x, y, color ){
var pixel = img.getPoint( x, y );
var pixel2 = ImageGetPoint( img, x, y );

structEach( pixel, function( k, v ) {
expect( pixel[ k ] ).toBe( pixel2[ k ] );
});

if (color eq "white"){
expect( pixel.red ).toBe( 255 );
expect( pixel.blue ).toBe( 255 );
expect( pixel.green ).toBe( 255 );
expect( pixel.hex ).toBe( "##ffffff" );
} else {
expect( pixel.red ).toBe( 255 );
expect( pixel.blue ).toBe( 0 );
expect( pixel.green ).toBe( 0 );
expect( pixel.hex ).toBe( "##ff0000" );
}
}

}

0 comments on commit b8dc043

Please sign in to comment.