source: thomson/tools/gfx2mo5/png2mo5.c@ 15d3e8a

main
Last change on this file since 15d3e8a was 15d3e8a, checked in by Adrien Destugues <pulkomandy@…>, 8 years ago

gfx2mo5: can now convert bitmap16 files.

git-svn-id: svn://localhost/thomson@68 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/* GFX2mo5 - png2mo5.c
2 * CloudStrife - 20080921
3 * PulkoMandy - 2012-2014
4 * Diffusé sous licence libre CeCILL v2
5 * Voir LICENCE
6 */
7
8#include <getopt.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <time.h>
12#include <png.h>
13#include "libraw2mo5.h"
14
15#include <assert.h>
16
17#define ERROR 1
18
19int main(int argc, char **argv)
20{
21 FILE *inFile, *outFile;
22 unsigned char *inBuffer, *outBuffer;
23
24 png_uint_32 width;
25 png_uint_32 height;
26 int bitdepth;
27 int colorType;
28
29 unsigned char header[8];
30 unsigned char is_png;
31
32 unsigned int y;
33 png_structp png_ptr;
34 png_infop info_ptr;
35 png_infop end_info;
36
37 png_bytep * ptrRow;
38 int pxsize;
39
40 char opt;
41 int fixup;
42 bool to;
43 bool bitmap16;
44
45 unsigned char thomheader[] = {
46 // Block 1 : address A7C0, 1 byte, select FORME
47 0x00, 0x00, 0x01, 0xA7, 0xC0, 0x51,
48 // Block 2 : address 0000, 1F40 bytes, FORME data
49 0x00, 0x1F, 0x40, 0x00, 0x00
50 };
51
52
53 // End marker block : type=255, size and address=0
54 static const unsigned char end[]={255,0,0,0,0};
55
56 if(argc < 3)
57 {
58 printf("Utilisation : %s [options] input_filename output_filename\n",argv[0]);
59 printf("Option -m s: Mode to use (bm16/40c)\n");
60 printf("Option -t: use TO transcoding.\n");
61 printf("Option -f n: use modified algorithm to avoid artifacts on some MO5 gate array versions. n is the index of the background color.\n");
62 exit(0);
63 }
64
65 fixup = -1;
66 to = false;
67 bitmap16 = false;
68
69 while((opt = getopt(argc, argv, "tf:m:")) != -1) {
70 switch(opt) {
71 case 't':
72 to = true;
73 thomheader[3] = 0xE7;
74 thomheader[4] = 0xC3;
75 thomheader[5] = 0x65;
76 thomheader[9] = 0x40;
77 break;
78 case 'f':
79 fixup = atoi(optarg);
80 break;
81 case 'm':
82 if (strcmp(optarg, "bm16") == 0)
83 bitmap16 = true;
84 }
85 }
86
87 inFile = fopen(argv[optind++],"rb");
88
89 if (inFile == NULL)
90 {
91 printf("Fichier Inexistant\n");
92 exit(1);
93 }
94
95 fread(header, 1, 8, inFile);
96 is_png = !png_sig_cmp(header, 0, 8);
97 if (!is_png)
98 {
99 printf("Ce n'est pas un png\n");
100 exit(2);
101 }
102
103 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL);
104 if (!png_ptr) return (ERROR);
105
106 info_ptr = png_create_info_struct(png_ptr);
107 if (!info_ptr)
108 {
109 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
110 return (ERROR);
111 }
112
113 end_info = png_create_info_struct(png_ptr);
114 if (!end_info)
115 {
116 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
117 return (ERROR);
118 }
119
120 if (setjmp(png_jmpbuf(png_ptr)))
121 {
122 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
123 fclose(inFile);
124 return (ERROR);
125 }
126
127 png_init_io(png_ptr, inFile);
128 png_set_sig_bytes(png_ptr, 8);
129
130 png_read_info(png_ptr, info_ptr);
131
132 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitdepth, &colorType, NULL, NULL, NULL);
133
134 if(!((colorType == PNG_COLOR_TYPE_GRAY) || (colorType == PNG_COLOR_TYPE_PALETTE)))
135 {
136 puts("Ce PNG n'est pas dans un format exploitable (niveaux de gris ou palette)");
137 return (ERROR);
138 }
139
140 if (bitdepth > 8)
141 {
142 puts("Ce PNG n'est pas dans un format exploitable (bitdepth = 1, 2 ou 4)");
143 return (ERROR);
144 }
145
146 png_set_packing(png_ptr); /* Convertir en mode 1 pixel par octets */
147 png_read_update_info(png_ptr, info_ptr);
148
149 inBuffer = (unsigned char*)malloc(width*height);
150 if (inBuffer == NULL)
151 {
152 printf("Allocation inBuffer raté\n");
153 exit(3);
154 }
155
156 ptrRow = (png_bytep*)malloc(sizeof(png_bytep)*height);
157 for(y = 0; y < height; y++)
158 {
159 ptrRow[y] = (inBuffer + width*y);
160 }
161
162 png_read_image(png_ptr, ptrRow);
163
164 if (bitmap16) {
165 if (width != 160) {
166 puts("Image not using the full screen width are not supported yet!");
167 return ERROR;
168 }
169 outBuffer = raw2bm16(inBuffer, height);
170 pxsize = width * height / 4;
171 } else {
172 if (width != 320) {
173 puts("Image not using the full screen width are not supported yet!");
174 return ERROR;
175 }
176 outBuffer = raw2mo5(inBuffer, height, fixup, to);
177 pxsize = width * height / 8;
178 }
179
180 thomheader[7] = pxsize >> 8;
181 thomheader[8] = pxsize;
182
183 outFile = fopen(argv[optind++], "wb");
184 fwrite(thomheader, 1, sizeof(thomheader), outFile);
185 //write forme data
186 fwrite(outBuffer, 1, pxsize, outFile);
187 --thomheader[5];
188 fwrite(thomheader, 1, sizeof(thomheader), outFile);
189 // write color data
190 fwrite(outBuffer+0x2000, 1, pxsize, outFile);
191 fwrite(end, 1, sizeof(end), outFile);
192
193
194 fclose(outFile);
195
196 png_read_end(png_ptr, end_info);
197 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
198
199 free(inBuffer);
200 inBuffer = NULL;
201
202 free(outBuffer);
203 outBuffer = NULL;
204
205 return 0;
206}
Note: See TracBrowser for help on using the repository browser.