Debian Codegraph @

extras/graphic_lcd/utils/pngtopic.c

raw
1 /*
2 pngtopic
3
4 Convert a png formatted graphics file into a format that may be
5 inserted into a Microchip PIC .asm file. The main purpose is to
6 allow graphics to be easily used with gpsim's graphic LCD driver.
7
8 Compile with:
9
10 gcc -g -o pngtopic pngtopic.c -lpng
11
12 */
13
14 #include <png.h>
15
16 #define ERROR 0xffff
17 //#define DEBUG
18
19 /* Globals */
20
21 png_bytep *gRowPointers; /* Dynamically allocated by libpng. .png file is stored here*/
22 png_uint_32 gWidth; /* Image width (in pixels) */
23 png_uint_32 gHeight; /* Image height (in pixels) */
24 int gBitDepth; /* Color depth for each pixel */
25 int gColorType; /* 3-bit field describing png color type */
26
27
28 int byte_to_nibble(int byte);
29
30 void usage()
31 {
32 printf ("usage: pngtopic [-i] filename.png\n");
33 printf (" options:\n");
34 printf (" -i - invert pixels\n");
35 printf (" -p - preview ASCII image\n");
36
37 }
38
39 void help()
40 {
41 usage();
42
43 printf("\n pngtopic converts png files to gpasm include files.\n");
44 printf(" These files are compatible with gpsim's graphics LCD driver\n");
45 printf(" Copyright - T. Scott Dattalo\n");
46
47 }
48
49 void error(const char *cPerrmsg)
50 {
51 printf("%s\n",cPerrmsg);
52 usage();
53 exit(1);
54 }
55
56 /*
57 getPixel(unsigned int row, unsigned int col)
58
59 Extract a single pixel from the image.
60
61
62 */
63 int getPixel(unsigned int row, unsigned int col)
64 {
65 if (row < gHeight && col < gWidth) {
66
67 if (gColorType & PNG_COLOR_MASK_ALPHA) {
68 /* This probably is not exactly correct.
69 */
70 int pixel_byte_index = col * 4;
71 int byte = gRowPointers[row][pixel_byte_index];
72 return (byte>127);
73
74 } else {
75 int pixels_per_byte = 8/gBitDepth;
76 if (pixels_per_byte) {
77 int pixel_byte_index = col / pixels_per_byte;
78 int pixel_bit_index = col % pixels_per_byte;
79 int byte = gRowPointers[row][pixel_byte_index];
80 int pixel = byte >> ((pixels_per_byte - pixel_bit_index-1)*gBitDepth);
81 int mask = (1<<gBitDepth)-1;
82 if (row == 700) {
83 printf("row=%d,col=%d, pixels_per_byte=%d pixel_byte_index=%d pixel_bit_index%d\n",
84 row, col,
85 pixels_per_byte,
86 pixel_byte_index,pixel_bit_index);
87 printf("byte=0x%x pixel=%d mask=0x%x\n ",byte,pixel&mask,mask);
88 }
89 return ((pixel & mask) > (mask/2)) ? 1 : 0;
90 }
91 }
92 }
93
94 return 0;
95 }
96
97
98 /*************************************************************************/
99
100 int main(int argc, char **argv)
101 {
102 char header[8];
103 int number = 8;
104 int is_png = 0;
105
106 if (argc < 2) {
107 usage();
108 return ERROR;
109 }
110
111 int bInvert=0; /* Command line argument for inverting the image */
112 int bPreview=0; /* Command line option for getting an ASCII image */
113
114 int argCounter = 1;
115 while (argCounter < (argc-1)) {
116
117 if (*argv[argCounter] == '-') {
118 switch (*(argv[argCounter]+1)) {
119 case 'i':
120 bInvert = 1;
121 break;
122 case 'h':
123 help();
124 return 0;
125 case 'p':
126 bPreview = 1;
127 break;
128 }
129
130 argCounter++;
131
132 } else
133
134 break;
135 }
136
137 /* Open the file containing the image */
138 /* A few preliminary checks are made to ensure the file is valid. */
139
140 const char *file_name = argv[argc-1];
141 FILE *fp = fopen(file_name, "rb");
142
143 if (!fp)
144 error ("File not found");
145
146 fread(header, 1, number, fp);
147 is_png = !png_sig_cmp(header, 0, number);
148 if (!is_png)
149 error("File is not png compatible\n");
150
151 /* Much of what follows comes straight from the png documentation.
152 */
153 png_structp png_ptr = png_create_read_struct
154 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
155 if (!png_ptr)
156 return (ERROR);
157
158 png_infop info_ptr = png_create_info_struct(png_ptr);
159 if (!info_ptr)
160 {
161 png_destroy_read_struct(&png_ptr,
162 (png_infopp)NULL, (png_infopp)NULL);
163 return (ERROR);
164 }
165
166 png_infop end_info = png_create_info_struct(png_ptr);
167 if (!end_info)
168 {
169 png_destroy_read_struct(&png_ptr, &info_ptr,
170 (png_infopp)NULL);
171 return (ERROR);
172 }
173
174 png_init_io(png_ptr, fp);
175 png_set_sig_bytes(png_ptr, number);
176
177 png_read_info(png_ptr, info_ptr);
178 int interlace_type;
179 int compression_type;
180 int filter_method;
181
182 png_get_IHDR(png_ptr, info_ptr, &gWidth, &gHeight,
183 &gBitDepth, &gColorType, &interlace_type,
184 &compression_type, &filter_method);
185
186 if (!(gWidth <= 100 && gHeight <= 32)) {
187 printf (" Image is too large (%dX%d)\n",gWidth,gHeight);
188 return (ERROR);
189 }
190
191 /* One way to handle png files is by allocating memory for holding
192 the image. This is recommended for small files like the ones
193 we're dealing with.
194 */
195 gRowPointers = png_malloc(png_ptr,
196 gHeight*png_sizeof(png_bytep));
197 int i,j;
198 int pixel_size = 1;
199 int pixel_depth = (gColorType & PNG_COLOR_MASK_ALPHA) ? 4 : 1;
200
201 for (i=0; i<gHeight; i++)
202 gRowPointers[i]=png_malloc(png_ptr,
203 gWidth*pixel_size*pixel_depth);
204 png_set_rows(png_ptr, info_ptr, gRowPointers);
205
206 /* Finally, we can read in the image.*/
207
208 png_read_image(png_ptr, gRowPointers);
209
210
211 /* Now that image is in memory, we can extract the pixels in any
212 * convenient order. For ASCII previews, we'll just read the pixels
213 * straight off a row at a time. For the PIC file format, a column
214 * of 8 pixels are combined into a single byte.
215 */
216 int bytes_per_row = gWidth * gBitDepth / 8 * pixel_depth;
217 int mask = (1<<gBitDepth) - 1;
218 int threshold = mask / 2; //(1<<(gBitDepth-1))-1;
219
220
221 printf ("; pngtopic - png to gpasm PIC include file\n");
222 printf ("; for use with gpsim's graphic's LCD driver\n");
223 printf ("; Copyright 2005 - Scott Dattalo\n");
224 printf ("; Automatically converted from %s\n", file_name);
225 printf (";\n");
226
227 #if defined(DEBUG)
228 printf (";BitDepth=%d gColorType=%d interlace_type=%d\n",
229 gBitDepth,gColorType,interlace_type);
230 printf (";bytes_per_row=%d\n",bytes_per_row);
231 printf (";mask=%d threshold=%d\n",mask, threshold);
232 #endif /* defined (DEBUG)*/
233
234
235
236
237 if (bPreview) {
238 printf(";");
239 for (i=0; i<gHeight; i++) {
240 printf ("\n; ");
241 #if defined(DEBUG)
242 printf ("row:%2d ",i);
243 for (j=0; j<gWidth/8*gBitDepth; j++) {
244 printf ("%02X",gRowPointers[i][j]);
245 }
246 printf (" .");
247
248 for (j=0; j<gWidth; j++) {
249 printf("%d",getPixel(i,j));
250 }
251 #endif
252
253 for (j=0; j<gWidth; j++) {
254 printf("%s", (getPixel(i,j) ^ bInvert) ? " " : "[]");
255 }
256
257 printf (".");
258 }
259
260 printf("\n;\n");
261 }
262
263 /* Convert the image into the PIC format. */
264 printf (" db 0x%02x,0x%02x ;width in pixels and height in bytes\n", gWidth, gHeight/8);
265
266 /* The 'db' directive is used to define bytes in gpasm/mpasm. Limit to 8 bytes: */
267 #define DBS_PER_ROW 8
268
269 int dbcount = 0;
270
271 for (i=0; i<gHeight; i+=8) {
272
273 for (j=0; j<gWidth; j++) {
274 int db = 0;
275 int k;
276 for (k=0; k<8; k++) {
277 db >>= 1;
278 db |= (getPixel(i+k,j) ^ bInvert) ? 0 : 0x80;
279 }
280
281 if (dbcount == 0)
282 printf (" db 0x%02x",db);
283 else
284 printf (",0x%02x",db);
285
286 if (++dbcount >= DBS_PER_ROW) {
287 printf("\n");
288 dbcount = 0;
289 }
290
291 }
292
293 }
294
295 return 0;
296 }
297