VGA image editing


Recommended Posts

Hello everyone,

don't know if it is of any interest but for those poor guys using windows (that don't have access to linux conversion tools) here is a small contribution to a simple image .dat generation process for ZPUino VGA with 8bit colors.

1) select your image

2) scale it to 160x120 pixels (almost any tools out there) and save in .png

3) convert the palette (Corel Photo-paint->Image->Convert to palette and select the attached .pal file)

4) save the new .png image

5) convert the .png in .raw ( Irfanview->Save As ->(raw))

6) rename to .dat

So I hope I saved you an hour or two.


Link to comment
Share on other sites

  • 8 months later...
  • 3 months later...

Pizus: if you're interested in porting the conversion tool to windows... just say :)


There's a dependency on libpng, that what makes things harder. The code itself is very simple.

#include <png.h>int width, height;png_byte color_type;png_byte bit_depth;png_structp png_ptr;png_infop info_ptr;int number_of_passes;png_bytep * row_pointers;#define COLOR_BYTES 2#define COLOR_WEIGHT_R 4#define COLOR_WEIGHT_G 4#define COLOR_WEIGHT_B 4#define COLOR_SHIFT_R (COLOR_WEIGHT_B+COLOR_WEIGHT_G)#define COLOR_SHIFT_G (COLOR_WEIGHT_#define COLOR_SHIFT_B 0int readpng(const char*name){    unsigned char header[8];    int y;    FILE *f = fopen(name, "rb");    if (!f) {        perror("fopen");        return -1;    }    fread(header, 1, 8, f);    if (png_sig_cmp(header, 0, 8)) {        fprintf(stderr,"Invalid PNG file");        fclose(f);        return -1;    }    /* initialize stuff */    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);    if (!png_ptr) {        fprintf(stderr,"Cannot read struct\n");        return -1;    }    info_ptr = png_create_info_struct(png_ptr);    if (!info_ptr) {        fprintf(stderr,"Cannot create struct\n");        return -1;    }    png_init_io(png_ptr, f);    png_set_sig_bytes(png_ptr, 8);    png_read_info(png_ptr, info_ptr);    width = png_get_image_width(png_ptr, info_ptr);    height = png_get_image_height(png_ptr, info_ptr);    color_type = png_get_color_type(png_ptr, info_ptr);    bit_depth = png_get_bit_depth(png_ptr, info_ptr);    number_of_passes = png_set_interlace_handling(png_ptr);    png_read_update_info(png_ptr, info_ptr);    row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);    for (y=0; y<height; y++)        row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));    png_read_image(png_ptr, row_pointers);    fclose(f);    return 0;}void process(){    FILE *outf;    outf=fopen("image.dat","wb");    if (NULL==outf)    {        perror("Cannot open file");        return;    }    if (png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_RGB) {        fprintf(stderr,"Unsupported format %d %d\n",                PNG_COLOR_TYPE_RGB, png_get_color_type(png_ptr, info_ptr));        return;    }    int x,y;    unsigned int v;    for (y=0; y<height; y++) {        png_byte* row = row_pointers[y];        for (x=0; x<width; x++) {            png_byte* ptr = &(row[x*3]);            /* printf("Pixel at position [ %d - %d ] has RGB values: %02x - %02x - %02x\n",             x, y, ptr[0], ptr[1], ptr[2]);*/            ptr[0] = (ptr[0] >> (8- COLOR_WEIGHT_R) );            ptr[1] = (ptr[1] >> (8-COLOR_WEIGHT_G));            ptr[2] = (ptr[2] >> (8-COLOR_WEIGHT_);            v = (ptr[0]<<(COLOR_SHIFT_R)) + (ptr[1]<<COLOR_SHIFT_G) + (ptr[2]<<COLOR_SHIFT_;#if COLOR_BYTES == 2            { unsigned char rv = (v>>8)&0xff;                fwrite(&rv,1,1,outf);            rv=v&0xff;            fwrite(&rv,1,1,outf);            }#else            { unsigned char rv = v&0xff;                fwrite(&rv,1,1,outf);            }#endif        }    }    fclose(outf);}int main(int argc,char **argv){    if (argc<2)        return -1;    if (readpng(argv[1])<0)        return -1;    if (width!=160 || height!=120) {        fprintf(stderr,"Image size not 160x120\n");        return -1;    }    process();    return 0;}


Link to comment
Share on other sites

  • 4 months later...
  • 2 months later...
  • 1 year later...


This topic is now archived and is closed to further replies.