source: thomson/tools/gfx2mo5/png2mo5.c@ 99bc67a

main
Last change on this file since 99bc67a was 99bc67a, checked in by Adrien Destugues <pulkomandy@…>, 12 years ago

Add PNG to MO5 converter.

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

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/* GFX2mo5 - png2mo5.c
2 * CloudStrife - 20080921
3 * Diffusé sous licence libre CeCILL v2
4 * Voir LICENCE
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <png.h>
10#include "libraw2mo5.h"
11
12#include <assert.h>
13
14#define ERROR 1
15
16int main(int argc, char **argv)
17{
18 FILE *inFile, *outFile;
19 unsigned char *inBuffer, *outBuffer;
20
21 png_uint_32 width;
22 png_uint_32 height;
23 int bitdepth;
24 int colorType;
25
26 unsigned char header[8];
27 unsigned char is_png;
28
29 unsigned int y;
30 png_structp png_ptr;
31 png_infop info_ptr;
32 png_infop end_info;
33
34 png_bytep * ptrRow;
35 unsigned char thomheader[] = {
36 // Block 1 : address A7C0, 1 byte, select FORME
37 0x00, 0x00, 0x01, 0xA7, 0xC0, 0x51,
38 // Block 2 : address 0000, 1F40 bytes, FORME data
39 0x00, 0x1F, 0x40, 0x00, 0x00
40 };
41
42
43 // End marker block : type=255, size and address=0
44 const unsigned char end[]={255,0,0,0x30,0};
45
46 if(argc != 3)
47 {
48 printf("Utilisation : %s input_filename output_filename\n",argv[0]);
49 exit(0);
50 }
51
52 inFile = fopen(argv[1],"rb");
53
54 if (inFile == NULL)
55 {
56 printf("Fichier Inexistant\n");
57 exit(1);
58 }
59
60 fread(header, 1, 8, inFile);
61 is_png = !png_sig_cmp(header, 0, 8);
62 if (!is_png)
63 {
64 printf("Ce n'est pas un png\n");
65 exit(2);
66 }
67
68 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL);
69 if (!png_ptr) return (ERROR);
70
71 info_ptr = png_create_info_struct(png_ptr);
72 if (!info_ptr)
73 {
74 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
75 return (ERROR);
76 }
77
78 end_info = png_create_info_struct(png_ptr);
79 if (!end_info)
80 {
81 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
82 return (ERROR);
83 }
84
85 if (setjmp(png_jmpbuf(png_ptr)))
86 {
87 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
88 fclose(inFile);
89 return (ERROR);
90 }
91
92 png_init_io(png_ptr, inFile);
93 png_set_sig_bytes(png_ptr, 8);
94
95 png_read_info(png_ptr, info_ptr);
96
97 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitdepth, &colorType, NULL, NULL, NULL);
98
99 if(!((colorType == PNG_COLOR_TYPE_GRAY) || (colorType == PNG_COLOR_TYPE_PALETTE)))
100 {
101 puts("Ce PNG n'est pas dans un format exploitable (niveaux de gris ou palette)");
102 return (ERROR);
103 }
104
105 if (bitdepth > 8)
106 {
107 puts("Ce PNG n'est pas dans un format exploitable (bitdepth = 1, 2 ou 4)");
108 return (ERROR);
109 }
110
111 png_set_packing(png_ptr); /* Convertir en mode 1 pixel par octets */
112 png_read_update_info(png_ptr, info_ptr);
113
114 inBuffer = (unsigned char*)malloc(width*height);
115 if (inBuffer == NULL)
116 {
117 printf("Allocation inBuffer raté\n");
118 exit(3);
119 }
120
121 ptrRow = (png_bytep*)malloc(sizeof(png_bytep)*height);
122 for(y = 0; y < height; y++)
123 {
124 ptrRow[y] = (inBuffer + width*y);
125 }
126
127 png_read_image(png_ptr, ptrRow);
128
129 outBuffer = raw2mo5(inBuffer);
130
131 outFile = fopen(argv[2], "wb");
132 fwrite(thomheader, 1, sizeof(thomheader), outFile);
133 //write forme data
134 fwrite(outBuffer, 1, 0x1F40, outFile);
135 thomheader[5] = 0x50;
136 fwrite(thomheader, 1, sizeof(thomheader), outFile);
137 // write color data
138 fwrite(outBuffer+0x2000, 1, 0x1f40, outFile);
139 fwrite(end, 1, sizeof(end), outFile);
140
141
142 fclose(outFile);
143
144 png_read_end(png_ptr, end_info);
145 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
146
147 free(inBuffer);
148 inBuffer = NULL;
149
150 free(outBuffer);
151 outBuffer = NULL;
152
153 return 0;
154}
Note: See TracBrowser for help on using the repository browser.