### ----------------------------------------------------
### -- GD::Raw
### -- Licenses: Artistic-2.0
### -- Authors: Dagur Valberg Johannsson, Raku Community
### -- File: lib/GD/Raw.rakumod
### ----------------------------------------------------
### -- Chunk 1 of 2
use NativeCall;

constant LIB = [ 'gd', v3 ];

constant gdAlphaMax is export = 127;
constant gdAlphaOpaque is export = 0;
constant gdAlphaTransparent is export = 127;
constant gdRedMax is export = 255;
constant gdGreenMax is export = 255;
constant gdBlueMax is export = 255;
sub gdTrueColorGetAlpha($c) is export {
    ((($c) +& 0x7F000000) +> 24)
}
sub gdTrueColorGetRed($c) is export {
    ((($c) +& 0xFF0000) +> 16)
}
sub gdTrueColorGetGreen($c) is export {
    ((($c) +& 0x00FF00) +> 8)
}
sub gdTrueColorGetBlue($c) is export {
    (($c) +& 0x0000FF)
}

enum gdInterpolationMethod (
	GD_DEFAULT          => 0,
	GD_BELL => 1,
	GD_BESSEL => 2,
	GD_BILINEAR_FIXED => 3,
	GD_BICUBIC => 4,
	GD_BICUBIC_FIXED => 5,
	GD_BLACKMAN => 6,
	GD_BOX => 7,
	GD_BSPLINE => 8,
	GD_CATMULLROM => 9,
	GD_GAUSSIAN => 10,
	GD_GENERALIZED_CUBIC => 11,
	GD_HERMITE => 12,
	GD_HAMMING => 13,
	GD_HANNING => 14,
	GD_MITCHELL => 15,
	GD_NEAREST_NEIGHBOUR => 16,
	GD_POWER => 17,
	GD_QUADRATIC => 18,
	GD_SINC => 19,
	GD_TRIANGLE => 20,
    GD_WEIGHTED4 => 21,
	GD_METHOD_COUNT => 21
);

constant gdEffectReplace is export = 0;
constant gdEffectAlphaBlend is export = 1;
constant gdEffectNormal is export = 2;
constant gdEffectOverlay is export = 3;

class gdImageStruct is repr('CStruct') is export {
    has OpaquePointer $.pixels;
    has int32 $.sx;
    has int32 $.sy;
	has int32 $.colorsTotal;

