Voilà, c'est un tout pitit programme pour Mac, son but est de convertir les fichiers .png en .mr<br />Ca sera une première dans le monde Mac pour celui qui le réalise !!!!<br />Vla la source PC ( c'était un fichier en .c unique )<br />
/* <br /> * pngtomr - convert pngs into files for use in a dreamcast ip.bin<br /> *<br /> * size should be 320x90 or less, colors must be less than 128<br /> * output file must be less than 8192 bytes to fit in a "normal" ip.bin<br /> *<br /> * adk / napalm 2001 <br /> *<br /> * andrewk@napalm-x.com<br /> *<br /> * http://www.napalm-x.com<br /> *<br /> *<br /> * This program is free software; you can redistribute it and/or modify<br /> * it under the terms of the GNU General Public License as published by<br /> * the Free Software Foundation; either version 2 of the License, or<br /> * (at your option) any later version.<br /> *<br /> * This program is distributed in the hope that it will be useful,<br /> * but WITHOUT ANY WARRANTY; without even the implied warranty of<br /> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br /> * GNU General Public License for more details.<br /> *<br /> * You should have received a copy of the GNU General Public License<br /> * along with this program; if not, write to the Free Software<br /> * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.<br /> *<br /> */<br /><br />#include "png.h"<br /><br />typedef struct {<br /> int width;<br /> int height;<br /> char *data;<br />} image_t;<br /><br />typedef struct {<br /> unsigned char r;<br /> unsigned char g;<br /> unsigned char b;<br />} color_t;<br /><br />typedef struct {<br /> color_t color[128];<br /> int count;<br />} palette_t;<br /><br />typedef struct {<br /> unsigned int size;<br /> unsigned int offset;<br /> unsigned int width;<br /> unsigned int height;<br /> unsigned int colors;<br /> char *data;<br />} mr_t;<br /><br />int read_png(char *file_name, image_t *image) /* We need to open the file */<br />{<br /> png_structp png_ptr;<br /> png_infop info_ptr;<br /> unsigned int sig_read = 0;<br /> png_uint_32 width, height, row;<br /> int bit_depth, color_type, interlace_type;<br /> FILE *fp;<br /> png_color_16 *image_background;<br /><br /> if ((fp = fopen(file_name, "rb")) == NULL)<br /> return 0;<br /><br /> png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,<br /> NULL, NULL, NULL);<br /><br /> if (png_ptr == NULL)<br /> {<br /> fclose(fp);<br /> return 0;<br /> }<br /><br /> info_ptr = png_create_info_struct(png_ptr);<br /> if (info_ptr == NULL)<br /> {<br /> fclose(fp);<br /> png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);<br /> return 0;<br /> }<br /><br /> if (setjmp(png_ptr->jmpbuf))<br /> {<br /> png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);<br /> fclose(fp);<br /> return 0;<br /> }<br /><br /> png_init_io(png_ptr, fp);<br /><br /> png_set_sig_bytes(png_ptr, sig_read);<br /><br /> png_read_info(png_ptr, info_ptr);<br /><br /> png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,<br /> &interlace_type, NULL, NULL);<br /><br /> image->width = width;<br /> image->height = height;<br /><br /> image->data = (char *)malloc(width*height*4);<br /><br /> /* tell libpng to strip 16 bit/color files down to 8 bits/color */<br /> png_set_strip_16(png_ptr);<br /><br /> /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single<br /> * byte into separate bytes (useful for paletted and grayscale images).<br /> */<br /> png_set_packing(png_ptr);<br /><br /> /* Expand paletted colors into true RGB triplets */<br /> if (color_type == PNG_COLOR_TYPE_PALETTE)<br /> png_set_expand(png_ptr);<br /><br /> /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */<br /> if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)<br /> png_set_expand(png_ptr);<br /><br /> /* Expand paletted or RGB images with transparency to full alpha channels<br /> * so the data will be available as RGBA quartets.<br /> */<br /> if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))<br /> png_set_expand(png_ptr);<br /><br /> if (png_get_bKGD(png_ptr, info_ptr, &image_background))<br /> png_set_background(png_ptr, image_background,<br /> PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);<br /><br /> /* Add filler (or alpha) byte (before/after each RGB triplet) */<br /> png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);<br /><br /> png_read_update_info(png_ptr, info_ptr);<br /><br /> {<br /> png_bytep row_pointers[height];<br /><br /> for (row = 0; row < height; row++)<br /> row_pointers[row] = image->data + image->width*4*row;<br /> <br /> png_read_image(png_ptr, row_pointers);<br /> }<br /><br /> png_read_end(png_ptr, info_ptr);<br /><br /> png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);<br /><br /> fclose(fp);<br /><br /> return 1;<br />}<br /><br />int mrcompress(char *in, char *out, int size)<br />{<br /> int length = 0;<br /> int position = 0;<br /> int run;<br /> <br /> while (position <= size) {<br /> <br /> run = 1;<br /><br /> while((in[position] == in[position+run]) && (run < 0x17f) && (position+run <= size)) {<br /> run++;<br /> }<br /> <br /> if (run > 0xff) {<br /> out[length++] = 0x82;<br /> out[length++] = 0x80 | (run - 0x100);<br /> out[length++] = in[position];<br /> } else if (run > 0x7f) {<br /> out[length++] = 0x81;<br /> out[length++] = run;<br /> out[length++] = in[position];<br /> } else if (run > 1) {<br /> out[length++] = 0x80 | run;<br /> out[length++] = in[position];<br /> } else<br /> out[length++] = in[position];<br /> <br /> position += run;<br /><br /> }<br /><br /> return length;<br /><br />}<br /><br /><br />int main(int argc, char *argv[])<br />{<br /> image_t image;<br /> palette_t palette;<br /> mr_t mr;<br /> int i;<br /> int *data;<br /> char *raw_output;<br /> char *compressed_output;<br /> FILE *output;<br /> int crap = 0;<br /> int compressed_size;<br /><br /> printf("pngtomr 0.1 by adk/napalm\n");<br /><br /> if (argc != 3) {<br /> printf("Usage: %s <input.png> <output.mr>\n",argv[0]);<br /> exit(0);<br /> }<br /><br /> if (read_png(argv[1],&image)) {<br /> printf("Loaded %s, %d x %d\n",argv[1],image.width,image.height);<br /> } else {<br /> printf("Error with %s\n",argv[1]);<br /> exit(0);<br /> }<br /> <br /> palette.count = 0;<br /><br /> data = (int *)image.data;<br /><br /> raw_output = (char *)malloc(image.width * image.height);<br /> compressed_output = (char *)malloc(image.width * image.height);<br /><br /> for(i=0; i<image.width*image.height; i++) {<br /> int found = 0;<br /> int c = 0;<br /><br /> while ((!found) && (c < palette.count))<br /> if (!memcmp(&data[i], &palette.color[c], 3))<br /> found = 1;<br /> else<br /> c++;<br /> if ((!found) && (c == 128)) {<br /> printf("Reduce the number of colors to <= 128 and try again.\n");<br /> exit(0);<br /> }<br /> if (!found) {<br /> memcpy(&palette.color[c], &data[i], 3);<br /> palette.count++;<br /> }<br /> raw_output[i] = c;<br /> }<br /><br /> printf("Found %d colors\n",palette.count);<br /><br /> mr.width = image.width;<br /> mr.height = image.height;<br /> mr.colors = palette.count;<br /><br /> printf("Compressing %d bytes to ",image.width*image.height); fflush(stdout);<br /><br /> compressed_size = mrcompress(raw_output, compressed_output, image.width*image.height);<br /><br /> printf("%d bytes.\n",compressed_size); fflush(stdout);<br /><br /> if (compressed_size <= 8192)<br /> printf("This will fit in a normal ip.bin.\n");<br /> else<br /> printf("This will NOT fit in a normal ip.bin - it is %d bytes too big!\n",compressed_size - 8192);<br /><br /> mr.offset = 2 + 7*4 + palette.count*4;<br /> mr.size = 2 + 7*4 + palette.count*4 + compressed_size;<br /><br /> printf("Writing %s\n",argv[2]);<br /><br /> output = fopen(argv[2], "wb");<br /><br /> if (!output) {<br /> printf("Cannot open %s\n",argv[2]);<br /> exit(0);<br /> }<br /><br /> fwrite("MR", 1, 2, output);<br /> fwrite(&mr.size, 1, 4, output);<br /> fwrite(&crap, 1, 4, output);<br /> fwrite(&mr.offset, 1, 4, output);<br /> fwrite(&mr.width, 1, 4, output);<br /> fwrite(&mr.height, 1, 4, output);<br /> fwrite(&crap, 1, 4, output);<br /> fwrite(&mr.colors, 1, 4, output);<br /><br /> for(i=0; i<palette.count; i++) {<br /> fwrite(&palette.color[i].b, 1, 1, output);<br /> fwrite(&palette.color[i].g, 1, 1, output);<br /> fwrite(&palette.color[i].r, 1, 1, output);<br /> fwrite(&crap, 1, 1, output);<br /> }<br /><br /> fwrite(compressed_output, compressed_size, 1, output);<br /> fclose(output);<br /><br /> printf("Done!\n");<br /><br /> exit(0);<br />}<br /><br /><br />
<br /><br /><br />Pensée particulière à eaglelouk@ et Sky :rolleyes: :D