Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 896060b50f76836efd23dbdd94a0e3749ceff1c1 @TheHippo committed Mar 25, 2010
Showing with 1,417 additions and 0 deletions.
  1. +50 −0 Test1.hx
  2. +2 −0 build.hxml
  3. +33 −0 hxGD/Color.hx
  4. +172 −0 hxGD/Graphics.hx
  5. +94 −0 hxGD/Image.hx
  6. +66 −0 hxGD/Loader.hx
  7. +41 −0 hxGD/Polygon.hx
  8. +33 −0 makefile
  9. +131 −0 src/nGD.c
  10. +46 −0 src/nGDCopy.c
  11. +21 −0 src/nGDCopy.h
  12. +190 −0 src/nGDDraw.c
  13. +49 −0 src/nGDDraw.h
  14. +142 −0 src/nGDFile.c
  15. +46 −0 src/nGDFile.h
  16. +219 −0 src/nGDImage.c
  17. +82 −0 src/nGDImage.h
@@ -0,0 +1,50 @@
+package;
+
+import hxGD.Image;
+import hxGD.Loader;
+import hxGD.Color;
+import hxGD.Polygon;
+import hxGD.Graphics;
+
+class Test1 {
+
+ public function new() {
+
+ var img:Image = Loader.createFromJpeg("test.jpg");
+
+ trace("width: "+img.width+" height: "+img.height);
+
+ //img.graphics.setBrush(brush);
+
+
+ img.graphics.color = Color.ofRGB(0xFF0000);
+
+ /* var ca:Array<Color> = new Array<Color>();
+ ca.push(new Color(255,0,0));
+ ca.push(new Color(255,0,0));
+ ca.push(new Color(0,255,0));
+ ca.push(new Color(0,255,0));
+ ca.push(new Color(0,0,255));
+ ca.push(new Color(0,0,255));
+
+ img.graphics.setStyle(ca); */
+ img.graphics.thickness = 5;
+ img.graphics.color = Color.ofRGBA(0xFF000055);
+
+ img.graphics.imageLine(0,0,img.width,img.height,false);
+ img.graphics.imageCircle(150,150,130);
+
+
+
+ img.saveImage("test2.png",FileType.Png);
+ }
+
+ public static function main() {
+ //for (i in 0...5000) {
+ new Test1();
+ // if (i % 10 == 0)
+ // neko.vm.Gc.run(true);
+ //}
+ }
+
+}
@@ -0,0 +1,2 @@
+-main Test1
+-neko test1.n
@@ -0,0 +1,33 @@
+package hxGD;
+
+class Color {
+
+ public var gd(gdColor,null):Int;
+ public var red:Int;
+ public var green:Int;
+ public var blue:Int;
+ public var alpha:Int;
+
+ public function new(red:Int,green:Int,blue:Int,?alpha:Int=255) {
+ this.red = red;
+ this.green = green;
+ this.blue = blue;
+ this.alpha = alpha;
+ // trace(this);
+ }
+
+ public static inline function ofRGB(color:Int):Color {
+ //0xRRGGBB
+ return new Color(((color & 0xFF0000) >> 16),((color & 0x00FF00) >> 8),(color & 0x0000FF));
+ }
+
+ public static inline function ofRGBA(color:Int):Color {
+ //0xRRGGBBAA
+ return new Color(((color & 0xFF000000) >> 24),((color & 0x00FF0000) >> 16) ,((color & 0x0000FF00) >> 8),(color & 0x000000FF));
+ }
+
+ private function gdColor():Int {
+ return ((((((red << 8) + green) << 8) + blue) << 8 ) + alpha);
+ }
+
+}
@@ -0,0 +1,172 @@
+
+package hxGD;
+
+import neko.Lib;
+
+enum ArcFillType {
+ ArcFilled;
+ ArcOutline;
+ ArcBorder;
+ ChordFilled;
+ ChordOutline;
+ ChordBorder;
+}
+
+class Graphics {
+
+ public static var defaultColor:Color = Color.ofRGB(0x000000);
+ public static var defaultThickness:Int = 1;
+ public static var defaultAntiAliasing:Bool = true;
+
+
+ private var img:Void;
+
+ public var color(getColor,setColor):Color;
+ private var _color:Color;
+
+ public var thickness(getThickness,setThickness):Int;
+ private var _thickness:Int;
+
+ public var antiAliasing(getAntiAliasing,setAntiAliasing):Bool;
+ private var _antiAliasing:Bool;
+
+
+ public function new(img:Void) {
+ this.img = img;
+ setColor(Graphics.defaultColor);
+ setAntiAliasing(Graphics.defaultAntiAliasing);
+ setThickness(Graphics.defaultThickness);
+ }
+
+ /* public function setBrush(?brushImage:Image=null) {
+ gd_setBrush(img,brushImage,(brushImage!=null));
+ }
+
+ public function setTile(?tileImage:Image=null) {
+ gd_setTile(img,tileImage,(tileImage!=null));
+ } */
+
+
+ public inline function setStyle(colors:Array<Color>):Void {
+ var ca:Array<Int> = new Array<Int>();
+ for (c in colors)
+ ca.push(c.gd);
+ gd_setStyle(img,Lib.haxeToNeko(ca));
+ }
+
+
+ private inline function getAntiAliasing():Bool {
+ return _antiAliasing;
+ }
+
+ private inline function setAntiAliasing(aa:Bool):Bool {
+ _antiAliasing = aa;
+ gd_setAntiAliasing(img,aa);
+ return _antiAliasing;
+ }
+
+ private inline function getThickness():Int {
+ return _thickness;
+ }
+
+ private inline function setThickness(thickn:Int):Int {
+ _thickness = gd_setThickness(img,(thickn!=0?thickn:1));
+ return _thickness;
+ }
+
+
+ private inline function setColor(c:Color):Color {
+ _color = c;
+ gd_setColor(img,_color.gd);
+ return color;
+ }
+
+ private inline function getColor():Color {
+ return _color;
+ }
+
+ public inline function setPixel(x:Int,y:Int):Void {
+ gd_setPixel(img,x,y);
+ }
+
+ public inline function imageLine(x1:Int,y1:Int,x2:Int,y2:Int,?dashed:Bool=false):Void {
+ if (dashed)
+ gd_imageDashedLine(img,x1,y1,x2,y2);
+ else
+ gd_imageLine(img,x1,y1,x2,y2);
+ }
+
+ public inline function imagePolygon(poly:Polygon,?open:Bool=false,?filled:Bool=false):Void {
+ if (open)
+ if (filled)
+ gd_imageFilledPolygon(img,poly.x,poly.y);
+ else
+ gd_imageOpenPolygon(img,poly.x,poly.y);
+ else
+ if (filled)
+ gd_imageFilledPolygon(img,poly.x,poly.y);
+ else
+ gd_imagePolygon(img,poly.x,poly.y);
+ }
+
+ public inline function imageRectangle(x:Int,y:Int,width:Int,height:Int,?filled:Bool=false):Void {
+ if (filled)
+ gd_imageFilledRectangle(img,x,y,width,height);
+ else
+ gd_imageRectangle(img,x,y,width,height);
+ }
+
+ public inline function imageEllipse(x:Int, y:Int, width:Int,height:Int,?filled:Bool=false):Void {
+ if (filled)
+ gd_imageFilledEllipse(img,x,y,width,height);
+ else
+ gd_imageArc(img,x,y,width,height,0,360);
+ }
+
+ public inline function imageCircle(x:Int, y:Int, radius:Int,?filled:Bool=false):Void {
+ if (filled)
+ gd_imageFilledArc(img,x,y,radius*2,radius*2,0,360,0);
+ else
+ gd_imageArc(img,x,y,radius*2,radius*2,0,360);
+ }
+
+ public inline function imageArc(x:Int,y:Int, width:Int,height:Int, start:Int, end:Int, fillType:ArcFillType):Void {
+ switch (fillType) {
+ case ArcFilled: gd_imageFilledArc(img,x,y,width,height,start,end,0);
+ case ArcOutline: gd_imageFilledArc(img,x,y,width,height,start,end,2);
+ case ArcBorder: gd_imageFilledArc(img,x,y,width,height,start,end,6);
+ case ChordFilled: gd_imageFilledArc(img,x,y,width,height,start,end,1);
+ case ChordOutline: gd_imageFilledArc(img,x,y,width,height,start,end,3);
+ case ChordBorder: gd_imageFilledArc(img,x,y,width,height,start,end,7);
+ }
+ }
+
+ public inline function imageFillToBorder(x:Int,y:Int, borderColor:Color):Void {
+ gd_imageFillToBorder(img,x,y,borderColor.gd);
+ }
+
+ public inline function imageFill(x:Int, y:Int) {
+ gd_imageFill(img,x,y);
+ }
+
+ private static var gd_setAntiAliasing = Lib.load("nGD","SetAntiAliasing",2);
+ private static var gd_setThickness = Lib.load("nGD","SetThickness",2);
+ private static var gd_setColor = Lib.load("nGD","SetColor",2);
+ private static var gd_setPixel = Lib.load("nGD","SetPixel",3);
+ private static var gd_imageLine = Lib.load("nGD","ImageLine",5);
+ private static var gd_imageDashedLine = Lib.load("nGD","ImageDashedLine",5);
+ private static var gd_imagePolygon = Lib.load("nGD","ImagePolygon",3);
+ private static var gd_imageFilledPolygon = Lib.load("nGD","ImageFilledPolygon",3);
+ private static var gd_imageOpenPolygon = Lib.load("nGD","ImageOpenPolygon",3);
+ private static var gd_imageRectangle = Lib.load("nGD","ImageRectangle",5);
+ private static var gd_imageFilledRectangle = Lib.load("nGD","ImageFilledRectangle",5);
+ private static var gd_imageFilledEllipse = Lib.load("nGD","ImageFilledEllipse",5);
+ private static var gd_imageArc = Lib.load("nGD","ImageArc",-1);
+ private static var gd_imageFilledArc = Lib.load("nGD","ImageFilledArc",-1);
+ private static var gd_imageFillToBorder = Lib.load("nGD","ImageFillToBorder",4);
+ private static var gd_imageFill = Lib.load("nGD","ImageFill",3);
+ //private static var gd_setBrush = Lib.load("nGD","SetBrush",3);
+ //private static var gd_setTile = Lib.load("nGD","SetTile",3);
+ private static var gd_setStyle = Lib.load("nGD","SetStyle",2);
+
+}
@@ -0,0 +1,94 @@
+package hxGD;
+
+import neko.Lib;
+
+
+enum FileType {
+ Png;
+ Jpeg (quality:Int);
+ Gif;
+ Gd;
+ Gd2 (compressed:Bool, chunkSize:Int);
+}
+
+
+class Image {
+
+ public static var defaultBackgroundColor:Color = Color.ofRGB(0xFFFFFF);
+
+ public var img:Void;
+
+ public var trueColor(isTrueColor,null):Bool;
+ public var width(getWidth,null):Int;
+ public var height(getHeight,null):Int;
+
+ public var graphics:Graphics;
+
+ public function new (width:Int,height:Int,?color:Color,trueColor:Bool=true) {
+ if (img!=null)
+ gd_freeImage(img);
+ if (!trueColor) {
+ img = gd_imageCreate(width,height,(color==null?Image.defaultBackgroundColor.gd:color.gd));
+ //gd_setColor(img,(color==null?Image.defaultBackgroundColor.gd:color.gd));
+ //gd_imageFill(0,0);
+ graphics = new Graphics(img);
+ }
+ else {
+ img = gd_imageCreateTrueColor(width,height,(color==null?Image.defaultBackgroundColor.gd:color.gd));
+ //gd_setColor(img,(color==null?Image.defaultBackgroundColor.gd:color.gd));
+ graphics = new Graphics(img);
+ }
+ //trace("new image "+trueColor);
+ }
+
+ public inline function saveImage(filename:String,type:FileType):Void {
+ var file:Dynamic = Lib.haxeToNeko(filename);
+ switch (type) {
+ case Png: gd_imagePng(img,file);
+ case Jpeg(q): gd_imageJpeg(img,file,q);
+ case Gif: gd_imageGif(img,file);
+ case Gd: gd_imageGd(img,file);
+ case Gd2(comp,ch): gd_imageGd2(img,file,comp,ch);
+ }
+ }
+
+ public inline function make256Colors(?dithering:Bool=true,?anzColors:Int=255):Void {
+ gd_makeImageToPalette(img,dithering,(Math.abs(anzColors)<256?Math.abs(anzColors):255));
+ }
+
+ public inline function cloneTo256Colors(?dithering:Bool,?anzColors:Int=255):Image {
+ var ret:Image = Type.createEmptyInstance(Image);
+ ret.img = gd_cloneImageToPalette(img,dithering,(Math.abs(anzColors)<256?Math.abs(anzColors):255));
+ ret.graphics = new Graphics(ret.img);
+ return ret;
+ }
+
+ private function isTrueColor():Bool {
+ return gd_imageTrueColor(img);
+ }
+
+ private inline function getWidth():Int {
+ return gd_imageX(img);
+ }
+
+ private inline function getHeight():Int {
+ return gd_imageY(img);
+ }
+
+ private static var gd_imageFill = Lib.load("nGD","ImageFill",3);
+ private static var gd_setColor = Lib.load("nGD","SetColor",2);
+ private static var gd_freeImage = Lib.load("nGD","FreeImage",1);
+ private static var gd_imageCreate = Lib.load("nGD","ImageCreate",3);
+ private static var gd_imageCreateTrueColor = Lib.load("nGD","ImageCreateTrueColor",3);
+ private static var gd_imageJpeg = Lib.load("nGD","ImageJpeg",3);
+ private static var gd_imageGif = Lib.load("nGD","ImageGif",2);
+ private static var gd_imagePng = Lib.load("nGD","ImagePng",2);
+ private static var gd_imageGd = Lib.load("nGD","ImageGd",2);
+ private static var gd_imageGd2 = Lib.load("nGD","ImageGd2",4);
+ private static var gd_makeImageToPalette = Lib.load("nGD","MakeImageToPalette",3);
+ private static var gd_cloneImageToPalette = Lib.load("nGD","CloneImageToPalette",3);
+ private static var gd_imageX = Lib.load("nGD","ImageX",1);
+ private static var gd_imageY = Lib.load("nGD","ImageY",1);
+ private static var gd_imageTrueColor = Lib.load("nGD","ImageTrueColor",1);
+
+}
Oops, something went wrong.

0 comments on commit 896060b

Please sign in to comment.