    # NativeCall doesn't support int red[gdMaxColors] yet.
    # Hack a placeholder.
    has int32 ($.red0, $.red1, $.red2, $.red3, $.red4, $.red5, $.red6, $.red7,
        $.red8, $.red9, $.red10, $.red11, $.red12, $.red13, $.red14, $.red15,
        $.red16, $.red17, $.red18, $.red19, $.red20, $.red21, $.red22, $.red23,
        $.red24, $.red25, $.red26, $.red27, $.red28, $.red29, $.red30, $.red31, $.red32, $.red33, $.red34, $.red35, $.red36, $.red37, $.red38, $.red39, $.red40, $.red41, $.red42, $.red43, $.red44, $.red45, $.red46, $.red47, $.red48, $.red49, $.red50, $.red51, $.red52, $.red53, $.red54, $.red55, $.red56, $.red57, $.red58, $.red59, $.red60, $.red61, $.red62, $.red63, $.red64, $.red65, $.red66, $.red67, $.red68, $.red69, $.red70, $.red71, $.red72, $.red73, $.red74, $.red75, $.red76, $.red77, $.red78, $.red79, $.red80, $.red81, $.red82, $.red83, $.red84, $.red85, $.red86, $.red87, $.red88, $.red89, $.red90, $.red91, $.red92, $.red93, $.red94, $.red95, $.red96, $.red97, $.red98, $.red99, $.red100, $.red101, $.red102, $.red103, $.red104, $.red105, $.red106, $.red107, $.red108, $.red109, $.red110, $.red111, $.red112, $.red113, $.red114, $.red115, $.red116, $.red117, $.red118, $.red119, $.red120, $.red121, $.red122, $.red123, $.red124, $.red125, $.red126, $.red127, $.red128, $.red129, $.red130, $.red131, $.red132, $.red133, $.red134, $.red135, $.red136, $.red137, $.red138, $.red139, $.red140, $.red141, $.red142, $.red143, $.red144, $.red145, $.red146, $.red147, $.red148, $.red149, $.red150, $.red151, $.red152, $.red153, $.red154, $.red155, $.red156, $.red157, $.red158, $.red159, $.red160, $.red161, $.red162, $.red163, $.red164, $.red165, $.red166, $.red167, $.red168, $.red169, $.red170, $.red171, $.red172, $.red173, $.red174, $.red175, $.red176, $.red177, $.red178, $.red179, $.red180, $.red181, $.red182, $.red183, $.red184, $.red185, $.red186, $.red187, $.red188, $.red189, $.red190, $.red191, $.red192, $.red193, $.red194, $.red195, $.red196, $.red197, $.red198, $.red199, $.red200, $.red201, $.red202, $.red203, $.red204, $.red205, $.red206, $.red207, $.red208, $.red209, $.red210, $.red211, $.red212, $.red213, $.red214, $.red215, $.red216, $.red217, $.red218, $.red219, $.red220, $.red221, $.red222, $.red223, $.red224, $.red225, $.red226, $.red227, $.red228, $.red229, $.red230, $.red231, $.red232, $.red233, $.red234, $.red235, $.red236, $.red237, $.red238, $.red239, $.red240, $.red241, $.red242, $.red243, $.red244, $.red245, $.red246, $.red247, $.red248, $.red249, $.red250, $.red251, $.red252, $.red253, $.red254, $.red255, );
    has int32 ($.green0, $.green1, $.green2, $.green3, $.green4, $.green5, $.green6, $.green7, $.green8, $.green9, $.green10, $.green11, $.green12, $.green13, $.green14, $.green15, $.green16, $.green17, $.green18, $.green19, $.green20, $.green21, $.green22, $.green23, $.green24, $.green25, $.green26, $.green27, $.green28, $.green29, $.green30, $.green31, $.green32, $.green33, $.green34, $.green35, $.green36, $.green37, $.green38, $.green39, $.green40, $.green41, $.green42, $.green43, $.green44, $.green45, $.green46, $.green47, $.green48, $.green49, $.green50, $.green51, $.green52, $.green53, $.green54, $.green55, $.green56, $.green57, $.green58, $.green59, $.green60, $.green61, $.green62, $.green63, $.green64, $.green65, $.green66, $.green67, $.green68, $.green69, $.green70, $.green71, $.green72, $.green73, $.green74, $.green75, $.green76, $.green77, $.green78, $.green79, $.green80, $.green81, $.green82, $.green83, $.green84, $.green85, $.green86, $.green87, $.green88, $.green89, $.green90, $.green91, $.green92, $.green93, $.green94, $.green95, $.green96, $.green97, $.green98, $.green99, $.green100, $.green101, $.green102, $.green103, $.green104, $.green105, $.green106, $.green107, $.green108, $.green109, $.green110, $.green111, $.green112, $.green113, $.green114, $.green115, $.green116, $.green117, $.green118, $.green119, $.green120, $.green121, $.green122, $.green123, $.green124, $.green125, $.green126, $.green127, $.green128, $.green129, $.green130, $.green131, $.green132, $.green133, $.green134, $.green135, $.green136, $.green137, $.green138, $.green139, $.green140, $.green141, $.green142, $.green143, $.green144, $.green145, $.green146, $.green147, $.green148, $.green149, $.green150, $.green151, $.green152, $.green153, $.green154, $.green155, $.green156, $.green157, $.green158, $.green159, $.green160, $.green161, $.green162, $.green163, $.green164, $.green165, $.green166, $.green167, $.green168, $.green169, $.green170, $.green171, $.green172, $.green173, $.green174, $.green175, $.green176, $.green177, $.green178, $.green179, $.green180, $.green181, $.green182, $.green183, $.green184, $.green185, $.green186, $.green187, $.green188, $.green189, $.green190, $.green191, $.green192, $.green193, $.green194, $.green195, $.green196, $.green197, $.green198, $.green199, $.green200, $.green201, $.green202, $.green203, $.green204, $.green205, $.green206, $.green207, $.green208, $.green209, $.green210, $.green211, $.green212, $.green213, $.green214, $.green215, $.green216, $.green217, $.green218, $.green219, $.green220, $.green221, $.green222, $.green223, $.green224, $.green225, $.green226, $.green227, $.green228, $.green229, $.green230, $.green231, $.green232, $.green233, $.green234, $.green235, $.green236, $.green237, $.green238, $.green239, $.green240, $.green241, $.green242, $.green243, $.green244, $.green245, $.green246, $.green247, $.green248, $.green249, $.green250, $.green251, $.green252, $.green253, $.green254, $.green255, );
has int32 ($.blue0, $.blue1, $.blue2, $.blue3, $.blue4, $.blue5, $.blue6, $.blue7, $.blue8, $.blue9, $.blue10, $.blue11, $.blue12, $.blue13, $.blue14, $.blue15, $.blue16, $.blue17, $.blue18, $.blue19, $.blue20, $.blue21, $.blue22, $.blue23, $.blue24, $.blue25, $.blue26, $.blue27, $.blue28, $.blue29, $.blue30, $.blue31, $.blue32, $.blue33, $.blue34, $.blue35, $.blue36, $.blue37, $.blue38, $.blue39, $.blue40, $.blue41, $.blue42, $.blue43, $.blue44, $.blue45, $.blue46, $.blue47, $.blue48, $.blue49, $.blue50, $.blue51, $.blue52, $.blue53, $.blue54, $.blue55, $.blue56, $.blue57, $.blue58, $.blue59, $.blue60, $.blue61, $.blue62, $.blue63, $.blue64, $.blue65, $.blue66, $.blue67, $.blue68, $.blue69, $.blue70, $.blue71, $.blue72, $.blue73, $.blue74, $.blue75, $.blue76, $.blue77, $.blue78, $.blue79, $.blue80, $.blue81, $.blue82, $.blue83, $.blue84, $.blue85, $.blue86, $.blue87, $.blue88, $.blue89, $.blue90, $.blue91, $.blue92, $.blue93, $.blue94, $.blue95, $.blue96, $.blue97, $.blue98, $.blue99, $.blue100, $.blue101, $.blue102, $.blue103, $.blue104, $.blue105, $.blue106, $.blue107, $.blue108, $.blue109, $.blue110, $.blue111, $.blue112, $.blue113, $.blue114, $.blue115, $.blue116, $.blue117, $.blue118, $.blue119, $.blue120, $.blue121, $.blue122, $.blue123, $.blue124, $.blue125, $.blue126, $.blue127, $.blue128, $.blue129, $.blue130, $.blue131, $.blue132, $.blue133, $.blue134, $.blue135, $.blue136, $.blue137, $.blue138, $.blue139, $.blue140, $.blue141, $.blue142, $.blue143, $.blue144, $.blue145, $.blue146, $.blue147, $.blue148, $.blue149, $.blue150, $.blue151, $.blue152, $.blue153, $.blue154, $.blue155, $.blue156, $.blue157, $.blue158, $.blue159, $.blue160, $.blue161, $.blue162, $.blue163, $.blue164, $.blue165, $.blue166, $.blue167, $.blue168, $.blue169, $.blue170, $.blue171, $.blue172, $.blue173, $.blue174, $.blue175, $.blue176, $.blue177, $.blue178, $.blue179, $.blue180, $.blue181, $.blue182, $.blue183, $.blue184, $.blue185, $.blue186, $.blue187, $.blue188, $.blue189, $.blue190, $.blue191, $.blue192, $.blue193, $.blue194, $.blue195, $.blue196, $.blue197, $.blue198, $.blue199, $.blue200, $.blue201, $.blue202, $.blue203, $.blue204, $.blue205, $.blue206, $.blue207, $.blue208, $.blue209, $.blue210, $.blue211, $.blue212, $.blue213, $.blue214, $.blue215, $.blue216, $.blue217, $.blue218, $.blue219, $.blue220, $.blue221, $.blue222, $.blue223, $.blue224, $.blue225, $.blue226, $.blue227, $.blue228, $.blue229, $.blue230, $.blue231, $.blue232, $.blue233, $.blue234, $.blue235, $.blue236, $.blue237, $.blue238, $.blue239, $.blue240, $.blue241, $.blue242, $.blue243, $.blue244, $.blue245, $.blue246, $.blue247, $.blue248, $.blue249, $.blue250, $.blue251, $.blue252, $.blue253, $.blue254, $.blue255, );

has int32 ($.open0, $.open1, $.open2, $.open3, $.open4, $.open5, $.open6, $.open7, $.open8, $.open9, $.open10, $.open11, $.open12, $.open13, $.open14, $.open15, $.open16, $.open17, $.open18, $.open19, $.open20, $.open21, $.open22, $.open23, $.open24, $.open25, $.open26, $.open27, $.open28, $.open29, $.open30, $.open31, $.open32, $.open33, $.open34, $.open35, $.open36, $.open37, $.open38, $.open39, $.open40, $.open41, $.open42, $.open43, $.open44, $.open45, $.open46, $.open47, $.open48, $.open49, $.open50, $.open51, $.open52, $.open53, $.open54, $.open55, $.open56, $.open57, $.open58, $.open59, $.open60, $.open61, $.open62, $.open63, $.open64, $.open65, $.open66, $.open67, $.open68, $.open69, $.open70, $.open71, $.open72, $.open73, $.open74, $.open75, $.open76, $.open77, $.open78, $.open79, $.open80, $.open81, $.open82, $.open83, $.open84, $.open85, $.open86, $.open87, $.open88, $.open89, $.open90, $.open91, $.open92, $.open93, $.open94, $.open95, $.open96, $.open97, $.open98, $.open99, $.open100, $.open101, $.open102, $.open103, $.open104, $.open105, $.open106, $.open107, $.open108, $.open109, $.open110, $.open111, $.open112, $.open113, $.open114, $.open115, $.open116, $.open117, $.open118, $.open119, $.open120, $.open121, $.open122, $.open123, $.open124, $.open125, $.open126, $.open127, $.open128, $.open129, $.open130, $.open131, $.open132, $.open133, $.open134, $.open135, $.open136, $.open137, $.open138, $.open139, $.open140, $.open141, $.open142, $.open143, $.open144, $.open145, $.open146, $.open147, $.open148, $.open149, $.open150, $.open151, $.open152, $.open153, $.open154, $.open155, $.open156, $.open157, $.open158, $.open159, $.open160, $.open161, $.open162, $.open163, $.open164, $.open165, $.open166, $.open167, $.open168, $.open169, $.open170, $.open171, $.open172, $.open173, $.open174, $.open175, $.open176, $.open177, $.open178, $.open179, $.open180, $.open181, $.open182, $.open183, $.open184, $.open185, $.open186, $.open187, $.open188, $.open189, $.open190, $.open191, $.open192, $.open193, $.open194, $.open195, $.open196, $.open197, $.open198, $.open199, $.open200, $.open201, $.open202, $.open203, $.open204, $.open205, $.open206, $.open207, $.open208, $.open209, $.open210, $.open211, $.open212, $.open213, $.open214, $.open215, $.open216, $.open217, $.open218, $.open219, $.open220, $.open221, $.open222, $.open223, $.open224, $.open225, $.open226, $.open227, $.open228, $.open229, $.open230, $.open231, $.open232, $.open233, $.open234, $.open235, $.open236, $.open237, $.open238, $.open239, $.open240, $.open241, $.open242, $.open243, $.open244, $.open245, $.open246, $.open247, $.open248, $.open249, $.open250, $.open251, $.open252, $.open253, $.open254, $.open255, );

