Browse files

added option to get raw image data, eg. for sending an image directly…

… to a browser
  • Loading branch information...
1 parent fd55ce5 commit aeddc6728bb99e62fb95b0b02cdccec409fe1415 @TheHippo committed May 1, 2010
Showing with 81 additions and 75 deletions.
  1. +6 −1 Test1.hx
  2. +0 −2 hxGD/Graphics.hx
  3. +29 −9 hxGD/Image.hx
  4. +6 −3 makefile
  5. +3 −2 src/nGD.c
  6. +31 −2 src/nGDFile.c
  7. +6 −0 src/nGDFile.h
  8. +0 −52 src/nGDImage.c
  9. +0 −4 src/nGDImage.h
View
7 Test1.hx
@@ -4,6 +4,11 @@ class Test1 {
hxGD.Image.defaultBackgroundColor = new hxGD.Color(255,255,255);
var i = new hxGD.Image(100, 100, new hxGD.Color(0,0,0), true);
i.graphics.line(0,0,100,100);
- i.saveImage("test.jpg",FileType.Jpeg(90));
+ if (neko.Web.isModNeko) {
+ neko.Web.setHeader("Content-Type","image/png");
+ neko.Lib.print(i.getImageData(FileType.Png));
+ }
+ else
+ i.saveImage("test.jpg",FileType.Jpeg(90));
}
}
View
2 hxGD/Graphics.hx
@@ -258,8 +258,6 @@ class Graphics {
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);
}
View
38 hxGD/Image.hx
@@ -21,16 +21,8 @@
package hxGD;
-import neko.Lib;
-
-enum FileType {
- Png;
- Jpeg (quality:Int);
- Gif;
- Gd;
- Gd2 (compressed:Bool, chunkSize:Int);
-}
+import neko.Lib;
enum StringAlign {
LeftTop;
@@ -44,6 +36,14 @@ enum StringAlign {
RightBottom;
}
+enum FileType {
+ Png;
+ Jpeg (quality:Int);
+ Gif;
+ Gd;
+ Gd2 (compressed:Bool, chunkSize:Int);
+}
+
class Image {
@@ -106,6 +106,23 @@ class Image {
}
/**
+ * returns plain image data, eg. for sending it to the browser
+ * @param type see FileTyoe
+ * @return String data
+ */
+ public inline function getImageData(type:FileType):String {
+ return Lib.nekoToHaxe(
+ switch (type) {
+ case Png: gd_imagePngData(img);
+ case Gif: gd_imageGifData(img);
+ case Jpeg(q): gd_imageJpegData(img,q);
+ default:
+ throw ("not supported");
+ }
+ );
+ }
+
+ /**
* converts a truecolor image to a palette based image
* @param ?dithering dithering?
* @param ?anzColors number of colors. max: 256
@@ -240,8 +257,11 @@ class Image {
private static var gd_freeImage = Lib.load("nGD","FreeImage",1);
private static var gd_imageCreate = Lib.load("nGD","ImageCreate",4);
private static var gd_imageJpeg = Lib.load("nGD","ImageJpeg",3);
+ private static var gd_imageJpegData = Lib.load("nGD","ImageJpegData",2);
private static var gd_imageGif = Lib.load("nGD","ImageGif",2);
+ private static var gd_imageGifData = Lib.load("nGD","ImageGifData",1);
private static var gd_imagePng = Lib.load("nGD","ImagePng",2);
+ private static var gd_imagePngData = Lib.load("nGD","ImagePngData",1);
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);
View
9 makefile
@@ -15,13 +15,16 @@ all: $(OBJ)
src/%.o: src/%.c
$(CC) $(CFLAGS) -c -o $@ $<
-.PHONY: clean haxe run
+.PHONY: clean haxe run install
clean:
rm -f src/*.o
-haxe:
+haxe: all
haxe build.hxml
-run:
+run: haxe
neko test1.n
+
+install: all
+ cp $(OUT) $(NEKOPATH)../
View
5 src/nGD.c
@@ -110,8 +110,11 @@ DEFINE_PRIM(ImageCreateFromGd2,1);
DEFINE_PRIM(ImageCreateFromGd2Part,5);
DEFINE_PRIM(ImageCreateFromWBMP,1);
DEFINE_PRIM(ImageJpeg,3);
+DEFINE_PRIM(ImageJpegData,2);
DEFINE_PRIM(ImageGif,2);
+DEFINE_PRIM(ImageGifData,1);
DEFINE_PRIM(ImagePng,2);
+DEFINE_PRIM(ImagePngData,1);
DEFINE_PRIM(ImageGd,2);
DEFINE_PRIM(ImageGd2,4);
DEFINE_PRIM(MakeImageToPalette,3);
@@ -129,8 +132,6 @@ DEFINE_PRIM_MULT(ImageFilledArc);
DEFINE_PRIM(ImageFilledEllipse,5);
DEFINE_PRIM(ImageFillToBorder,4);
DEFINE_PRIM(ImageFill,3);
-//DEFINE_PRIM(SetBrush,3);
-//DEFINE_PRIM(SetTile,3);
DEFINE_PRIM(SetStyle,2);
DEFINE_PRIM(CloneImage,1);
DEFINE_PRIM(GetImagePart,5);
View
33 src/nGDFile.c
@@ -18,8 +18,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
-
-
#include <neko.h>
#include <gd.h>
#include "nGDImage.h"
@@ -106,6 +104,17 @@ value ImageJpeg(value img,value filename,value quality) {
return val_null;
}
+value ImageJpegData(value img, value quality) {
+ ImageData _img = getImage(img);
+ int _quality = val_int(quality);
+ int size;
+ void *ptr = gdImageJpegPtr(imageImage(_img),&size,_quality);
+ buffer ret = alloc_buffer(NULL);
+ buffer_append_sub(ret,ptr,size);
+ gdFree(ptr);
+ return buffer_to_string(ret);
+}
+
value ImageGif(value img,value filename) {
char *_filename = val_string(filename);
ImageData _img = getImage(img);
@@ -115,6 +124,16 @@ value ImageGif(value img,value filename) {
return val_null;
}
+value ImageGifData(value img) {
+ ImageData _img = getImage(img);
+ int size;
+ void *ptr = gdImageGifPtr(imageImage(_img),&size);
+ buffer ret = alloc_buffer(NULL);
+ buffer_append_sub(ret,ptr,size);
+ gdFree(ptr);
+ return buffer_to_string(ret);
+}
+
value ImagePng(value img,value filename) {
char *_filename = val_string(filename);
ImageData _img = getImage(img);
@@ -124,6 +143,16 @@ value ImagePng(value img,value filename) {
return val_null;
}
+value ImagePngData(value img) {
+ ImageData _img = getImage(img);
+ int size;
+ void *ptr = gdImagePngPtr(imageImage(_img),&size);
+ buffer ret = alloc_buffer(NULL);
+ buffer_append_sub(ret,ptr,size);
+ gdFree(ptr);
+ return buffer_to_string(ret);
+}
+
value ImageGd (value img,value filename) {
char *_filename = val_string(filename);
ImageData _img = getImage(img);
View
6 src/nGDFile.h
@@ -37,10 +37,16 @@ value ImageCreateFromWBMP(value filename);
value ImageJpeg(value img,value filename,value quality);
+value ImageJpegData(value img,value quality);
+
value ImageGif(value img,value filename);
+value ImageGifData(value img);
+
value ImagePng(value img,value filename);
+value ImagePngData(value img);
+
value ImageGd (value img,value filename);
value ImageGd2 (value img,value filename,value compressed,value chunksize);
View
52 src/nGDImage.c
@@ -39,7 +39,6 @@ struct _ImageData {
int color;
int thickness;
gdImagePtr img;
- //gdImagePtr brush;
};
typedef struct _ImageData *ImageData;
@@ -72,56 +71,9 @@ ImageData getImage(value img) {
return val_data(img);
}
-/*
- *
- * Brushing & Tiles seem to does not work with many gd versions
- *
- *
-void setBrushImage(ImageData in,ImageData cl) {
- gdImagePtr cld;
- if (gdImageTrueColor(imageImage(in))) {
- // printf("clone true color\n");
- cld = gdImageCreateTrueColor(gdImageSX(imageImage(in)),gdImageSY(imageImage(in)));
- }
- else {
-// printf("clone palette\n");
- cld = gdImageCreate(gdImageSX(imageImage(in)),gdImageSY(imageImage(in)));
- }
-// printf("made copy\n");
- gdImageCopy(cld,imageImage(in),0,0,0,0,gdImageSX(imageImage(in)),gdImageSY(imageImage(in)));
- imageBrush(in)=cld;
-}
-
-value SetBrush(value img,value brush, value flag) {
- ImageData _img = getImage(img);
- if (val_bool(flag)==1) {
- imageSetTiled(_img,0);
- imageSetBrushed(_img,1);
- ImageData _brush = getImage(brush);
- //printf("before clone\n");
- setBrushImage(_img,_brush);
- //(imageImage(_brush));
- //printf("after clone\n");
- }
- return val_null;
-}
-
-value SetTile(value img, value tile, value flag) {
- ImageData _img = getImage(img);
- if (val_bool(flag)==1) {
- imageSetBrushed(_img,0);
- imageSetTiled(_img,1);
- ImageData _tile = getImage(tile);
- setBrushImage(_img,_tile);
- }
- return val_null;
-} */
-
value FreeImage (value v) {
ImageData img = getImage(v);
gdImageDestroy(imageImage(img));
-// if (imageBrush(img)!=NULL)
-// gdImageDestroy(imageBrush(img));
return val_null;
}
@@ -136,17 +88,13 @@ value alloc_gc_image(gdImagePtr image) {
imageImage(abstr) = image;
imageConfig(abstr) = 0;
imageThickness(abstr) = 0;
- //imageBrush(abstr) = NULL;
value ret = alloc_abstract(ImagePtr,abstr);
val_gc(ret,finalize);
return ret;
}
// setting current drawing color
int initColor(ImageData img,value color) {
-
- //imageSetBrushed(img,0);
- //imageSetTiled(img,0);
imageSetStyled(img,0);
// 0xRRGGBBAA = color
View
4 src/nGDImage.h
@@ -59,10 +59,6 @@ int initColor(ImageData img,value color);
value SetStyle(value img,value colors);
-//value SetBrush(value img,value brush, value flag);
-
-//value SetTile(value img, value tile, value flag);
-
value SetAntiAliasing (value img, value antiAlias);
value SetThickness(value img, value thickness);

0 comments on commit aeddc67

Please sign in to comment.