Alexei Podtelezhnikov pushed to branch master at FreeType / FreeType Demo Programs
Commits:
-
33b27c1e
by Alexei Podtelezhnikov at 2021-02-23T22:26:24-05:00
4 changed files:
Changes:
1 |
+2021-02-18 Alexei Podtelezhnikov <apodtele@gmail.com>
|
|
2 |
+ |
|
3 |
+ Split out PNG writing routines.
|
|
4 |
+ |
|
5 |
+ * src/ftcommon.c (FTDemo_Display_Print): Move from here...
|
|
6 |
+ * src/ftpngout.c (FTDemo_Display_Print): ... to here.
|
|
7 |
+ * Makefile: Updated accordingly.
|
|
8 |
+ |
|
1 | 9 |
2021-02-18 Alexei Podtelezhnikov <apodtele@gmail.com>
|
2 | 10 |
|
3 | 11 |
* src/ftcommon.c (FTDemo_String_Draw): Control pen position better.
|
... | ... | @@ -342,12 +342,16 @@ else |
342 | 342 |
$(OBJ_DIR_2)/output.$(SO) \
|
343 | 343 |
$(OBJ_DIR_2)/mlgetopt.$(SO)
|
344 | 344 |
|
345 |
+ $(OBJ_DIR_2)/ftcommon.$(SO): $(SRC_DIR)/ftcommon.c $(SRC_DIR)/ftcommon.h
|
|
346 |
+ $(COMPILE) $(GRAPH_INCLUDES:%=$I%) \
|
|
347 |
+ $T$(subst /,$(COMPILER_SEP),$@ $<)
|
|
345 | 348 |
|
346 |
- FTCOMMON_OBJ := $(OBJ_DIR_2)/ftcommon.$(SO)
|
|
347 |
- $(FTCOMMON_OBJ): $(SRC_DIR)/ftcommon.c $(SRC_DIR)/ftcommon.h
|
|
349 |
+ $(OBJ_DIR_2)/ftpngout.$(SO): $(SRC_DIR)/ftpngout.c $(SRC_DIR)/ftcommon.h
|
|
348 | 350 |
$(COMPILE) $(GRAPH_INCLUDES:%=$I%) \
|
349 | 351 |
$T$(subst /,$(COMPILER_SEP),$@ $<)
|
350 | 352 |
|
353 |
+ FTCOMMON_OBJ := $(OBJ_DIR_2)/ftcommon.$(SO) \
|
|
354 |
+ $(OBJ_DIR_2)/ftpngout.$(SO)
|
|
351 | 355 |
|
352 | 356 |
$(OBJ_DIR_2)/ftlint.$(SO): $(SRC_DIR)/ftlint.c
|
353 | 357 |
$(COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $<)
|
... | ... | @@ -43,10 +43,6 @@ |
43 | 43 |
#include <string.h>
|
44 | 44 |
#include <stdarg.h>
|
45 | 45 |
|
46 |
-#ifdef FT_CONFIG_OPTION_USE_PNG
|
|
47 |
-#include <png.h>
|
|
48 |
-#endif
|
|
49 |
- |
|
50 | 46 |
|
51 | 47 |
#ifdef _WIN32
|
52 | 48 |
#define strcasecmp _stricmp
|
... | ... | @@ -233,141 +229,6 @@ |
233 | 229 |
}
|
234 | 230 |
|
235 | 231 |
|
236 |
- int
|
|
237 |
- FTDemo_Display_Print( FTDemo_Display* display,
|
|
238 |
- const char* filename,
|
|
239 |
- FT_String* ver_str )
|
|
240 |
- {
|
|
241 |
- |
|
242 |
-#ifdef FT_CONFIG_OPTION_USE_PNG
|
|
243 |
- |
|
244 |
- grBitmap* bit = display->bitmap;
|
|
245 |
- int width = bit->width;
|
|
246 |
- int height = bit->rows;
|
|
247 |
- int color_type;
|
|
248 |
- |
|
249 |
- int code = 1;
|
|
250 |
- FILE *fp = NULL;
|
|
251 |
- |
|
252 |
- png_structp png_ptr = NULL;
|
|
253 |
- png_infop info_ptr = NULL;
|
|
254 |
- png_bytep row = NULL;
|
|
255 |
- |
|
256 |
- |
|
257 |
- /* Set color_type */
|
|
258 |
- switch ( bit-> mode )
|
|
259 |
- {
|
|
260 |
- case gr_pixel_mode_gray:
|
|
261 |
- color_type = PNG_COLOR_TYPE_GRAY;
|
|
262 |
- break;
|
|
263 |
- case gr_pixel_mode_rgb24:
|
|
264 |
- case gr_pixel_mode_rgb32:
|
|
265 |
- color_type = PNG_COLOR_TYPE_RGB;
|
|
266 |
- break;
|
|
267 |
- default:
|
|
268 |
- fprintf( stderr, "Unsupported color type\n" );
|
|
269 |
- goto Exit0;
|
|
270 |
- }
|
|
271 |
- |
|
272 |
- /* Open file for writing (binary mode) */
|
|
273 |
- fp = fopen( filename, "wb" );
|
|
274 |
- if ( fp == NULL )
|
|
275 |
- {
|
|
276 |
- fprintf( stderr, "Could not open file %s for writing\n", filename );
|
|
277 |
- goto Exit0;
|
|
278 |
- }
|
|
279 |
- |
|
280 |
- /* Initialize write structure */
|
|
281 |
- png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
|
|
282 |
- if ( png_ptr == NULL )
|
|
283 |
- {
|
|
284 |
- fprintf( stderr, "Could not allocate write struct\n" );
|
|
285 |
- goto Exit1;
|
|
286 |
- }
|
|
287 |
- |
|
288 |
- /* Initialize info structure */
|
|
289 |
- info_ptr = png_create_info_struct( png_ptr );
|
|
290 |
- if ( info_ptr == NULL )
|
|
291 |
- {
|
|
292 |
- fprintf( stderr, "Could not allocate info struct\n" );
|
|
293 |
- goto Exit2;
|
|
294 |
- }
|
|
295 |
- |
|
296 |
- /* Set up exception handling */
|
|
297 |
- if ( setjmp( png_jmpbuf( png_ptr ) ) )
|
|
298 |
- {
|
|
299 |
- fprintf( stderr, "Error during png creation\n" );
|
|
300 |
- goto Exit2;
|
|
301 |
- }
|
|
302 |
- |
|
303 |
- png_init_io( png_ptr, fp );
|
|
304 |
- |
|
305 |
- /* Write header (8 bit colour depth) */
|
|
306 |
- png_set_IHDR( png_ptr, info_ptr, width, height,
|
|
307 |
- 8, color_type, PNG_INTERLACE_NONE,
|
|
308 |
- PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE );
|
|
309 |
- |
|
310 |
- /* Record version string */
|
|
311 |
- if ( ver_str != NULL )
|
|
312 |
- {
|
|
313 |
- png_text text;
|
|
314 |
- |
|
315 |
- |
|
316 |
- text.compression = PNG_TEXT_COMPRESSION_NONE;
|
|
317 |
- text.key = (char *)"Software";
|
|
318 |
- text.text = ver_str;
|
|
319 |
- |
|
320 |
- png_set_text( png_ptr, info_ptr, &text, 1 );
|
|
321 |
- }
|
|
322 |
- |
|
323 |
- /* Set gamma */
|
|
324 |
- png_set_gAMA( png_ptr, info_ptr, 1.0 / display->gamma );
|
|
325 |
- |
|
326 |
- png_write_info( png_ptr, info_ptr );
|
|
327 |
- |
|
328 |
- if ( bit->mode == gr_pixel_mode_rgb32 )
|
|
329 |
- {
|
|
330 |
- const int x = 1;
|
|
331 |
- |
|
332 |
- |
|
333 |
- if ( *(char*)&x ) /* little endian */
|
|
334 |
- {
|
|
335 |
- png_set_filler( png_ptr, 0, PNG_FILLER_AFTER );
|
|
336 |
- png_set_bgr( png_ptr );
|
|
337 |
- }
|
|
338 |
- else
|
|
339 |
- png_set_filler( png_ptr, 0, PNG_FILLER_BEFORE );
|
|
340 |
- }
|
|
341 |
- |
|
342 |
- /* Write image rows */
|
|
343 |
- row = bit->buffer;
|
|
344 |
- if ( bit->pitch < 0 )
|
|
345 |
- row -= ( bit->rows - 1 ) * bit->pitch;
|
|
346 |
- while ( height-- )
|
|
347 |
- {
|
|
348 |
- png_write_row( png_ptr, row );
|
|
349 |
- row += bit->pitch;
|
|
350 |
- }
|
|
351 |
- |
|
352 |
- /* End write */
|
|
353 |
- png_write_end( png_ptr, NULL );
|
|
354 |
- code = 0;
|
|
355 |
- |
|
356 |
- Exit2:
|
|
357 |
- png_destroy_write_struct( &png_ptr, &info_ptr );
|
|
358 |
- Exit1:
|
|
359 |
- fclose( fp );
|
|
360 |
- Exit0:
|
|
361 |
- return code;
|
|
362 |
- |
|
363 |
-#else
|
|
364 |
- |
|
365 |
- return 0;
|
|
366 |
- |
|
367 |
-#endif /* !FT_CONFIG_OPTION_USE_PNG */
|
|
368 |
- }
|
|
369 |
- |
|
370 |
- |
|
371 | 232 |
/*************************************************************************/
|
372 | 233 |
/*************************************************************************/
|
373 | 234 |
/***** *****/
|
1 |
+/****************************************************************************/
|
|
2 |
+/* */
|
|
3 |
+/* The FreeType project -- a free and portable quality TrueType renderer. */
|
|
4 |
+/* */
|
|
5 |
+/* Copyright (C) 2019-2021 by */
|
|
6 |
+/* D. Turner, R.Wilhelm, and W. Lemberg */
|
|
7 |
+/* */
|
|
8 |
+/* */
|
|
9 |
+/* ftpngout.c - PNG printing routines for FreeType demo programs. */
|
|
10 |
+/* */
|
|
11 |
+/****************************************************************************/
|
|
12 |
+ |
|
13 |
+#include "ftcommon.h"
|
|
14 |
+ |
|
15 |
+ |
|
16 |
+#ifdef FT_CONFIG_OPTION_USE_PNG
|
|
17 |
+ |
|
18 |
+#include <png.h>
|
|
19 |
+ |
|
20 |
+ int
|
|
21 |
+ FTDemo_Display_Print( FTDemo_Display* display,
|
|
22 |
+ const char* filename,
|
|
23 |
+ FT_String* ver_str )
|
|
24 |
+ {
|
|
25 |
+ grBitmap* bit = display->bitmap;
|
|
26 |
+ int width = bit->width;
|
|
27 |
+ int height = bit->rows;
|
|
28 |
+ int color_type;
|
|
29 |
+ |
|
30 |
+ int code = 1;
|
|
31 |
+ FILE *fp = NULL;
|
|
32 |
+ |
|
33 |
+ png_structp png_ptr = NULL;
|
|
34 |
+ png_infop info_ptr = NULL;
|
|
35 |
+ png_bytep row = NULL;
|
|
36 |
+ |
|
37 |
+ |
|
38 |
+ /* Set color_type */
|
|
39 |
+ switch ( bit-> mode )
|
|
40 |
+ {
|
|
41 |
+ case gr_pixel_mode_gray:
|
|
42 |
+ color_type = PNG_COLOR_TYPE_GRAY;
|
|
43 |
+ break;
|
|
44 |
+ case gr_pixel_mode_rgb24:
|
|
45 |
+ case gr_pixel_mode_rgb32:
|
|
46 |
+ color_type = PNG_COLOR_TYPE_RGB;
|
|
47 |
+ break;
|
|
48 |
+ default:
|
|
49 |
+ fprintf( stderr, "Unsupported color type\n" );
|
|
50 |
+ goto Exit0;
|
|
51 |
+ }
|
|
52 |
+ |
|
53 |
+ /* Open file for writing (binary mode) */
|
|
54 |
+ fp = fopen( filename, "wb" );
|
|
55 |
+ if ( fp == NULL )
|
|
56 |
+ {
|
|
57 |
+ fprintf( stderr, "Could not open file %s for writing\n", filename );
|
|
58 |
+ goto Exit0;
|
|
59 |
+ }
|
|
60 |
+ |
|
61 |
+ /* Initialize write structure */
|
|
62 |
+ png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
|
|
63 |
+ if ( png_ptr == NULL )
|
|
64 |
+ {
|
|
65 |
+ fprintf( stderr, "Could not allocate write struct\n" );
|
|
66 |
+ goto Exit1;
|
|
67 |
+ }
|
|
68 |
+ |
|
69 |
+ /* Initialize info structure */
|
|
70 |
+ info_ptr = png_create_info_struct( png_ptr );
|
|
71 |
+ if ( info_ptr == NULL )
|
|
72 |
+ {
|
|
73 |
+ fprintf( stderr, "Could not allocate info struct\n" );
|
|
74 |
+ goto Exit2;
|
|
75 |
+ }
|
|
76 |
+ |
|
77 |
+ /* Set up exception handling */
|
|
78 |
+ if ( setjmp( png_jmpbuf( png_ptr ) ) )
|
|
79 |
+ {
|
|
80 |
+ fprintf( stderr, "Error during png creation\n" );
|
|
81 |
+ goto Exit2;
|
|
82 |
+ }
|
|
83 |
+ |
|
84 |
+ png_init_io( png_ptr, fp );
|
|
85 |
+ |
|
86 |
+ /* Write header (8 bit colour depth) */
|
|
87 |
+ png_set_IHDR( png_ptr, info_ptr, width, height,
|
|
88 |
+ 8, color_type, PNG_INTERLACE_NONE,
|
|
89 |
+ PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE );
|
|
90 |
+ |
|
91 |
+ /* Record version string */
|
|
92 |
+ if ( ver_str != NULL )
|
|
93 |
+ {
|
|
94 |
+ png_text text;
|
|
95 |
+ |
|
96 |
+ |
|
97 |
+ text.compression = PNG_TEXT_COMPRESSION_NONE;
|
|
98 |
+ text.key = (char *)"Software";
|
|
99 |
+ text.text = ver_str;
|
|
100 |
+ |
|
101 |
+ png_set_text( png_ptr, info_ptr, &text, 1 );
|
|
102 |
+ }
|
|
103 |
+ |
|
104 |
+ /* Set gamma */
|
|
105 |
+ png_set_gAMA( png_ptr, info_ptr, 1.0 / display->gamma );
|
|
106 |
+ |
|
107 |
+ png_write_info( png_ptr, info_ptr );
|
|
108 |
+ |
|
109 |
+ if ( bit->mode == gr_pixel_mode_rgb32 )
|
|
110 |
+ {
|
|
111 |
+ const int x = 1;
|
|
112 |
+ |
|
113 |
+ |
|
114 |
+ if ( *(char*)&x ) /* little endian */
|
|
115 |
+ {
|
|
116 |
+ png_set_filler( png_ptr, 0, PNG_FILLER_AFTER );
|
|
117 |
+ png_set_bgr( png_ptr );
|
|
118 |
+ }
|
|
119 |
+ else
|
|
120 |
+ png_set_filler( png_ptr, 0, PNG_FILLER_BEFORE );
|
|
121 |
+ }
|
|
122 |
+ |
|
123 |
+ /* Write image rows */
|
|
124 |
+ row = bit->buffer;
|
|
125 |
+ if ( bit->pitch < 0 )
|
|
126 |
+ row -= ( bit->rows - 1 ) * bit->pitch;
|
|
127 |
+ while ( height-- )
|
|
128 |
+ {
|
|
129 |
+ png_write_row( png_ptr, row );
|
|
130 |
+ row += bit->pitch;
|
|
131 |
+ }
|
|
132 |
+ |
|
133 |
+ /* End write */
|
|
134 |
+ png_write_end( png_ptr, NULL );
|
|
135 |
+ code = 0;
|
|
136 |
+ |
|
137 |
+ Exit2:
|
|
138 |
+ png_destroy_write_struct( &png_ptr, &info_ptr );
|
|
139 |
+ Exit1:
|
|
140 |
+ fclose( fp );
|
|
141 |
+ Exit0:
|
|
142 |
+ return code;
|
|
143 |
+ }
|
|
144 |
+ |
|
145 |
+#else
|
|
146 |
+ |
|
147 |
+ int
|
|
148 |
+ FTDemo_Display_Print( FTDemo_Display* display,
|
|
149 |
+ const char* filename,
|
|
150 |
+ FT_String* ver_str )
|
|
151 |
+ {
|
|
152 |
+ return 0;
|
|
153 |
+ }
|
|
154 |
+ |
|
155 |
+#endif /* !FT_CONFIG_OPTION_USE_PNG */
|
|
156 |
+ |
|
157 |
+ |
|
158 |
+/* End */
|