File: Image.k

package info (click to toggle)
kaya 0.2.0-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,012 kB
  • ctags: 1,307
  • sloc: cpp: 6,691; haskell: 4,833; sh: 2,868; yacc: 768; makefile: 700; perl: 87
file content (288 lines) | stat: -rw-r--r-- 9,095 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
module Image;

import Prelude;
import IO;
import Binary;
import Webprog;

%include "image.h";
%imported "image";
%link "gd";

"Image data."
abstract data Image = Image(Ptr imptr,Int xsize, Int ysize);

"Web image data"
abstract data WebImage = WebImage(Binary img, String ctype, [(String,String)] headers);

"Drawing colour."
abstract data Colour = Col(Int col);

"Drawing font."
abstract data Font = Font(Ptr fontptr);

"Arc fill styles."
public data ArcStyle = Arc | Chord | Pie | NoFill | Edged;

type Color = Colour; // Euw

foreign "image.o" {
    Ptr do_createImage(Int x,Int y, Bool tc) = createImage;
    Void do_destroyImage(Ptr imptr) = destroyImage;
    Image do_createImageFromPNG(File f) = createImageFromPNG;
    Image do_createImageFromJPEG(File f) = createImageFromJPEG;
    Int imageColourAllocate(Ptr ptr, Int r, Int g, Int b, Int a) 
	= imageColourAllocate;

    Ptr fontGetSmall() = fontGetSmall;
    Ptr fontGetLarge() = fontGetLarge;
    Ptr fontGetMediumBold() = fontGetMediumBold;
    Ptr fontGetGiant() = fontGetGiant;
    Ptr fontGetTiny() = fontGetTiny;

    Void imageString(Ptr ptr, Ptr font, Int x, Int y, String str,
		     Int col, Bool up) = imageString;
    Void imageLine(Ptr ptr, Int x1, Int y1, Int x2, Int y2, Int col)
	= imageLine;
    Void imageDashedLine(Ptr ptr, Int x1, Int y1, Int x2, Int y2, Int col)
	= imageDashedLine;
    Void imageRectangle(Ptr ptr, Int x1, Int y1, Int x2, Int y2, 
			Int col, Bool filled) = imageRectangle;
    Void imageSetPixel(Ptr ptr, Int x, Int y, Int col) = imageSetPixel;
    Void imageDrawPoly(Ptr ptr,[(Int,Int)] points, Int col, Bool filled)
	= imageDrawPoly;

    Void imageArc(Ptr ptr, Int cx, Int cy, Int w,Int h, Int start, Int end,
		  Int col, [ArcStyle] style, Bool filled) = imageArc;
    Void imageCopy(Ptr dest, Ptr src, Int dstx, Int dsty, 
		   Int srcx, Int srcy, Int w, Int h) = imageCopy;

    Void imageCopyResized(Ptr dest, Ptr src, Int dstx, Int dsty,
			  Int srcx, Int srcy, Int destw, Int desth,
			  Int srcw, Int srch) = imageCopyResized;

    Void imageWritePNG(Ptr ptr, File f) = imageWritePNG;
    Void imageWriteJPEG(Ptr ptr, File f, Int qual) = imageWriteJPEG;
    Binary imageGetPNG(Ptr ptr) = imageGetPNG;
    Binary imageGetJPEG(Ptr ptr, Int qual) = imageGetJPEG;
}

/// Image creation

"Create an image with dimensions (x,y). Optionally it can be made true-colour."
public Image create(Int x, Int y, Bool truecolour=false) {
    imgdata = do_createImage(x,y,truecolour);
    return Image(imgdata,x,y);
}

"DEPRECATED synonym for create"
public Image createImage(Int x, Int y) = create(x,y);

"Destroy an image."
public Void destroy(Image img) {
    do_destroyImage(img.imptr);
}

public Int width(Image img) = img.xsize;
public Int height(Image img) = img.ysize;

"DEPRECATED synonym for destroy"
public Void destroyImage(Image img) = destroy(img);

public data ImageType = PNG | JPEG;

"Create an image from a file"
public Image create(String fname, ImageType ty) {
    f = open(fname,[Read,Binary]);
    case ty of {
	PNG -> img = do_createImageFromPNG(f);
      | JPEG -> img = do_createImageFromJPEG(f);
    }
    close(f);
    return img;
}

"DEPRECATED - use create instead"
public Image createImageFromPNG(String fname)
    = create(fname, PNG);

"DEPRECATED - use create instead"
public Image createImageFromJPEG(String fname)
    = create(fname, JPEG);

"Allocate a colour.
The colour is allocated for use within image <em>img</em>. <em>r,g,b</em>
are in the range 0..255. <em>a</em> is the alpha channel, and if
specified is in the range 0..127, where 127 is full transparency."
public Colour makeColour(Image img, Int r, Int g, Int b, Int a = 0) {
    if (r<0 || r>255 || g<0 || g>255 || b<0 || b>255 || a<0 || a>127) {
	throw(Exception("Colour value out of range",1));
    }
    return Col(imageColourAllocate(img.imptr, r, g, b, a));
}

/// Fonts and text

public Font small() = Font(fontGetSmall());
public Font large() = Font(fontGetLarge());
public Font mediumBold() = Font(fontGetMediumBold());
public Font giant() = Font(fontGetGiant());
public Font tiny() = Font(fontGetTiny());