    has int32 $.transparent;
	has OpaquePointer $.polyInts;
	has int32 $.polyAllocated;
	has gdImageStruct $.brush;
	has gdImageStruct $.tile;
has int32 ($.brushColorMap, $.bcm1, $.bcm2, $.bcm3, $.bcm4, $.bcm5, $.bcm6, $.bcm7, $.bcm8, $.bcm9, $.bcm10, $.bcm11, $.bcm12, $.bcm13, $.bcm14, $.bcm15, $.bcm16, $.bcm17, $.bcm18, $.bcm19, $.bcm20, $.bcm21, $.bcm22, $.bcm23, $.bcm24, $.bcm25, $.bcm26, $.bcm27, $.bcm28, $.bcm29, $.bcm30, $.bcm31, $.bcm32, $.bcm33, $.bcm34, $.bcm35, $.bcm36, $.bcm37, $.bcm38, $.bcm39, $.bcm40, $.bcm41, $.bcm42, $.bcm43, $.bcm44, $.bcm45, $.bcm46, $.bcm47, $.bcm48, $.bcm49, $.bcm50, $.bcm51, $.bcm52, $.bcm53, $.bcm54, $.bcm55, $.bcm56, $.bcm57, $.bcm58, $.bcm59, $.bcm60, $.bcm61, $.bcm62, $.bcm63, $.bcm64, $.bcm65, $.bcm66, $.bcm67, $.bcm68, $.bcm69, $.bcm70, $.bcm71, $.bcm72, $.bcm73, $.bcm74, $.bcm75, $.bcm76, $.bcm77, $.bcm78, $.bcm79, $.bcm80, $.bcm81, $.bcm82, $.bcm83, $.bcm84, $.bcm85, $.bcm86, $.bcm87, $.bcm88, $.bcm89, $.bcm90, $.bcm91, $.bcm92, $.bcm93, $.bcm94, $.bcm95, $.bcm96, $.bcm97, $.bcm98, $.bcm99, $.bcm100, $.bcm101, $.bcm102, $.bcm103, $.bcm104, $.bcm105, $.bcm106, $.bcm107, $.bcm108, $.bcm109, $.bcm110, $.bcm111, $.bcm112, $.bcm113, $.bcm114, $.bcm115, $.bcm116, $.bcm117, $.bcm118, $.bcm119, $.bcm120, $.bcm121, $.bcm122, $.bcm123, $.bcm124, $.bcm125, $.bcm126, $.bcm127, $.bcm128, $.bcm129, $.bcm130, $.bcm131, $.bcm132, $.bcm133, $.bcm134, $.bcm135, $.bcm136, $.bcm137, $.bcm138, $.bcm139, $.bcm140, $.bcm141, $.bcm142, $.bcm143, $.bcm144, $.bcm145, $.bcm146, $.bcm147, $.bcm148, $.bcm149, $.bcm150, $.bcm151, $.bcm152, $.bcm153, $.bcm154, $.bcm155, $.bcm156, $.bcm157, $.bcm158, $.bcm159, $.bcm160, $.bcm161, $.bcm162, $.bcm163, $.bcm164, $.bcm165, $.bcm166, $.bcm167, $.bcm168, $.bcm169, $.bcm170, $.bcm171, $.bcm172, $.bcm173, $.bcm174, $.bcm175, $.bcm176, $.bcm177, $.bcm178, $.bcm179, $.bcm180, $.bcm181, $.bcm182, $.bcm183, $.bcm184, $.bcm185, $.bcm186, $.bcm187, $.bcm188, $.bcm189, $.bcm190, $.bcm191, $.bcm192, $.bcm193, $.bcm194, $.bcm195, $.bcm196, $.bcm197, $.bcm198, $.bcm199, $.bcm200, $.bcm201, $.bcm202, $.bcm203, $.bcm204, $.bcm205, $.bcm206, $.bcm207, $.bcm208, $.bcm209, $.bcm210, $.bcm211, $.bcm212, $.bcm213, $.bcm214, $.bcm215, $.bcm216, $.bcm217, $.bcm218, $.bcm219, $.bcm220, $.bcm221, $.bcm222, $.bcm223, $.bcm224, $.bcm225, $.bcm226, $.bcm227, $.bcm228, $.bcm229, $.bcm230, $.bcm231, $.bcm232, $.bcm233, $.bcm234, $.bcm235, $.bcm236, $.bcm237, $.bcm238, $.bcm239, $.bcm240, $.bcm241, $.bcm242, $.bcm243, $.bcm244, $.bcm245, $.bcm246, $.bcm247, $.bcm248, $.bcm249, $.bcm250, $.bcm251, $.bcm252, $.bcm253, $.bcm254, $.bcm255, );
has int32 ($.tileColorMap, $.tcm1, $.tcm2, $.tcm3, $.tcm4, $.tcm5, $.tcm6, $.tcm7, $.tcm8, $.tcm9, $.tcm10, $.tcm11, $.tcm12, $.tcm13, $.tcm14, $.tcm15, $.tcm16, $.tcm17, $.tcm18, $.tcm19, $.tcm20, $.tcm21, $.tcm22, $.tcm23, $.tcm24, $.tcm25, $.tcm26, $.tcm27, $.tcm28, $.tcm29, $.tcm30, $.tcm31, $.tcm32, $.tcm33, $.tcm34, $.tcm35, $.tcm36, $.tcm37, $.tcm38, $.tcm39, $.tcm40, $.tcm41, $.tcm42, $.tcm43, $.tcm44, $.tcm45, $.tcm46, $.tcm47, $.tcm48, $.tcm49, $.tcm50, $.tcm51, $.tcm52, $.tcm53, $.tcm54, $.tcm55, $.tcm56, $.tcm57, $.tcm58, $.tcm59, $.tcm60, $.tcm61, $.tcm62, $.tcm6