1 /*
2 
3  test_paint.c -- RasterLite2 Test Case
4 
5  Author: Alessandro Furieri <a.furieri@lqt.it>
6 
7  ------------------------------------------------------------------------------
8 
9  Version: MPL 1.1/GPL 2.0/LGPL 2.1
10 
11  The contents of this file are subject to the Mozilla Public License Version
12  1.1 (the "License"); you may not use this file except in compliance with
13  the License. You may obtain a copy of the License at
14  http://www.mozilla.org/MPL/
15 
16 Software distributed under the License is distributed on an "AS IS" basis,
17 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
18 for the specific language governing rights and limitations under the
19 License.
20 
21 The Original Code is the SpatiaLite library
22 
23 The Initial Developer of the Original Code is Alessandro Furieri
24 
25 Portions created by the Initial Developer are Copyright (C) 2013
26 the Initial Developer. All Rights Reserved.
27 
28 Contributor(s):
29 
30 Alternatively, the contents of this file may be used under the terms of
31 either the GNU General Public License Version 2 or later (the "GPL"), or
32 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33 in which case the provisions of the GPL or the LGPL are applicable instead
34 of those above. If you wish to allow use of your version of this file only
35 under the terms of either the GPL or the LGPL, and not to allow others to
36 use your version of this file under the terms of the MPL, indicate your
37 decision by deleting the provisions above and replace them with the notice
38 and other provisions required by the GPL or the LGPL. If you do not delete
39 the provisions above, a recipient may use your version of this file under
40 the terms of any one of the MPL, the GPL or the LGPL.
41 */
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 
46 #include "rasterlite2/rasterlite2.h"
47 #include "rasterlite2/rl2graphics.h"
48 
49 static int
do_paint_test(rl2GraphicsContextPtr ctx)50 do_paint_test (rl2GraphicsContextPtr ctx)
51 {
52     unsigned char *buffer;
53     int buf_size;
54     unsigned int width;
55     unsigned int height;
56     rl2GraphicsBitmapPtr bmp;
57     rl2GraphicsPatternPtr pattern;
58     rl2RasterPtr rst;
59     rl2SectionPtr img;
60     unsigned char *rgba;
61     unsigned char *p_rgba;
62     int row;
63     int col;
64     unsigned char red;
65     unsigned char green;
66     unsigned char blue;
67     unsigned char alpha;
68     rl2GraphicsFontPtr font;
69     double pre_x;
70     double pre_y;
71     double w;
72     double h;
73     double post_x;
74     double post_y;
75 
76 /* loading a sample Image (RGB) */
77     img = rl2_section_from_jpeg ("./jpeg1.jpg");
78     if (img == NULL)
79       {
80 	  fprintf (stderr, "Unable to read: %s\n", "./jpeg1.jpg");
81 	  return -10;
82       }
83     rst = rl2_get_section_raster (img);
84     if (rst == NULL)
85       {
86 	  fprintf (stderr, "\"%s\" invalid raster pointer\n", "./jpeg1.jpg");
87 	  return -11;
88       }
89     rl2_get_raster_size (rst, &width, &height);
90     if (width != 558)
91       {
92 	  fprintf (stderr, "\"%s\" unexpected raster width %d\n", "./jpeg1.jpg",
93 		   width);
94 	  return -12;
95       }
96     if (height != 543)
97       {
98 	  fprintf (stderr, "\"%s\" unexpected raster width %d\n", "./jpeg1.jpg",
99 		   height);
100 	  return -13;
101       }
102 
103 /* extracting RGBA data */
104     if (rl2_raster_data_to_RGBA (rst, &buffer, &buf_size) != RL2_OK)
105       {
106 	  fprintf (stderr, "Unable to get RGBA data: \"%s\"\n", "./jpeg1.jpg");
107 	  return -14;
108       }
109     rl2_destroy_section (img);
110 
111 /* creating a Graphics Bitmap */
112     bmp = rl2_graph_create_bitmap (buffer, width, height);
113     if (bmp == NULL)
114       {
115 	  fprintf (stderr, "Unable to create a Graphics Bitmap\n");
116 	  return -15;
117       }
118 
119 /* rendering the Bitmap */
120     if (!rl2_graph_draw_bitmap (ctx, bmp, 256, 256))
121       {
122 	  fprintf (stderr,
123 		   "Unable to render the Bitmap #1 into the Graphics backend\n");
124 	  return -16;
125       }
126     rl2_graph_destroy_bitmap (bmp);
127 
128 /* loading a sample Image (Grayscale) */
129     img = rl2_section_from_jpeg ("./jpeg2.jpg");
130     if (img == NULL)
131       {
132 	  fprintf (stderr, "Unable to read: %s\n", "./jpeg2.jpg");
133 	  return -17;
134       }
135     rst = rl2_get_section_raster (img);
136     if (rst == NULL)
137       {
138 	  fprintf (stderr, "\"%s\" invalid raster pointer\n", "./jpeg2.jpg");
139 	  return -18;
140       }
141     rl2_get_raster_size (rst, &width, &height);
142     if (width != 558)
143       {
144 	  fprintf (stderr, "\"%s\" unexpected raster width %d\n", "./jpeg2.jpg",
145 		   width);
146 	  return -19;
147       }
148     if (height != 543)
149       {
150 	  fprintf (stderr, "\"%s\" unexpected raster width %d\n", "./jpeg2.jpg",
151 		   height);
152 	  return -20;
153       }
154 
155 /* extracting RGBA data */
156     if (rl2_raster_data_to_RGBA (rst, &buffer, &buf_size) != RL2_OK)
157       {
158 	  fprintf (stderr, "Unable to get RGBA data: \"%s\"\n", "./jpeg2.jpg");
159 	  return -21;
160       }
161     rl2_destroy_section (img);
162 
163 /* creating a Graphics Bitmap */
164     bmp = rl2_graph_create_bitmap (buffer, width, height);
165     if (bmp == NULL)
166       {
167 	  fprintf (stderr, "Unable to create a Graphics Bitmap\n");
168 	  return -22;
169       }
170 
171 /* rendering the Bitmap */
172     if (!rl2_graph_draw_bitmap (ctx, bmp, 700, 600))
173       {
174 	  fprintf (stderr,
175 		   "Unable to render the Bitmap #2 into the Graphics backend\n");
176 	  return -23;
177       }
178     rl2_graph_destroy_bitmap (bmp);
179 
180 /* setting up a RED dotted pen */
181     if (!rl2_graph_set_pen (ctx, 255, 0, 0, 255, 8.0, RL2_PENSTYLE_DOT))
182       {
183 	  fprintf (stderr, "Unable to set a Red Dotted Pen\n");
184 	  return -24;
185       }
186 
187 /* setting up a Green solid semi-transparent Brush */
188     if (!rl2_graph_set_brush (ctx, 0, 255, 0, 128))
189       {
190 	  fprintf (stderr, "Unable to set a Green semi-transparent Brush\n");
191 	  return -25;
192       }
193 
194 /* drawing a rectangle */
195     if (!rl2_graph_draw_rectangle (ctx, 650, 500, 200, 200))
196       {
197 	  fprintf (stderr, "Unable to draw a rectangle\n");
198 	  return -26;
199       }
200 
201 /* drawing a rounded rectangle */
202     if (!rl2_graph_draw_rounded_rectangle (ctx, 650, 950, 300, 100, 30))
203       {
204 	  fprintf (stderr, "Unable to draw a rounded rectangle\n");
205 	  return -27;
206       }
207 
208 /* setting up a Black solid pen */
209     if (!rl2_graph_set_pen (ctx, 0, 0, 0, 255, 2.0, RL2_PENSTYLE_SOLID))
210       {
211 	  fprintf (stderr, "Unable to set a Black solid Pen\n");
212 	  return -28;
213       }
214 
215 /* setting up a Yellow/Blue linear gradient Brush */
216     if (!rl2_graph_set_linear_gradient_brush
217 	(ctx, 1024, 1500, 200, 100, 255, 255, 0, 255, 0, 0, 255, 255))
218       {
219 	  fprintf (stderr,
220 		   "Unable to set a Yellow/Blue linear gradient Brush\n");
221 	  return -29;
222       }
223 
224 /* drawing an Ellipse */
225     if (!rl2_graph_draw_ellipse (ctx, 1024, 1500, 200, 100))
226       {
227 	  fprintf (stderr, "Unable to draw an ellipse\n");
228 	  return -30;
229       }
230 
231 /* setting up a Yellow/Red linear gradient Brush */
232     if (!rl2_graph_set_linear_gradient_brush
233 	(ctx, 1500, 1500, 200, 200, 255, 255, 0, 255, 255, 0, 0, 255))
234       {
235 	  fprintf (stderr,
236 		   "Unable to set a Yellow/Red linear gradient Brush\n");
237 	  return -31;
238       }
239 
240 /* drawing a Circular Sector */
241     if (!rl2_graph_draw_circle_sector (ctx, 1500, 1500, 100, 270, 180))
242       {
243 	  fprintf (stderr, "Unable to draw a circular sector\n");
244 	  return -32;
245       }
246 
247 /* setting up a Red solid pen */
248     if (!rl2_graph_set_pen (ctx, 255, 0, 0, 255, 16.0, RL2_PENSTYLE_SOLID))
249       {
250 	  fprintf (stderr, "Unable to set a Red solid Pen\n");
251 	  return -33;
252       }
253 
254 /* creating an RGBA buffer */
255     rgba = malloc (64 * 64 * 4);
256     p_rgba = rgba;
257     for (row = 0; row < 64; row++)
258       {
259 	  for (col = 0; col < 64; col++)
260 	    {
261 		if (row >= 32)
262 		  {
263 		      if (col >= 32)
264 			{
265 			    red = 0;
266 			    green = 255;
267 			    blue = 0;
268 			    alpha = 128;
269 			}
270 		      else
271 			{
272 			    red = 0;
273 			    green = 255;
274 			    blue = 255;
275 			    alpha = 255;
276 			}
277 		  }
278 		else
279 		  {
280 		      if (col >= 32)
281 			{
282 			    red = 0;
283 			    green = 0;
284 			    blue = 0;
285 			    alpha = 0;
286 			}
287 		      else
288 			{
289 			    red = 255;
290 			    green = 255;
291 			    blue = 0;
292 			    alpha = 255;
293 			}
294 		  }
295 		*p_rgba++ = red;
296 		*p_rgba++ = green;
297 		*p_rgba++ = blue;
298 		*p_rgba++ = alpha;
299 	    }
300       }
301 
302 /* creating and setting up a pattern brush */
303     pattern = rl2_graph_create_pattern (rgba, 64, 64);
304     if (pattern == NULL)
305       {
306 	  fprintf (stderr, "Unable to create a Pattern Brush\n");
307 	  return -34;
308       }
309     if (!rl2_graph_set_pattern_brush (ctx, pattern))
310       {
311 	  fprintf (stderr, "Unable to set up a pattern brush\n");
312 	  return -35;
313       }
314     if (!rl2_graph_stroke_line (ctx, 300, 300, 600, 600))
315       {
316 	  fprintf (stderr, "Unable to stroke a line\n");
317 	  return -36;
318       }
319 
320 /* arbitrary path */
321     if (!rl2_graph_move_to_point (ctx, 50, 1400))
322       {
323 	  fprintf (stderr, "Unable to move to point\n");
324 	  return -37;
325       }
326     if (!rl2_graph_add_line_to_path (ctx, 250, 1250))
327       {
328 	  fprintf (stderr, "Unable to move to point #1\n");
329 	  return -38;
330       }
331     if (!rl2_graph_add_line_to_path (ctx, 500, 1400))
332       {
333 	  fprintf (stderr, "Unable to move to point #2\n");
334 	  return -39;
335       }
336     if (!rl2_graph_add_line_to_path (ctx, 750, 1250))
337       {
338 	  fprintf (stderr, "Unable to move to point #3\n");
339 	  return -40;
340       }
341     if (!rl2_graph_add_line_to_path (ctx, 1000, 1400))
342       {
343 	  fprintf (stderr, "Unable to move to point #4\n");
344 	  return -41;
345       }
346     if (!rl2_graph_add_line_to_path (ctx, 1000, 1500))
347       {
348 	  fprintf (stderr, "Unable to move to point #4\n");
349 	  return -42;
350       }
351     if (!rl2_graph_add_line_to_path (ctx, 50, 1500))
352       {
353 	  fprintf (stderr, "Unable to move to point #5\n");
354 	  return -43;
355       }
356     if (!rl2_graph_close_subpath (ctx))
357       {
358 	  fprintf (stderr, "Unable to close a sub-path\n");
359 	  return -44;
360       }
361     if (!rl2_graph_fill_path (ctx, 1))
362       {
363 	  fprintf (stderr, "Unable to fill a path\n");
364 	  return -45;
365       }
366     if (!rl2_graph_stroke_path (ctx, 0))
367       {
368 	  fprintf (stderr, "Unable to stroke a path\n");
369 	  return -46;
370       }
371 
372     rl2_graph_destroy_pattern (pattern);
373 
374 /* creating and setting up a Green bold italic font */
375     font =
376 	rl2_graph_create_font (32, RL2_FONTSTYLE_ITALIC, RL2_FONTWEIGHT_BOLD);
377     if (pattern == NULL)
378       {
379 	  fprintf (stderr, "Unable to create a Font\n");
380 	  return -47;
381       }
382     if (!rl2_graph_font_set_color (font, 0, 255, 0, 255))
383       {
384 	  fprintf (stderr, "Unable to set the font color\n");
385 	  return -48;
386       }
387     if (!rl2_graph_set_font (ctx, font))
388       {
389 	  fprintf (stderr, "Unable to set up a font\n");
390 	  return -49;
391       }
392     if (!rl2_graph_draw_text (ctx, "Armageddon", 1000, 100, 120))
393       {
394 	  fprintf (stderr, "Unable to print text #1\n");
395 	  return -50;
396       }
397     rl2_graph_destroy_font (font);
398 
399 /* creating and setting up a Black outlined font */
400     font =
401 	rl2_graph_create_font (32, RL2_FONTSTYLE_NORMAL, RL2_FONTWEIGHT_BOLD);
402     if (pattern == NULL)
403       {
404 	  fprintf (stderr, "Unable to create a Font #2\n");
405 	  return -51;
406       }
407     if (!rl2_graph_font_set_color (font, 0, 0, 0, 255))
408       {
409 	  fprintf (stderr, "Unable to set the font color #2\n");
410 	  return -52;
411       }
412     if (!rl2_graph_font_set_outline (font, 1.5))
413       {
414 	  fprintf (stderr, "Unable to set the font outline\n");
415 	  return -53;
416       }
417     if (!rl2_graph_set_font (ctx, font))
418       {
419 	  fprintf (stderr, "Unable to set up a font #2\n");
420 	  return -54;
421       }
422     if (!rl2_graph_draw_text (ctx, "Walhalla", 300, 400, 0))
423       {
424 	  fprintf (stderr, "Unable to print text #2\n");
425 	  return -55;
426       }
427     if (!rl2_graph_get_text_extent
428 	(ctx, "Walhalla", &pre_x, &pre_y, &w, &h, &post_x, &post_y))
429       {
430 	  fprintf (stderr, "Unable to measure text\n");
431 	  return -56;
432       }
433 
434     rl2_graph_destroy_font (font);
435 
436 
437     return 0;
438 }
439 
440 int
main(int argc,char * argv[])441 main (int argc, char *argv[])
442 {
443     rl2GraphicsContextPtr svg;
444     rl2GraphicsContextPtr pdf;
445     rl2GraphicsContextPtr ctx;
446     rl2RasterPtr rst;
447     rl2SectionPtr img;
448     int ret;
449     unsigned char *rgb;
450     unsigned char *alpha;
451 
452     if (argc > 1 || argv[0] == NULL)
453 	argc = 1;		/* silencing stupid compiler warnings */
454 
455 /* testing the SVG backend */
456     svg = rl2_graph_create_svg_context ("./test_paint.svg", 2048, 2048);
457     if (svg == NULL)
458       {
459 	  fprintf (stderr, "Unable to create an SVG backend\n");
460 	  return -1;
461       }
462     ret = do_paint_test (svg);
463     if (ret < 0)
464 	return ret;
465     rl2_graph_destroy_context (svg);
466     unlink ("./test_paint.svg");
467 
468 /* testing the PDF backend */
469     pdf =
470 	rl2_graph_create_pdf_context ("./test_paint.pdf", 300, 11.7, 8.3, 1.0,
471 				      1.0);
472     if (pdf == NULL)
473       {
474 	  fprintf (stderr, "Unable to create a PDF backend\n");
475 	  return -2;
476       }
477     ret = do_paint_test (pdf);
478     if (ret < 0)
479 	return ret;
480     rl2_graph_destroy_context (pdf);
481     unlink ("./test_paint.pdf");
482 
483 /* testing an ordinary graphics backend */
484     ctx = rl2_graph_create_context (2048, 2048);
485     if (ctx == NULL)
486       {
487 	  fprintf (stderr, "Unable to create an ordinary graphics backend\n");
488 	  return -3;
489       }
490     ret = do_paint_test (ctx);
491     if (ret < 0)
492 	return ret;
493     rgb = rl2_graph_get_context_rgb_array (ctx);
494     if (rgb == NULL)
495       {
496 	  fprintf (stderr, "invalid RGB buffer from Graphics Context\n");
497 	  return -4;
498       }
499     alpha = rl2_graph_get_context_alpha_array (ctx);
500     if (alpha == NULL)
501       {
502 	  fprintf (stderr, "invalid Alpha buffer from Graphics Context\n");
503 	  return -5;
504       }
505     free (alpha);
506     rl2_graph_destroy_context (ctx);
507 
508 /* exporting a PNG image */
509     rst = rl2_create_raster (2048, 2048, RL2_SAMPLE_UINT8, RL2_PIXEL_RGB, 3,
510 			     rgb, 2048 * 2048 * 3, NULL, NULL, 0, NULL);
511     if (rst == NULL)
512       {
513 	  fprintf (stderr, "Unable to create the output raster+mask\n");
514 	  return -6;
515       }
516     img =
517 	rl2_create_section ("beta", RL2_COMPRESSION_NONE,
518 			    RL2_TILESIZE_UNDEFINED, RL2_TILESIZE_UNDEFINED,
519 			    rst);
520     if (img == NULL)
521       {
522 	  fprintf (stderr, "Unable to create the output section+mask\n");
523 	  return -7;
524       }
525     if (rl2_section_to_png (img, "./test_paint.png") != RL2_OK)
526       {
527 	  fprintf (stderr, "Unable to write: test_paint.png\n");
528 	  return -8;
529       }
530     rl2_destroy_section (img);
531     unlink ("./test_paint.png");
532 
533     return 0;
534 }
535