source: svn/trunk/newcon3bcm2_21bu/dta/tools/pngtoraw.c @ 5

Last change on this file since 5 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 15.5 KB
Line 
1/***************************************************************
2**
3** Broadcom Corp. Confidential
4** Copyright 2002 Broadcom Corp. All Rights Reserved.
5**
6** THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED
7** SOFTWARE LICENSE AGREEMENT BETWEEN THE USER AND BROADCOM.
8** YOU HAVE NO RIGHT TO USE OR EXPLOIT THIS MATERIAL EXCEPT
9** SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
10**
11** Description: convert simple clut 8 pngs to raw clut images
12**
13** Created: 8/22/2002 by Jeffrey P. Fisher
14**
15**
16**
17****************************************************************/
18
19#include <sys/time.h> /* for gettimeofday() */
20#include <stdio.h>    /* for fprintf()      */
21#include <stdlib.h>   /* for rand()         */
22#include <unistd.h>   /* for sleep()        */
23#include <string.h>   /* for memset()       */
24#include <stdio.h>
25#include <png.h>
26
27/* Broadcom includes */
28
29/* structure holding all necessary decode info */
30typedef struct ptw_t
31{
32        FILE *log_fp;
33        png_structp png_ptr;
34        png_infop info_ptr;
35        png_infop end_info;
36        png_uint_32 width;
37        png_uint_32 height;
38        int bit_depth;
39        int color_type; 
40        int interlace_method;
41        int compression_method;
42        int filter_method;
43        png_byte channels;
44        png_uint_32 rowbytes;
45        png_colorp palette;
46        int num_palette;
47        png_bytep trans;
48        int num_trans;
49        png_color_16p trans_values;
50        png_bytepp row_pointers;
51}ptw_t;
52
53typedef struct bcm_raw_8_t
54{
55        unsigned long version;                  /* version number of raw file - for future use */
56        unsigned long type;                             /* type of raw file - for future use */
57        unsigned long pitch;                    /* number of bytes per row */
58        unsigned short width;                   /* number of pixel per row */
59        unsigned short height;                  /* number of rows */
60        unsigned char color_key;                /* color key/transparency color value */
61        unsigned short clut_size;               /* number of entries in the ARGB8888 CLUT */
62}bcm_raw_8_t;
63
64#define DBG_FPRINTF(x)   fprintf x
65#define RAW_TYPE                0x52415720
66#define RAW_VERSION             0x00000001
67/****************************************************************
68* validate_png_header
69*
70* INPUTS:       ptw_p
71* OUTPUTS:      none
72* RETURNS:      non-zero if supported
73* FUNCTION: Function to inspect header and validate it is a compatible
74*                       image
75*
76****************************************************************/
77int validate_png_header(ptw_t *ptw_p)
78{
79        int supported = 0;
80        int format_changed = 0;
81        png_get_IHDR(ptw_p->png_ptr, ptw_p->info_ptr, &ptw_p->width, &ptw_p->height,
82                                 &ptw_p->bit_depth, &ptw_p->color_type, &ptw_p->interlace_method,
83                                 &ptw_p->compression_method, &ptw_p->filter_method);
84        ptw_p->channels = png_get_channels(ptw_p->png_ptr, ptw_p->info_ptr);
85        ptw_p->rowbytes = png_get_rowbytes(ptw_p->png_ptr, ptw_p->info_ptr);
86
87        DBG_FPRINTF((ptw_p->log_fp,"width = %d\n",ptw_p->width));
88        DBG_FPRINTF((ptw_p->log_fp,"height = %d\n",ptw_p->height));
89        DBG_FPRINTF((ptw_p->log_fp,"bit_depth = %d\n",ptw_p->bit_depth));
90        DBG_FPRINTF((ptw_p->log_fp,"color_type = 0x%08x(0x%08x,0x%08x,0x%08x)\n",ptw_p->color_type,
91                        ptw_p->color_type & PNG_COLOR_MASK_PALETTE,
92                        ptw_p->color_type & PNG_COLOR_MASK_COLOR,
93                        ptw_p->color_type & PNG_COLOR_MASK_ALPHA));
94        DBG_FPRINTF((ptw_p->log_fp,"channels = %d\n",ptw_p->channels));
95        DBG_FPRINTF((ptw_p->log_fp,"rowbytes = %d\n",ptw_p->rowbytes));
96        DBG_FPRINTF((ptw_p->log_fp,"interlace_type = %d\n",ptw_p->interlace_method));
97        DBG_FPRINTF((ptw_p->log_fp,"compression_type = %d\n",ptw_p->compression_method));
98        DBG_FPRINTF((ptw_p->log_fp,"filter_method = %d\n",ptw_p->filter_method));
99
100        supported = (((ptw_p->bit_depth == 8) || (ptw_p->bit_depth == 4)) && (ptw_p->color_type & PNG_COLOR_MASK_PALETTE)
101                                 && ((ptw_p->channels == 3) || (ptw_p->channels == 4) || (ptw_p->channels == 1)));
102        if (ptw_p->bit_depth == 4)
103        {
104#if 0
105                png_color_8p sig_bit;
106
107                if (png_get_sBIT(ptw_p->png_ptr, ptw_p->info_ptr, &sig_bit))
108                        png_set_shift(ptw_p->png_ptr, sig_bit);
109#endif
110        }
111
112        if (!supported)
113        {
114                /* Check for ARGB 8888 */
115
116                supported = ((ptw_p->bit_depth == 8) && (ptw_p->color_type & PNG_COLOR_MASK_COLOR));
117                if (!(ptw_p->color_type & PNG_COLOR_MASK_ALPHA) || (ptw_p->channels != 4))
118                {
119                        png_set_add_alpha(ptw_p->png_ptr,0xff,PNG_FILLER_AFTER);
120                        format_changed = 1;
121                }
122#if 0
123                if (ptw_p->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
124                        png_set_swap_alpha(ptw_p->png_ptr);
125#endif
126                if (ptw_p->color_type == PNG_COLOR_TYPE_RGB || ptw_p->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
127                {
128                        png_set_bgr(ptw_p->png_ptr);
129                        format_changed = 1;
130                }
131
132                if (format_changed)
133                {
134                        png_read_update_info(ptw_p->png_ptr, ptw_p->info_ptr);
135                        png_get_IHDR(ptw_p->png_ptr, ptw_p->info_ptr, &ptw_p->width, &ptw_p->height,
136                                                 &ptw_p->bit_depth, &ptw_p->color_type, &ptw_p->interlace_method,
137                                                 &ptw_p->compression_method, &ptw_p->filter_method);
138                        ptw_p->channels = png_get_channels(ptw_p->png_ptr, ptw_p->info_ptr);
139                        ptw_p->rowbytes = png_get_rowbytes(ptw_p->png_ptr, ptw_p->info_ptr);
140                }
141        }
142        DBG_FPRINTF((ptw_p->log_fp,"PNG type %s\n" ,supported ? "Supported":"Not Supported"));
143        return supported;
144}
145/****************************************************************
146* read_row_callback
147*
148* INPUTS:       
149* OUTPUTS:      none
150* RETURNS:      none
151* FUNCTION:
152*
153****************************************************************/
154void read_row_callback(png_structp ptr, png_uint_32 row, int pass)
155{
156#ifdef PTR_DEBUG
157        printf("read_row_callback(%d,%d)\n",row,pass);
158#endif
159}
160/****************************************************************
161* read_chunk_callback
162*
163* INPUTS:       
164* OUTPUTS:      none
165* RETURNS:      none
166* FUNCTION:
167*
168****************************************************************/
169int read_chunk_callback(png_structp ptr, png_unknown_chunkp chunk)
170{
171
172        /* put your code here.  Return one of the
173           following: */
174#ifdef PTR_DEBUG
175        printf("read_chunk_callback(0x%02%02%02%02%02,%d,%p)\n",
176                   chunk->name[0],chunk->name[1],chunk->name[2],
177                   chunk->name[3],chunk->name[4],chunk->size,chunk->data);
178#endif
179        //return(-n);   /* chunk had an error */
180        //return(0); /* did not recognize */
181        return (chunk->size); /* success */
182}
183
184
185/****************************************************************
186* inspect_palette
187*
188* INPUTS:       ptw_p
189* OUTPUTS:      none
190* RETURNS:      none
191* FUNCTION: Output the palette and transparency info
192*
193****************************************************************/
194void inspect_palette(ptw_t *ptw_p)
195{
196        int i,cnt;
197        png_get_PLTE(ptw_p->png_ptr, ptw_p->info_ptr, &ptw_p->palette, &ptw_p->num_palette);
198        png_get_tRNS(ptw_p->png_ptr, ptw_p->info_ptr, &ptw_p->trans, &ptw_p->num_trans,
199                                 &ptw_p->trans_values);
200
201        DBG_FPRINTF((ptw_p->log_fp,"\nRGB Palette[%d]:\n",ptw_p->num_palette));
202        for (i = 0; i < ptw_p->num_palette; ++i)
203        {
204                DBG_FPRINTF((ptw_p->log_fp,"RGB[0x%02x]=0x%02x 0x%02x 0x%02x\n",i,ptw_p->palette[i].red,
205                                ptw_p->palette[i].green, ptw_p->palette[i].blue));
206        }
207
208        DBG_FPRINTF((ptw_p->log_fp,"\nTransparency[%d]:\n",ptw_p->num_trans));
209        for (i = 0; i < ptw_p->num_trans; ++i)
210        {
211                DBG_FPRINTF((ptw_p->log_fp,"TRN[0x%02x]=0x%02x\n",i,ptw_p->trans[i]));
212        }
213
214        cnt = 0;
215        DBG_FPRINTF((ptw_p->log_fp,"\nARGB Palette[%d]:\n",ptw_p->num_palette));
216        for (i = 0; i < ptw_p->num_palette; ++i)
217        {
218                DBG_FPRINTF((ptw_p->log_fp,"0x%02x%02x%02x%02x,",(i < ptw_p->num_trans) ? ptw_p->trans[i] : 0xFF ,
219                        ptw_p->palette[i].red, ptw_p->palette[i].green, ptw_p->palette[i].blue));
220                if (cnt++ >= 0x7)
221                {
222                        cnt = 0;
223                        DBG_FPRINTF((ptw_p->log_fp,"\n"));
224                }
225        }
226        DBG_FPRINTF((ptw_p->log_fp,"\n"));
227}
228/****************************************************************
229* get_color_key
230*
231* INPUTS:       ptw_p
232* OUTPUTS:      none
233* RETURNS:      value or -1 if no value is found
234* FUNCTION: Get the first transparent value
235*
236****************************************************************/
237unsigned char get_color_key(ptw_t *ptw_p)
238{
239        int i;
240        //printf("get_color_key %d\n", ptw_p->num_palette);
241        for (i = 0; i < ptw_p->num_trans; ++i)
242        {
243                if (ptw_p->trans[i] == 0)
244                {
245                        if (i >= ptw_p->num_palette)
246                                return (ptw_p->num_palette - 1);
247                        return i;
248                }
249        }
250        return ptw_p->num_palette - 1;
251}
252
253/****************************************************************
254* Output a row (bit_depth = 4 only)
255*
256****************************************************************/
257void print_row(unsigned char* p_row,int width, int pitch, int rowbytes)
258{
259#if 0
260        int i;
261        for (i = 0; i < width; ++i)
262        {
263                if (i & 1)
264                {
265                        printf("0x%1x,", p_row[i / 2] & 0x0F);
266                }
267                else
268                {
269                        printf("0x%1x,", (p_row[i / 2] >> 4) & 0x0F);
270                }
271        }
272        printf("\n");
273        for (i = 0; i < width; ++i)
274        {
275                printf("0x%02x,", p_row[i]);
276        }
277        printf("\n");
278#endif
279}
280/****************************************************************
281*
282****************************************************************/
283void hex_output(FILE* f,unsigned char* ptr,int len)
284{
285        int i;
286        for (i = 0; i < len; i++)
287        {
288                if (i % 8 == 0)
289                {
290                        fprintf(f,"\n\t");
291                }
292                fprintf(f,"0x%02x, ",ptr[i]);
293        }
294}
295/****************************************************************
296* main
297*
298* INPUTS:               arc - argument count
299*                           argv - array of arguments
300* OUTPUTS:              none
301* RETURNS:              none
302* FUNCTION:             main entry point
303* DESCRIPTION:  Convert PNGs to raw clut 8 images.
304*
305****************************************************************/
306
307int main(int argc, char *argv[])
308{
309        FILE *fp = NULL;
310        FILE *out_fp = NULL;
311        int result = -1; 
312        char header[8];
313        int is_png,row,i,palette_only,yuv,cfile;
314        ptw_t ptw;
315        bcm_raw_8_t raw;
316        unsigned long clut[0xFF];
317        FILE* logfile = NULL;
318        palette_only = 0;
319        cfile = 0;
320
321        memset(clut,0,sizeof(unsigned long) * 0xFF);
322        memset(&ptw,0,sizeof(ptw_t));
323        yuv = 1;
324
325        if (argc < 3)
326        {
327                printf("USAGE: pngtoraw <png filename> <out_filename> [-p -v -r -c]\n");
328                printf("\t-p  Output a raw palette only\n");
329                printf("\t-r  Output RGB palette (default is YUV)\n");
330                printf("\t-v  Verbose mode\n");
331                printf("\t-c  Output raw image as C structure\n");
332                goto done;
333        }
334
335        for (i = 3; i < argc; ++i)
336        {
337                if ((strcmp(argv[i],"-v") == 0) || (strcmp(argv[i],"-V") == 0))
338                {
339                        ptw.log_fp = stdout;
340                }
341                else if ((strcmp(argv[i],"-p") == 0) || (strcmp(argv[i],"-P") == 0))
342                {
343                        printf("Output palette only\n");
344                        palette_only = 1;
345                }
346                else if ((strcmp(argv[i],"-r") == 0) || (strcmp(argv[i],"-R") == 0))
347                {
348                        printf("Output RGB palette\n");
349                        yuv = 0;
350                }
351                else if ((strcmp(argv[i],"-c") == 0) || (strcmp(argv[i],"-C") == 0))
352                {
353                        printf("Output raw image as C structure.\n");
354                        cfile = 1;
355                }
356        }
357        if (!ptw.log_fp)
358        {
359                logfile = ptw.log_fp = fopen("pngtoraw.log", "w");
360        }
361       
362        fp = fopen(argv[1], "rb");
363        if (!fp)
364        {
365                printf("Error opening file %s\n",argv[1]);
366                goto done;
367        }
368
369        out_fp = fopen(argv[2], "wb");
370        if (!out_fp)
371        {
372                printf("Error opening file %s\n",argv[2]);
373                goto done;
374        }
375
376        /* check the PNG header */
377        fread(header, 1, 8, fp);
378        is_png = !png_sig_cmp(header, 0, 8);
379        if (!is_png)
380        {
381                printf("%s not a PNG file\n",argv[1]);
382                goto done;
383        }
384
385        /* Initialize the PNG library and structures */
386        ptw.png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
387                                                                                  NULL, NULL);
388        if (!ptw.png_ptr)
389        {
390                printf("png_create_read_struct error\n");
391                goto done;
392        }
393
394        ptw.info_ptr = png_create_info_struct(ptw.png_ptr);
395        if (!ptw.info_ptr)
396        {
397                printf("png_create_info_struct error\n");
398                goto done;
399        }
400
401        ptw.end_info = png_create_info_struct(ptw.png_ptr);
402        if (!ptw.end_info)
403        {
404                printf("png_create_info_struct error\n");
405                goto done;
406        }
407       
408        png_init_io(ptw.png_ptr, fp);
409
410    png_set_sig_bytes(ptw.png_ptr,8);
411
412        png_set_read_user_chunk_fn(ptw.png_ptr, &ptw, read_chunk_callback);
413        png_set_read_status_fn(ptw.png_ptr, read_row_callback);
414
415        png_read_info(ptw.png_ptr, ptw.info_ptr);
416
417        if (!validate_png_header(&ptw))
418                goto done;
419
420        if (ptw.color_type & PNG_COLOR_MASK_PALETTE)
421        {
422                inspect_palette(&ptw);
423        }
424
425        // png_read_png(ptw.png_ptr, ptw.info_ptr,PNG_TRANSFORM_IDENTITY , NULL);
426        ptw.row_pointers = png_malloc(ptw.png_ptr, ptw.height * sizeof(ptw.row_pointers));
427        for (row = 0; row < ptw.height; row++)
428        {
429           ptw.row_pointers[row] = png_malloc(ptw.png_ptr, ptw.rowbytes);
430        }
431        png_read_image(ptw.png_ptr, ptw.row_pointers);
432               
433        png_read_end(ptw.png_ptr, ptw.end_info);
434       
435        /* pitch required for broadcom surface */
436        //raw.pitch =  ((((ptw.width * 4) / 4/* pp4 bytes */) +  0xF) &  ~0xF);
437        if (ptw.color_type & PNG_COLOR_MASK_PALETTE)
438                raw.pitch =  (ptw.width * ptw.bit_depth) / 8;
439        else
440                raw.pitch =  ptw.rowbytes;
441
442        raw.width = ptw.width;
443        raw.height = ptw.height;
444        raw.clut_size = ptw.num_palette;
445        raw.type = RAW_TYPE;
446        raw.version = RAW_VERSION;
447        DBG_FPRINTF((ptw.log_fp,"raw.pitch = %d\n",raw.pitch));
448        DBG_FPRINTF((ptw.log_fp,"raw.width = %d\n",raw.width));
449        DBG_FPRINTF((ptw.log_fp,"raw.height = %d\n",raw.height));
450        DBG_FPRINTF((ptw.log_fp,"raw.clut_size = %d\n",raw.clut_size));
451
452        if (!cfile)
453        {
454                fwrite((char*)&raw,1,sizeof(raw),out_fp); 
455        }
456        else
457        {
458                char *dot_ptr;
459                unsigned char *ptr;
460                static char out_name[1024];
461                int len;
462
463                fprintf(out_fp,"/* FILE: %s */\n",argv[2]);
464                fprintf(out_fp,"/* pitch: %d */\n",raw.pitch);
465                fprintf(out_fp,"/* width: %d */\n",raw.width);
466                fprintf(out_fp,"/* height: %d */\n",raw.height);
467                fprintf(out_fp,"/* clut_size: %d */\n",raw.clut_size);
468
469                dot_ptr = strstr(argv[2],".");
470
471                if (!dot_ptr)
472                {
473                        sprintf(out_name,"%s.c",argv[2]);
474                }
475                else
476                {
477                        strcpy(out_name,argv[2]);
478                        dot_ptr[0] = 0;
479                        sprintf(out_name,"%s.c",argv[2]);
480                }
481
482                len = sizeof(raw) + (raw.pitch * raw.height) + (raw.clut_size * sizeof(unsigned int));
483                fprintf(out_fp,"\nunsigned int g_%s_size = %d;\n",argv[2],len);
484                fprintf(out_fp,"const unsigned char g_%s[] = \n{\n\t",argv[2]);
485                hex_output(out_fp,(unsigned char*)&raw,sizeof(raw));
486        }
487
488        if (ptw.color_type & PNG_COLOR_MASK_PALETTE)
489        {
490                raw.color_key = get_color_key(&ptw);
491                DBG_FPRINTF((ptw.log_fp,"raw.color_key = 0x%02x\n",raw.color_key));
492                for (i = 0; i < raw.clut_size; ++i)
493                {
494                        unsigned long trans;
495                        if (i < ptw.num_trans)
496                                trans = ptw.trans[i];
497                        else
498                                trans = 0xFF;
499                       
500                        if ((ptw.num_trans == 0) && (i == raw.color_key))
501                        {
502                                trans = 0;
503                        }
504       
505                        if (yuv)
506                        {
507                                unsigned int Y,U,V;
508                                unsigned int R,G,B;
509                                R = ((unsigned int)ptw.palette[i].red) & 0xFF;
510                                G = ((unsigned int)ptw.palette[i].green) & 0xFF;
511                                B = ((unsigned int)ptw.palette[i].blue) & 0xFF;
512       
513                                Y  =      (0.257 * R) + (0.504 * G) + (0.098 * B) + 16;
514       
515                                V =  (0.439 * R) - (0.368 * G) - (0.071 * B) + 128;
516       
517                                U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128;
518       
519                                clut[i] = (trans << 24) | 
520                                                ((Y & 0xFF) << 16) |
521                                                ((U & 0xFF) << 8) | 
522                                                (V & 0xFF);
523                        }
524                        else
525                        {
526                                clut[i] = (trans << 24) | 
527                                                (((unsigned long)ptw.palette[i].red) << 16) |
528                                                (((unsigned long)ptw.palette[i].green) << 8) | 
529                                                (unsigned long)ptw.palette[i].blue;
530                        }
531                }
532
533                /* ouput the file */
534                if (cfile)
535                {
536                        hex_output(out_fp,(unsigned char*)clut,(sizeof(unsigned long) * raw.clut_size));
537                }
538                else
539                {
540                        fwrite((char*)&clut,1,(sizeof(unsigned long) * raw.clut_size),out_fp); 
541                }
542        }
543       
544        if (palette_only)
545                goto done;
546
547        for (row = 0; row < raw.height; row++)
548        {
549                if (cfile)
550                        hex_output(out_fp,(unsigned char*)ptw.row_pointers[row],raw.pitch);
551                else
552                        fwrite((char*)ptw.row_pointers[row],1,raw.pitch,out_fp);
553               
554                print_row((char*)ptw.row_pointers[row],raw.width, raw.pitch,ptw.rowbytes);
555
556                if (ptw.rowbytes < raw.pitch)
557                {
558                        if (cfile)
559                                hex_output(out_fp,(unsigned char*)ptw.row_pointers[row],raw.pitch - ptw.rowbytes);
560                        else
561                                fwrite((char*)ptw.row_pointers[row],1,raw.pitch - ptw.rowbytes,out_fp); 
562                }
563        }
564        if (cfile)
565                fprintf(out_fp,"\n};\n");
566
567done:
568
569        if (ptw.png_ptr && !ptw.info_ptr && !ptw.end_info)
570                png_destroy_read_struct(&ptw.png_ptr, (png_infopp)NULL, (png_infopp)NULL);
571        if (ptw.png_ptr && ptw.info_ptr && !ptw.end_info)
572                png_destroy_read_struct(&ptw.png_ptr, (png_infopp)&ptw.info_ptr, (png_infopp)NULL);
573        if (ptw.png_ptr && ptw.info_ptr && ptw.end_info)
574                png_destroy_read_struct(&ptw.png_ptr, (png_infopp)&ptw.info_ptr, (png_infopp)&ptw.end_info);
575        if (fp)
576                fclose(fp);
577        if (out_fp)
578                fclose(out_fp);
579        if (logfile)
580                fclose(logfile);
581
582        return result;
583}
584
585
Note: See TracBrowser for help on using the repository browser.