Post Reply 
Converting Rom Art
09-16-2020, 10:52 PM (This post was last modified: 09-16-2020 10:53 PM by ijabbott.)
Post: #3
RE: Converting Rom Art
This might help. It uses libpng.

png2rom.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <png.h>

int main(int argc, char **argv)
{
    png_image image;
    png_bytep buffer;
    void *colormap;
    unsigned int imgsize;
    unsigned int rowstride;
    unsigned int numrows;
    unsigned int written;
    unsigned int row;
    FILE *fp;
    int rc;

    if (argc != 3) {
        fprintf(stderr, "usage: %s PNGFILEIN ROMFILEOUT\n", argv[0]);
        return 2;
    }

    /* Initialize the 'png_image' structure. */
    memset(&image, 0, sizeof image);
    image.version = PNG_IMAGE_VERSION;

    /* Begin to read the PNG file named by argv[1]. */
    if (png_image_begin_read_from_file(&image, argv[1]) == 0) {
        fprintf(stderr, "%s: error: %s\n", argv[1], image.message);
        return 1;
    }

    /* We expect to read color-mapped images. */
    image.format = PNG_FORMAT_RGB_COLORMAP;

    /* Image size is the ROM size. */
    rowstride = PNG_IMAGE_ROW_STRIDE(image);
    imgsize = PNG_IMAGE_BUFFER_SIZE(image, rowstride);
    numrows = image.height;

    /* Allocate memory to hold the image in this format. */
    buffer = malloc(imgsize);
    colormap = malloc(PNG_IMAGE_COLORMAP_SIZE(image));
    if (!buffer || !colormap) {
        fprintf(stderr, "Memory allocation error\n");
        free(buffer);
        free(colormap);
        png_image_free(&image);
        return 1;
    }

    if (png_image_finish_read(&image, NULL, buffer, 0, colormap) == 0) {
        fprintf(stderr, "%s: error: %s\n", argv[1], image.message);
        png_image_free(&image);
        free(buffer);
        free(colormap);
        return 1;
    }

    /* Write the ROM image named by argv[2]. */
    fp = fopen(argv[2], "wb");
    if (!fp) {
        perror(argv[2]);
        png_image_free(&image);
        free(buffer);
        free(colormap);
        return 1;
    }
    for (row = 0; row < numrows; row++) {
        png_bytep rowbuf = buffer + rowstride * (numrows - row - 1);

        written = fwrite(rowbuf, 1, rowstride, fp);
        if (written < rowstride) {
            break;
        }
    }

    if (row < numrows) {
        fprintf(stderr, "%s: Write error!\n", argv[2]);
    }
    rc = fclose(fp);
    if (rc == EOF) {
        perror(argv[2]);
    }
    png_image_free(&image);
    free(buffer);
    free(colormap);
    if (row < numrows || rc != 0) {
        return 1;
    }
    return 0;
}

— Ian Abbott
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Converting Rom Art - RPLman - 09-16-2020, 11:16 AM
RE: Converting Rom Art - Ross Barnes - 09-16-2020, 10:34 PM
RE: Converting Rom Art - ijabbott - 09-16-2020 10:52 PM
RE: Converting Rom Art - RPLman - 09-17-2020, 03:26 AM
RE: Converting Rom Art - Eric Rechlin - 09-17-2020, 03:07 PM
RE: Converting Rom Art - Massimo Gnerucci - 09-17-2020, 04:36 PM
RE: Converting Rom Art - Didier Lachieze - 09-17-2020, 06:10 PM
RE: Converting Rom Art - Ross Barnes - 09-17-2020, 06:18 PM
RE: Converting Rom Art - kwarda - 09-17-2020, 07:50 PM
RE: Converting Rom Art - aamiel - 09-21-2020, 07:54 AM
RE: Converting Rom Art - Eric Rechlin - 09-18-2020, 01:20 AM
RE: Converting Rom Art - RPLman - 09-25-2020, 02:29 PM
RE: Converting Rom Art - RPLman - 09-26-2020, 12:56 AM
RE: Converting Rom Art - RPLman - 09-26-2020, 10:17 AM
RE: Converting Rom Art - JanS - 10-24-2024, 09:32 PM



User(s) browsing this thread: 3 Guest(s)