Skip to content

Instantly share code, notes, and snippets.

@nanjizal
Created October 21, 2023 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanjizal/b9b777c3ebb757ccd93c00218db4ab87 to your computer and use it in GitHub Desktop.
Save nanjizal/b9b777c3ebb757ccd93c00218db4ab87 to your computer and use it in GitHub Desktop.
Testing pixelimagexy bytes verses UInt32Array
//https://try.haxe.org/#A9A751b4
import haxe.io.UInt32Array;
import haxe.io.Bytes;
class Test {
static function main() {
ImageStruct.imageType = BYTEIMAGE;
var img = new Pixelimage( 10, 10 );
img.image[0] = 0xFFFF0000;
ImageStruct.imageType = UINT32ARRAY;
var img2 = new Pixelimage( 10, 10 );
img2.image[0] = 0xFF00FF00;
trace( StringTools.hex(img.image[0]) + ' ' + StringTools.hex( img2.image[0] ) );
}
}
enum abstract AbstractEnumImageType( Int ) to Int from Int{
var UINT32ARRAY;
var BYTEIMAGE;
}
typedef ImageDef = {
public var length(default, default):Int;
public function set( index: Int, value: UInt ): UInt;
public function get( index: Int ): UInt;
}
@:transient
@:forward
abstract ImgDef( ImageDef ){
public inline function new( img: ImageDef ){
this = img;
}
@:arrayAccess
function set( index: Int, value: UInt ): UInt {
return this.set( index, value );
}
@:arrayAccess
function get( index: Int ): UInt{
return this.get( index );
}
}
class ByteImage_ {
public var width: Int;
public var height: Int;
public var image: Bytes;
public function new( width: Int, height: Int, image: Bytes ){
this.width = width;
this.height = height;
this.image = image;
}
}
@:structInit
class ArrayImage_ {
public var width: Int;
public var height: Int;
public var data: Array<Int>;
public function new( width: Int, height: Int, data: Array<Int> ){
this.width = width;
this.height = height;
this.data = data;
}
}
@:structInit
class UInt32ArrayImage_ {
public var width: Int;
public var height: Int;
public var data: haxe.io.UInt32Array ;
public function new( width: Int, height: Int, data: haxe.io.UInt32Array ){
this.width = width;
this.height = height;
this.data = data;
}
}
@:forward
@:transient
abstract ByteImage( ByteImage_ ) from ByteImage_ to ByteImage_ {
public inline
function new( w: Int, h: Int ){
this = new ByteImage_( w, h, Bytes.alloc( w * h * 4 ) );
zero();
}
public inline
function zero(){
var w = 0;
for( y in 0...this.height ) {
for( x in 0...this.width ) {
this.image.set(w++,0);
this.image.set(w++,0);
this.image.set(w++,0);
this.image.set(w++,0);
}
}
}
@:from
public static
function fromArrayImage( img: ArrayImage_ ): ByteImage {
var l = img.data.length;
var out = new ByteImage( img.width, img.height );
for( i in 0...l ) out[ i ] = img.data[ i ];
return out;
}
@:from
public static
function fromUInt32ArrayImage( img: UInt32ArrayImage_ ): ByteImage {
var l = img.data.length;
var out = new ByteImage( img.width, img.height );
for( i in 0...l ) out[ i ] = img.data[ i ];
return out;
}
@:to
public inline
function toUInt32Array(): haxe.io.UInt32Array {
var out = new haxe.io.UInt32Array( length );
for( i in 0...length ) out[ i ] = abstract[ i ];
return out;
}
@:to
public inline
function toArray(): Array<Int> {
#if (cpp && !cppia)
var result = cpp.NativeArray.create( length );
for( i in 0...length ) {
cpp.NativeArray.unsafeSet( result, i, abstract[ i ] );
}
return result;
#else
return [ for( i in 0...length ) abstract[ i ] ];
#end
}
#if js
@:to
public inline
function toUInt8Array(): js.lib.Uint8Array {
var dataimg: js.lib.Uint32Array = cast toUInt32Array();
return new js.lib.Uint8Array( dataimg.buffer );
}
#else
@:to
public inline
function toUInt8Array(): haxe.io.UInt8Array {
return haxe.io.UInt8Array.fromBytes( toUInt32Array().view.buffer );
}
#end
public inline
function position( px: Float, py: Float ): Int {
// key
return Std.int( py * this.width + px );
}
@:arrayAccess
public inline
function get( key: Int ): UInt {
return this.image.getInt32( Std.int( key*4 ) );
}
@:arrayAccess
public inline
function set( key: Int, col: UInt ): UInt {
this.image.setInt32( Std.int( key*4 ), col );
return col;
}
public var length( get, never ): Int;
inline
function get_length(): Int {
return Std.int( this.width * this.height );
}
public var bytesLength( get, never ): Int;
inline
function get_bytesLength(): Int {
return this.image.length;
}
}
@:structInit
class ImageStruct {
public static var imageType: AbstractEnumImageType = 0;
public var width: Int;
public var height: Int;
public var image( default, default ):ImgDef; // Awkward
public function new<T:ImgDef>( width: Int, height: Int, image: T ){
this.width = width;
this.height = height;
this.image = image;
}
}
@:forward
@:transient
abstract Pixelimage( ImageStruct ) from ImageStruct to ImageStruct {
public function new( w: Int, h: Int ){
this = switch( ImageStruct.imageType ){
case UINT32ARRAY:
new ImageStruct( w, h
, create1( new UInt32Array(w*h) )
);
case BYTEIMAGE:
new ImageStruct( w, h
, create2( new ByteImage(w,h) )
);
}
}
static function create1( u:UInt32Array ):ImgDef {
return new ImgDef({set:u.set,get:u.get,length:u.length});
}
static function create2( b:ByteImage ):ImgDef {
return new ImgDef({set:b.set,get:b.get,length:b.length});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment