-
Notifications
You must be signed in to change notification settings - Fork 22
/
pngprepare.c
354 lines (303 loc) · 8.53 KB
/
pngprepare.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright 2002-2010 Guillaume Cottenceau.
* Copyright 2015 Paul Gardner-Stephen.
*
* This software may be freely redistributed under the terms
* of the X11 license.
*
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#define PNG_DEBUG 3
#include <png.h>
char *vhdl_prefix="library IEEE;\n"
"use IEEE.STD_LOGIC_1164.ALL;\n"
"use ieee.numeric_std.all;\n"
"use work.debugtools.all;\n"
"\n"
"--\n"
"entity charrom is\n"
"port (Clk : in std_logic;\n"
" address : in integer range 0 to 4095;\n"
" -- chip select, active low \n"
" cs : in std_logic;\n"
" data_o : out std_logic_vector(7 downto 0);\n"
"\n"
" writeclk : in std_logic;\n"
" -- Yes, we do have a write enable, because we allow modification of ROMs\n"
" -- in the running machine, unless purposely disabled. This gives us\n"
" -- something like the WOM that the Amiga had.\n"
" writecs : in std_logic;\n"
" we : in std_logic;\n"
" writeaddress : in unsigned(11 downto 0);\n"
" data_i : in std_logic_vector(7 downto 0)\n"
" );\n"
"end charrom;\n"
"\n"
"architecture Behavioral of charrom is\n"
"\n"
"-- 4K x 8bit pre-initialised RAM for character ROM\n"
"\n"
"type ram_t is array (0 to 4095) of std_logic_vector(7 downto 0);\n"
"signal ram : ram_t := (\n"
"\n";
char *vhdl_suffix=
");\n"
"\n"
"begin\n"
"\n"
"--process for read and write operation.\n"
"PROCESS(Clk)\n"
"BEGIN\n"
" data_o <= ram(address); \n"
"\n"
" if(rising_edge(writeClk)) then \n"
" if writecs='1' then\n"
" if(we='1') then\n"
" ram(to_integer(writeaddress)) <= data_i;\n"
" end if;\n"
" end if;\n"
" end if;\n"
"END PROCESS;\n"
"\n"
"end Behavioral;\n";
void abort_(const char * s, ...)
{
va_list args;
va_start(args, s);
vfprintf(stderr, s, args);
fprintf(stderr, "\n");
va_end(args);
abort();
}
int x, y;
int width, height;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;
void read_png_file(char* file_name)
{
unsigned char header[8]; // 8 is the maximum size that can be checked
/* open file and test for it being a png */
FILE *fp = fopen(file_name, "rb");
if (!fp)
abort_("[read_png_file] File %s could not be opened for reading", file_name);
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
abort_("[read_png_file] File %s is not recognized as a PNG file", file_name);
/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
abort_("[read_png_file] png_create_read_struct failed");
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
abort_("[read_png_file] png_create_info_struct failed");
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during init_io");
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
// Convert palette to RGB values
png_set_expand(png_ptr);
png_read_info(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
/* read file */
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during read_image");
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++)
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
png_read_image(png_ptr, row_pointers);
fclose(fp);
}
void process_file(int mode,char *outputfilename)
{
int multiplier=-1;
if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB)
multiplier=3;
if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGBA)
multiplier=4;
if (multiplier==-1) {
fprintf(stderr,"Could not convert file to RGB or RGBA\n");
}
if (mode==0) {
// Logo mode
FILE *outfile=fopen(outputfilename,"w");
if (height!=64||width!=64) {
fprintf(stderr,"Logo images must be 64x64\n");
}
for (y=0; y<height; y++) {
png_byte* row = row_pointers[y];
for (x=0; x<width; x++) {
png_byte* ptr = &(row[x*multiplier]);
int r=ptr[0],g=ptr[1],b=ptr[2]; // a=ptr[3];
// Compute colour cube colour
unsigned char c=(r&0xe0)|((g>>5)<<2)|(b>>6);
/* work out where in logo file it must be written.
image is made of 8x8 blocks. So every 8 pixels across increases address
by 64, and every 8 pixels down increases pixel count by (64*8), and every
single pixel down increases address by 8.
*/
int address=(x&7)+(y&7)*8;
address+=(x>>3)*64;
address+=(y>>3)*64*8;
fseek(outfile,address,SEEK_SET);
int n=fwrite(&c,1,1,outfile);
if (n!=1) {
fprintf(stderr,"Could not write pixel (%d,%d) @ $%x\n",x,y,address);
exit(-1);
}
}
}
fclose(outfile);
}
if (mode==1) {
// charrom mode
int bytes=0;
FILE *outfile=fopen(outputfilename,"w");
fprintf(outfile,"%s",vhdl_prefix);
if (width!=8) {
fprintf(stderr,"Fonts must be 8 pixels wide\n");
}
int spots[8][8];
for (y=0; y<height; y++) {
png_byte* row = row_pointers[y];
int byte=0;
int yy=y&7;
for (x=0; x<width; x++) {
png_byte* ptr = &(row[x*multiplier]);
int r=ptr[0]; // g=ptr[1],b=ptr[2], a=ptr[3];
if (x<8) {
if (r>0x7f) {
byte|=(1<<(7-x));
spots[yy][x]=1;
} else spots[yy][x]=0;
}
}
fflush(stdout);
char comma = ',';
if (y==height-1) comma=' ';
fprintf(outfile,"x\"%02x\"%c",byte,comma);
bytes++;
if ((y&7)==7) {
fprintf(outfile,"\n");
int yy;
for(yy=0;yy<8;yy++) {
fprintf(outfile,"-- [");
for(x=0;x<8;x++) {
if (spots[yy][x]) fprintf(outfile,"*"); else fprintf(outfile," ");
}
fprintf(outfile,"]\n");
}
}
}
// Fill in any missing bytes
if (bytes<4096) {
fprintf(outfile,",\n");
for(;bytes<4096;bytes+=8) {
fprintf(outfile,"x\"00\",x\"00\",x\"00\",x\"00\",x\"00\",x\"00\",x\"00\",x\"00\"%c\n",
bytes<(4096-8)?',':' ');
}
}
fprintf(outfile,"%s",vhdl_suffix);
fclose(outfile);
}
if (mode==2) {
// hi-res image preparation mode
// int bytes=0;
if (width%8||height%8) {
fprintf(stderr,"Image must be multiple of 8 pixels wide and high\n");
}
int problems=0;
int total=0;
int threes=0;
int fours=0;
int ones=0;
int tiles[8000][8][8];
int tile_count=0;
int this_tile[8][8];
for (y=0; y<height; y+=8) {
for (x=0; x<width; x+=8) {
int yy,xx;
int i;
int colour_count=0;
int colours[64];
printf("[%d,%d]\n",x,y);
total++;
for(yy=y;yy<y+8;yy++) {
png_byte* row = row_pointers[yy];
for(xx=x;xx<x+8;xx++) {
png_byte* ptr = &(row[xx*multiplier]);
int r=ptr[0], g=ptr[1],b=ptr[2]; // , a=ptr[3];
int c=r+256*g+65536*b;
this_tile[yy-y][xx-x]=c;
for(i=0;i<colour_count;i++) if (c==colours[i]) break;
if (i==colour_count) {
colours[colour_count++]=c;
}
}
}
for(i=0;i<tile_count;i++) {
int dud=0;
int xx,yy;
for(xx=0;xx<8;xx++)
for(yy=0;yy<8;yy++) {
if (this_tile[yy][xx]!=tiles[i][yy][xx]) dud=1;
}
if (!dud) break;
}
if (i==tile_count) {
int xx,yy;
for(xx=0;xx<8;xx++)
for(yy=0;yy<8;yy++) {
tiles[tile_count][yy][xx]=this_tile[yy][xx];
}
printf(".[%d]",tile_count); fflush(stdout);
tile_count++;
if (tile_count>=8000) {
fprintf(stderr,"Too many tiles\n");
exit(-1);
}
}
if (colour_count==1) ones++;
if (colour_count==3) threes++;
if (colour_count==4) fours++;
if (colour_count>2) {
printf("%d colours in card\n",colour_count);
problems++;
}
}
}
printf("%d problem tiles out of %d total tiles\n",problems,total);
printf("%d with 3, %d with 4, %d with only one colour\n",threes,fours,ones);
printf("%d unique tiles\n",tile_count);
}
}
int main(int argc, char **argv)
{
if (argc != 4) {
fprintf(stderr,"Usage: program_name <logo|charrom> <file_in> <file_out>\n");
exit(-1);
}
int mode=-1;
if (!strcasecmp("logo",argv[1])) mode=0;
if (!strcasecmp("charrom",argv[1])) mode=1;
if (!strcasecmp("hires",argv[1])) mode=2;
if (mode==-1) {
fprintf(stderr,"Usage: program_name <logo|charrom> <file_in> <file_out>\n");
exit(-1);
}
read_png_file(argv[2]);
process_file(mode,argv[3]);
return 0;
}