"DEPRECATED"
public Font smallFont() = Font(fontGetSmall());
"DEPRECATED"
public Font largeFont() = Font(fontGetLarge());
"DEPRECATED"
public Font mediumBoldFont() = Font(fontGetMediumBold());
"DEPRECATED"
public Font giantFont() = Font(fontGetGiant());
"DEPRECATED"
public Font tinyFont() = Font(fontGetTiny());

"Draw a string.
Writes the text at the given location. If <em>up</em> is set, draws the
text bottom to top, rather than left to right."
public Void drawString(Image img, Font font, Int x, Int y,
		       String str, Colour col, Bool up = false) {
    imageString(img.imptr, font.fontptr, x,y,str,col.col, up);
}

/// Drawing

"Draw a line.
Draws on the image <em>img</em>, from <em>(x1,y1)</em> to <em>(x2,y2)</em>."
public Void line(Image img, Int x1, Int y1, Int x2, Int y2, Colour col) {
    imageLine(img.imptr, x1, y1, x2, y2, col.col);
}

"Draw a dashed line.
Draws on the image <em>img</em>, from <em>(x1,y1)</em> to <em>(x2,y2)</em>."
public Void dashedLine(Image img, Int x1, Int y1, 
			Int x2, Int y2, Colour col) {
    imageDashedLine(img.imptr, x1, y1, x2, y2, col.col);
}

"Draw a rectangle.
Draws on the image <em>img</em>, from cornert <em>(x1,y1)</em> to 
<em>(x2,y2)</em>."
public Void rectangle(Image img, Int x1, Int y1, 
		      Int x2, Int y2, Colour col,
		      Bool filled = false) {
    imageRectangle(img.imptr, x1, y1, x2, y2, col.col, filled);
}

"Draw a pixel.
Draws on the image <em>img</em>, at <em>(x,y)</em>."
public Void setPixel(Image img, Int x, Int y, Colour col) {
    imageSetPixel(img.imptr, x, y, col.col);
}

"Draw a polygon.
Points are given as a list of pairs."
public Void poly(Image img, [(Int,Int)] points, Colour col, 
		 Bool filled = false)
{
    imageDrawPoly(img.imptr,points,col.col,filled);
}

"Draw an arc of an ellipse.
Draws from angle <em>start</em> to <em>end</em>; angles are specified
in degrees. <em>style</em> is relevant only for filled arcs."
public Void arc(Image img, Int cx, Int cy, 
		Int w, Int h, Int start, Int end, Colour col,
		Bool filled = false,
		[ArcStyle] style = [Arc])
{
    imageArc(img.imptr,cx,cy,w,h,start,end,col.col,style,filled);
}

"Draw an ellipse."
public Void ellipse(Image img, Int cx, Int cy, 
		    Int w, Int h, Colour col,
		    Bool filled = false) 
{
    arc(img,cx,cy,w,h,0,360,col,filled,[Arc]);
}

/// Copying/resizing

"Copy a portion of an image."
public Void copy(Image dest, Image src, Int dstx, Int dsty, 
		 Int srcx, Int srcy, Int w, Int h)
{
    imageCopy(dest.imptr,src.imptr,dstx,dsty,srcx,srcy,w,h);
}

"Copy and resize a portion of an image."
public Void copyResized(Image dest, Image src, Int dstx, Int dsty, 
			Int srcx, Int srcy, Int destw, Int desth,
			Int srcw, Int srch)
{
    imageCopyResized(dest.imptr,src.imptr,dstx,dsty,srcx,srcy,
				  destw,desth,srcw,srch);
}

/// Output

"Output a PNG.
Outputs from the image <em>img</em>. If a file is not specified, outputs
the data to standard output (useful in a webapp which generates image
content. If used in a webapp, ensure that you have called <em>flush</em>
first so that all of the headers have been output."
public Void makePNG(Image img, String file = "") {
    if (file=="") {
	f = stdout;
    } else {
	f = open(file,[Write,Binary]);
    }
    imageWritePNG(img.imptr, f);
    if (file!="") { close(f); }
}

"Returns a Binary object containing the image data in PNG format."
public Binary getPNG(Image img) {
  return imageGetPNG(img.imptr);
}

"Output a JPEG.  
Outputs from the image <em>img</em>, with quality <em>qual</em>,
usually in the range 0-95 (or negative for default quality). If a file
is not specified, outputs the data to standard output (useful in a
webapp which generates image content. If used in a webapp, ensure that
you have called <em>flush</em> first so that all of the headers have
been output."
public Void makeJPEG(Image img, Int qual = -1, String file = "") {
    if (file=="") {
	f = stdout;
    } else {
	f = open(file,[Write,Binary]);
    }
    imageWriteJPEG(img.imptr, f, qual);
    if (file!="") { close(f); }
}

"Returns a Binary object containing the image data in JPEG format.
The quality value is used in the same way as in makeJPEG"
public Binary getJPEG(Image img, Int qual=-1) {
  return imageGetJPEG(img.imptr,qual);
}


"Returns a PNG image suitable for returning from webmain()"
public WebImage webPNG(Image img, [(String,String)] extraheaders) {
  return WebImage(getPNG(img),"image/png",extraheaders);
}

"Returns a JPEG image suitable for returning from webmain()"
public WebImage webJPEG(Image img, [(String,String)] extraheaders, Int qual=-1) {
  return WebImage(getJPEG(img,qual),"image/jpeg",extraheaders);
}

"Displays the image via HTTP. The displayPage functions are called
automatically by a webprog based on the return value of webmain() and
should only be called directly for debugging purposes."
public Void displayPage(WebImage image) {
  push(image.headers,("Content-type",image.ctype));
  displayPage(image.img,image.headers);
}