Adrien Destugues | 99bc67a | 2012-03-16 06:56:04 +0000 | [diff] [blame] | 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 | |
| 16 | int 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 |
Adrien Destugues | cc8f3af | 2012-03-19 06:46:08 +0000 | [diff] [blame^] | 44 | const unsigned char end[]={255,0,0,0,0}; |
Adrien Destugues | 99bc67a | 2012-03-16 06:56:04 +0000 | [diff] [blame] | 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 | |
Adrien Destugues | cc8f3af | 2012-03-19 06:46:08 +0000 | [diff] [blame^] | 131 | int pxsize = width * height / 8; |
| 132 | thomheader[7] = pxsize >> 8; |
| 133 | thomheader[8] = pxsize; |
| 134 | |
Adrien Destugues | 99bc67a | 2012-03-16 06:56:04 +0000 | [diff] [blame] | 135 | outFile = fopen(argv[2], "wb"); |
| 136 | fwrite(thomheader, 1, sizeof(thomheader), outFile); |
| 137 | //write forme data |
Adrien Destugues | cc8f3af | 2012-03-19 06:46:08 +0000 | [diff] [blame^] | 138 | fwrite(outBuffer, 1, pxsize, outFile); |
Adrien Destugues | 99bc67a | 2012-03-16 06:56:04 +0000 | [diff] [blame] | 139 | thomheader[5] = 0x50; |
| 140 | fwrite(thomheader, 1, sizeof(thomheader), outFile); |
| 141 | // write color data |
Adrien Destugues | cc8f3af | 2012-03-19 06:46:08 +0000 | [diff] [blame^] | 142 | fwrite(outBuffer+0x2000, 1, pxsize, outFile); |
Adrien Destugues | 99bc67a | 2012-03-16 06:56:04 +0000 | [diff] [blame] | 143 | fwrite(end, 1, sizeof(end), outFile); |
| 144 | |
| 145 | |
| 146 | fclose(outFile); |
| 147 | |
| 148 | png_read_end(png_ptr, end_info); |
| 149 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 150 | |
| 151 | free(inBuffer); |
| 152 | inBuffer = NULL; |
| 153 | |
| 154 | free(outBuffer); |
| 155 | outBuffer = NULL; |
| 156 | |
| 157 | return 0; |
| 158 | } |