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