1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - tools - ppmconv
3 File: ppmconv.c
4
5 Copyright 2003-2008 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Date:: $
14 $Rev:$
15 $Author:$
16 *---------------------------------------------------------------------------*/
17 //
18 // This 'ppmconv' tool is for $TwlSDK/build/demos/GX/UnitTest/2D_BmpBg_*
19 // Please see detail at 2D_BmpBg_* directories.
20 //
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26
27 #define TRUE 1
28 #define FALSE 0
29
30 #define NUM_COLUMN 16
31
32 #define V5bit( x ) ((x)>>3)
33 #define RGB5551( r, g, b, a ) ((V5bit(r)<<0) | (V5bit(g)<<5) | (V5bit(b)<<10)| (((a)&1)<<15))
34
35 #define SIZE_READBUFFER 1024
36
37 enum
38 {
39 PHASE_READ_MAGIC_NUMBER,
40 PHASE_READ_WIDTH,
41 PHASE_READ_HEIGHT,
42 PHASE_READ_DEPTH,
43 };
44
45 typedef unsigned char u8;
46 typedef unsigned short u16;
47
usage(void)48 static void usage(void)
49 {
50 fprintf(stderr, "Usage: ppmconv [ppm file]\n");
51 exit(1);
52 }
53
54 /*---------------------------------------------------------------------------*
55 Name: ReadHeader
56
57 Description: Read header information of ppm file
58
59 Arguments: fp ppm file
60 pwidth output ptr for the width of ppm picture
61 pheight output ptr for the height of ppm picture
62 pdepth output ptr for the depth of ppm picture
63
64 Returns: 1 if success, 0 if error
65 *---------------------------------------------------------------------------*/
ReadHeader(FILE * fp,int * pwidth,int * pheight,int * pdepth)66 static int ReadHeader(FILE * fp, int *pwidth, int *pheight, int *pdepth)
67 {
68 // Get ppm header
69 //
70 // P6[WS]256[WS]192[WS]255[WS]\n WS=Space,Tab,CR
71 //
72 char buffer[SIZE_READBUFFER], *p;
73 int n;
74 int phase = PHASE_READ_MAGIC_NUMBER;
75
76 while (buffer == fgets(buffer, sizeof(buffer), fp))
77 {
78 if (buffer[0] == '#') // comment
79 {
80 continue;
81 }
82
83 p = buffer;
84 while (*p != '\0')
85 {
86 switch (phase)
87 {
88 case PHASE_READ_MAGIC_NUMBER:
89 if (0 != sscanf(p, "P6%n", &n))
90 {
91 return FALSE; // not ppm file
92 }
93 phase = PHASE_READ_WIDTH;
94 break;
95
96 case PHASE_READ_WIDTH:
97 if (1 != sscanf(p, "%d%n", pwidth, &n))
98 {
99 return FALSE;
100 }
101 phase = PHASE_READ_HEIGHT;
102 break;
103
104 case PHASE_READ_HEIGHT:
105 if (1 != sscanf(p, "%d%n", pheight, &n))
106 {
107 return FALSE;
108 }
109 phase = PHASE_READ_DEPTH;
110 break;
111
112 case PHASE_READ_DEPTH:
113 if (1 != sscanf(p, "%d%n", pdepth, &n))
114 {
115 return FALSE;
116 }
117 return TRUE;
118
119 default:
120 break;
121 }
122
123 for (p += n; isspace(*p); p++)
124 {
125 // Do nothing
126 }
127 }
128 }
129 return FALSE;
130 }
131
132
133 /*---------------------------------------------------------------------------*
134 Name: ReadBody
135
136 Description: Read picture image
137
138 Arguments: fp ppm file
139 buffer output ptr for image
140 size buffer size
141
142 Returns: 1 if success, 0 if error
143 *---------------------------------------------------------------------------*/
ReadBody(FILE * fp,u8 * buffer,int size)144 static int ReadBody(FILE * fp, u8 *buffer, int size)
145 {
146 return size == fread(buffer, sizeof(u8), size, fp);
147 }
148
149
150 /*---------------------------------------------------------------------------*
151 Name: main
152
153 Description: output ppm image bitmap as the format link C source code
154 *---------------------------------------------------------------------------*/
main(int argc,char * argv[])155 int main(int argc, char *argv[])
156 {
157 FILE *fp;
158 u8 *buffer;
159 int result = 1;
160 int width;
161 int height;
162 int depth;
163 int size;
164 int column;
165 int i;
166 u16 color;
167
168 if (argc != 2)
169 {
170 usage();
171 }
172
173 if (NULL != (fp = fopen(argv[1], "rb")))
174 {
175 if (ReadHeader(fp, &width, &height, &depth) && depth == 255)
176 {
177 size = width * height * 3;
178 printf("\t/* %s: WIDTH=%d HEIGHT=%d DEPTH=%d */\n", argv[1], width, height, depth);
179
180 if (NULL != (buffer = (u8 *)malloc(size)))
181 {
182 if (ReadBody(fp, buffer, size))
183 {
184 column = 0;
185 for (i = 0; i < size; i += 3)
186 {
187 if (column % NUM_COLUMN == 0)
188 {
189 printf("\t");
190 }
191
192 color = RGB5551(buffer[i], buffer[i + 1], buffer[i + 2], 1);
193 printf("0x%04x,", color);
194
195 column++;
196 if (column % NUM_COLUMN == 0)
197 {
198 printf("\n");
199 }
200 }
201 result = 0;
202 }
203 }
204 free(buffer);
205 }
206 fclose(fp);
207 }
208
209 if (result)
210 {
211 fprintf(stderr, "Cannot convert file \"%s\".\n", argv[1]);
212 }
213
214 return result;
215 }
216