diff --git a/Examples/GIFPlot/Interface/gifplot.i b/Examples/GIFPlot/Interface/gifplot.i index f64162385..b8f17f657 100644 --- a/Examples/GIFPlot/Interface/gifplot.i +++ b/Examples/GIFPlot/Interface/gifplot.i @@ -30,7 +30,11 @@ typedef struct ColorMap { %addmethods { ColorMap(char *filename); ~ColorMap(); +#ifdef SWIGJAVA + %name(make_default) void default(); +#else void default(); +#endif void assign(int index,int r, int g, int b); %name(__getitem__) int getitem(int index); %name(__setitem__) void setitem(int index, int value); @@ -40,6 +44,16 @@ typedef struct ColorMap { /* Some default colors */ +#ifdef SWIGJAVA +const Pixel BLACK = 0; +const Pixel WHITE = 1; +const Pixel RED = 2; +const Pixel GREEN = 3; +const Pixel BLUE = 4; +const Pixel YELLOW = 5; +const Pixel CYAN = 6; +const Pixel MAGENTA = 7; +#else #define BLACK 0 #define WHITE 1 #define RED 2 @@ -48,6 +62,7 @@ typedef struct ColorMap { #define YELLOW 5 #define CYAN 6 #define MAGENTA 7 +#endif /*------------------------------------------------------------------------- FrameBuffer @@ -245,9 +260,11 @@ typedef struct Plot3D { } } Plot3D; +#ifndef SWIGJAVA const PixMap *SQUARE = &PixMap_SQUARE; const PixMap *TRIANGLE = &PixMap_TRIANGLE; const PixMap *CROSS = &PixMap_CROSS; +#endif %enabledoc diff --git a/Examples/GIFPlot/Java/Makefile b/Examples/GIFPlot/Java/Makefile new file mode 100644 index 000000000..f9d368963 --- /dev/null +++ b/Examples/GIFPlot/Java/Makefile @@ -0,0 +1,17 @@ +TOP = ../.. +SWIG = $(TOP)/../swig -shadow +SWIGOPT = -I../Include +SRCS = +TARGET = libjgifplot +INTERFACE = gifplot.i +LIBS = -L.. -lgifplot -lm +INCLUDE = -I../Include + +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + INCLUDE='$(INCLUDE)' LIBS='$(LIBS)' SWIGOPT='$(SWIGOPT)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java + +clean:: + rm -f *_wrap* *.o *~ *.so .~* core *.gif *.class ColorMap.java FrameBuffer.java Plot2D.java Plot3D.java gifplot.java + diff --git a/Examples/GIFPlot/Java/README b/Examples/GIFPlot/Java/README new file mode 100644 index 000000000..30058e52b --- /dev/null +++ b/Examples/GIFPlot/Java/README @@ -0,0 +1,30 @@ +The gifplot example does not work straight out of the box, +I had to change ../Interface/gifplot.i slightly for java. + +a) +The colors (e.g. BLACK) where defined as: + + #define BLACK 0 + +and the functions expect 'Pixel color' where Pixel is a unsigned char. +#define constants contain no type information and are translated to integer +constants. Because of that, you have to cast every Pixel to a byte in java. + +Changing the definition to: + const Pixel BLACK = 0; +fixes this. + +b) +The definitions: + +const PixMap *SQUARE = &PixMap_SQUARE; +const PixMap *TRIANGLE = &PixMap_TRIANGLE; +const PixMap *CROSS = &PixMap_CROSS; + +don't work. +The wrapper code expects actual variables SQUARE, etc. and they are not +defined in gifplot.h. + +c) +In shadow mode the method ColorMap::default() clashes with the reserved name +default. diff --git a/Examples/GIFPlot/Java/cm15 b/Examples/GIFPlot/Java/cm15 new file mode 100644 index 000000000..7a8cd99b5 Binary files /dev/null and b/Examples/GIFPlot/Java/cm15 differ diff --git a/Examples/GIFPlot/Java/cmap b/Examples/GIFPlot/Java/cmap new file mode 100644 index 000000000..a20c331a9 Binary files /dev/null and b/Examples/GIFPlot/Java/cmap differ diff --git a/Examples/GIFPlot/Java/gifplot.i b/Examples/GIFPlot/Java/gifplot.i new file mode 100644 index 000000000..b8f17f657 --- /dev/null +++ b/Examples/GIFPlot/Java/gifplot.i @@ -0,0 +1,273 @@ +// +// Graphics module +// +%module gifplot +%{ +#include "gifplot.h" +%} + +/* Pixel is 8-bits */ + +typedef unsigned char Pixel; +typedef float Zvalue; + +%disabledoc + +/* ------------------------------------------------------------------------ + ColorMap + + Definition and methods for colormaps + ------------------------------------------------------------------------ */ + +typedef struct ColorMap { + char *name; + +// +// %addmethods adds some C methods to this structure to make it +// look like a C++ class in Python. +// These are really named things like ColorMap_default, ColorMap_assign, etc... + + %addmethods { + ColorMap(char *filename); + ~ColorMap(); +#ifdef SWIGJAVA + %name(make_default) void default(); +#else + void default(); +#endif + void assign(int index,int r, int g, int b); + %name(__getitem__) int getitem(int index); + %name(__setitem__) void setitem(int index, int value); + int write(char *filename); + } +} ColorMap; + +/* Some default colors */ + +#ifdef SWIGJAVA +const Pixel BLACK = 0; +const Pixel WHITE = 1; +const Pixel RED = 2; +const Pixel GREEN = 3; +const Pixel BLUE = 4; +const Pixel YELLOW = 5; +const Pixel CYAN = 6; +const Pixel MAGENTA = 7; +#else +#define BLACK 0 +#define WHITE 1 +#define RED 2 +#define GREEN 3 +#define BLUE 4 +#define YELLOW 5 +#define CYAN 6 +#define MAGENTA 7 +#endif + +/*------------------------------------------------------------------------- + FrameBuffer + + This structure defines a simple 8 bit framebuffer. + ------------------------------------------------------------------------- */ + +typedef struct FrameBuffer { + unsigned int height; + unsigned int width; + int xmin; /* These are used for clipping */ + int ymin; + int xmax; + int ymax; + %addmethods { + FrameBuffer(unsigned int width, unsigned int height); + ~FrameBuffer(); + void resize(int width, int height); + void clear(Pixel color); + void plot(int x, int y, Pixel color); + void horizontal(int xmin, int xmax, int y, Pixel color); + void horizontalinterp(int xmin, int xmax, int y, Pixel c1, Pixel c2); + void vertical(int ymin, int ymax, int x, Pixel color); + void box(int x1, int y1, int x2, int y2, Pixel color); + void solidbox(int x1, int y1, int x2, int y2, Pixel color); + void interpbox(int x1, int y1, int x2, int y2, Pixel c1, Pixel c2, Pixel c3, Pixel c4); + void circle(int x1, int y1, int radius, Pixel color); + void solidcircle(int x1, int y1, int radius, Pixel color); + void line(int x1, int y1, int x2, int y2, Pixel color); + void setclip(int xmin, int ymin, int xmax, int ymax); + void noclip(); + int makeGIF(ColorMap *cmap, void *buffer, unsigned int maxsize); + void zresize(int width, int height); + void zclear(); + void drawchar(int x, int y, int fgcolor, int bgcolor, char chr, int orientation); + void drawstring(int x, int y, int fgcolor, int bgcolor, char *text, int orientation); + void drawpixmap(PixMap *pm, int x, int y, int fgcolor, int bgcolor); + int writeGIF(ColorMap *cmap, char *filename); + } +} FrameBuffer; + +#define HORIZONTAL 1 +#define VERTICAL 2 + +/* -------------------------------------------------------------------------- + PixMap + + The equivalent of "bit-maps". + -------------------------------------------------------------------------- */ + +/* PIXMAP methods */ + +extern PixMap *new_PixMap(int width, int height, int centerx, int centery); +extern void delete_PixMap(PixMap *pm); +extern void PixMap_set(PixMap *pm, int x, int y, int pix); + +#define TRANSPARENT 0 +#define FOREGROUND 1 +#define BACKGROUND 2 + +/* -------------------------------------------------------------------------- + Plot2D + + Definition and methods for 2D plots. + --------------------------------------------------------------------------- */ + +typedef struct Plot2D { + FrameBuffer *frame; + int view_xmin; /* Minimum coordinates of view region */ + int view_ymin; + int view_xmax; /* Maximum coordinates of view region */ + int view_ymax; + double xmin; /* Minimum coordinates of plot region */ + double ymin; + double xmax; /* Maximum coordinates of plot region */ + double ymax; + int xscale; /* Type of scaling (LINEAR, LOG, etc..) */ + int yscale; + %addmethods { + Plot2D(FrameBuffer *frame,double xmin,double ymin, double xmax, double ymax); + ~Plot2D(); + Plot2D *copy(); + void clear(Pixel c); + void setview(int vxmin, int vymin, int vxmax, int vymax); + void setrange(double xmin, double ymin, double xmax, double ymax); + void setscale(int xscale, int yscale); + void plot(double x, double y, Pixel color); + void box(double x1, double y1, double x2, double y2, Pixel color); + void solidbox(double x1, double y1, double x2, double y2, Pixel color); + void interpbox(double x1, double y1, double x2, double y2, Pixel c1, Pixel c2, Pixel c3, Pixel c4); + + void circle(double x, double y, double radius, Pixel color); + void solidcircle(double x, double y, double radius, Pixel color); + void line(double x1, double y1, double x2, double y2, Pixel color); + void start(); + void drawpixmap(PixMap *pm, double x, double y, Pixel color, Pixel bgcolor); + void xaxis(double x, double y, double xtick, int ticklength, Pixel color); + void yaxis(double x, double y, double ytick, int ticklength, Pixel color); + void triangle(double x1, double y1, double x2, double y2, double x3, double y3, Pixel c); + + void solidtriangle(double x1, double y1, double x2, double y2, double x3, double y3, Pixel c); + + void interptriangle(double x1, double y1, Pixel c1, + double x2, double y2, Pixel c2, + double x3, double y3, Pixel c3); + + } +} Plot2D; + +#define LINEAR 10 +#define LOG 11 + +/* ------------------------------------------------------------------------------ + Plot3D + + Data Structure for 3-D plots + ------------------------------------------------------------------------------ */ + +typedef struct Plot3D { + FrameBuffer *frame; + int view_xmin; /* Viewing region */ + int view_ymin; + int view_xmax; + int view_ymax; + double xmin; /* Bounding box */ + double ymin; + double zmin; + double xmax; + double ymax; + double zmax; + double xcenter; /* Center point */ + double ycenter; + double zcenter; + double fovy; /* Field of view */ + double aspect; /* Aspect ratio */ + double znear; /* near "clipping" plane */ + double zfar; /* far "clipping" plane */ + double lookatz; /* Where is the z-lookat point */ + double xshift; /* Used for translation and stuff */ + double yshift; + %addmethods { + Plot3D(FrameBuffer *frame, double xmin, double ymin, double zmin, double xmax, double ymax, double zmax); + ~Plot3D(); + Plot3D *copy(); + void clear(Pixel bgcolor); + void perspective( double fovy, double znear, double zfar); + void lookat( double z); + void autoperspective( double fovy); + void ortho(double left, double right, double bottom, double top); + void autoortho(); + void rotx( double deg); + void roty( double deg); + void rotz( double deg); + void rotl( double deg); + void rotr( double deg); + void rotd( double deg); + void rotu( double deg); + void rotc( double deg); + void zoom( double percent); + void left( double percent); + void right( double percent); + void down( double percent); + void up( double percent); + void center( double cx, double cy); + void plot( double x, double y, double z, Pixel Color); + void setview( int vxmin, int vymin, int vxmax, int vymax); + void start(); + void line( double x1, double y1, double z1, + double x2, double y2, double z2, Pixel color); + void triangle( double x1, double y1, double z1, + double x2, double y2, double z2, + double x3, double y3, double z3, Pixel color); + void solidtriangle( double x1, double y1, double z1, + double x2, double y2, double z2, + double x3, double y3, double z3, Pixel color); + void interptriangle(double x1, double y1, double z1, Pixel c1, + double x2, double y2, double z2, Pixel c2, + double x3, double y3, double z3, Pixel c3); + void quad( double x1, double y1, double z1, + double x2, double y2, double z2, + double x3, double y3, double z3, + double x4, double y4, double z4, + Pixel color); + void solidquad( double x1, double y1, double z1, + double x2, double y2, double z2, + double x3, double y3, double z3, + double x4, double y4, double z4, + Pixel color); + void interpquad( double x1, double y1, double z1, Pixel c1, + double x2, double y2, double z2, Pixel c2, + double x3, double y3, double z3, Pixel c3, + double x4, double y4, double z4, Pixel c4); + void solidsphere( double x, double y, double z, double radius,Pixel c); + void outlinesphere( double x, double y, double z, double radius,Pixel c, Pixel bc); + } +} Plot3D; + +#ifndef SWIGJAVA +const PixMap *SQUARE = &PixMap_SQUARE; +const PixMap *TRIANGLE = &PixMap_TRIANGLE; +const PixMap *CROSS = &PixMap_CROSS; +#endif + +%enabledoc + + + + diff --git a/Examples/GIFPlot/Java/ortho.java b/Examples/GIFPlot/Java/ortho.java new file mode 100644 index 000000000..78d1d2cdd --- /dev/null +++ b/Examples/GIFPlot/Java/ortho.java @@ -0,0 +1,107 @@ +import gifplot; + +public class ortho { + + static { + System.loadLibrary("jgifplot"); + }; + + public static double func(double x, double y) { + double r; + double f; + r = Math.sqrt(x*x + y*y); + + f = (Math.sin(0.30*r*x)+Math.cos(0.30*r*y))/(1.0+r); + return f; + } + + public static void main(String argv[]) { + + FrameBuffer f; + Plot3D p3; + ColorMap cm; + + double x,y; + double dx,dy; + double z1,z2,z3,z4; + int c1,c2,c3,c4; + + /* Create a framebuffer */ + + f = new FrameBuffer(700,400); + + /* Load a colormap */ + + cm = new ColorMap("cm15"); + + /* Create a new 2D image */ + + f.clear(gifplot.BLACK); + p3 = new Plot3D(f,-6.3,-6.3,-1.5,6.3,6.3,1.5); + + /* Set viewing region in 2D plot */ + + p3.setview(50,50,650,350); + + /* Set how far away from the image we are */ + p3.lookat(20); + + /* Set the field of view for the perspective */ + + +// Plot3D_autoperspective(p3,40); + + p3.autoortho(); + + /* Now make a plot of a 3D function */ + + /* Make a frame */ + + f.noclip(); + f.box(49,49,650,350,gifplot.WHITE); + p3.start(); /* Always call this prior to making an image */ + p3.clear(gifplot.BLACK); + p3.rotu(60); + p3.rotz(40); + x = -6.3; + dx = 0.25; + while (x < 6.3) { + y = -6.3; + dy = 0.25; + while (y < 6.3) { + z1 = func(x,y); + z2 = func(x+dx,y); + z3 = func(x+dx,y+dy); + z4 = func(x,y+dy); + c1 = (int) ((z1 + 1.0)*120) + 16; + if (c1 < 16) c1 = 16; + if (c1 > 254) c1 = 254; + + c2 = (int) ((z2 + 1.0)*120) + 16; + if (c2 < 16) c2 = 16; + if (c2 > 254) c2 = 254; + + c3 = (int) ((z3 + 1.0)*120) + 16; + if (c3 < 16) c3 = 16; + if (c3 > 254) c3 = 254; + + c4 = (int) ((z4 + 1.0)*120) + 16; + if (c4 < 16) c4 = 16; + if (c4 > 254) c4= 254; + + p3.interpquad(x,y,z1,(byte) c1, + x+dx,y,z2,(byte) c2, + x+dx,y+dy,z3,(byte) c3, + x,y+dy,z4,(byte) c4); + y = y + dy; + } + x = x + dx; + } + + /* Make a GIF file */ + + f.writeGIF(cm,"plot.gif"); + + System.out.println("Image written to 'plot.gif'"); + } +} diff --git a/Examples/GIFPlot/Java/shadow.java b/Examples/GIFPlot/Java/shadow.java new file mode 100644 index 000000000..34e9bf6f9 --- /dev/null +++ b/Examples/GIFPlot/Java/shadow.java @@ -0,0 +1,22 @@ +import gifplot; + +public class shadow { + + static { + System.loadLibrary("jgifplot"); + } + + public static void main(String argv[]) { + FrameBuffer fb = new FrameBuffer(300, 300); + ColorMap cm = new ColorMap("cmap"); + + fb.clear(gifplot.BLACK); + fb.drawstring(50, 50, gifplot.WHITE, gifplot.BLACK, "Hello world", gifplot.VERTICAL); + fb.solidbox(200, 200, 220, 240, gifplot.BLUE); + fb.line(0, 290, 293, 50, gifplot.RED); + fb.circle(100, 100, 10, gifplot.YELLOW); + fb.writeGIF(cm, "plot.gif"); + + System.out.println("Image written to 'plot.gif'"); + } +} diff --git a/Examples/GIFPlot/Java/simple.java b/Examples/GIFPlot/Java/simple.java new file mode 100644 index 000000000..b5a243206 --- /dev/null +++ b/Examples/GIFPlot/Java/simple.java @@ -0,0 +1,22 @@ +import gifplot; + +public class simple { + + static { + System.loadLibrary("jgifplot"); + } + + public static void main(String argv[]) { + long f = gifplot.new_FrameBuffer(300, 300); + long c = gifplot.new_ColorMap("cmap"); + + gifplot.FrameBuffer_clear(f, gifplot.BLACK); + gifplot.FrameBuffer_drawstring(f, 50, 50, gifplot.WHITE, gifplot.BLACK, "Hello world", gifplot.HORIZONTAL); + gifplot.FrameBuffer_solidbox(f, 200, 200, 220, 240, gifplot.BLUE); + gifplot.FrameBuffer_line(f, 0, 290, 293, 50, gifplot.RED); + gifplot.FrameBuffer_circle(f, 100, 100, 10, gifplot.YELLOW); + gifplot.FrameBuffer_writeGIF(f, c, "plot.gif"); + + System.out.println("Image written to 'plot.gif'"); + } +}