f256/imageconvert/imageconvert.c
2024-01-15 16:08:12 -06:00

167 lines
3.7 KiB
C

/*
* Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
typedef unsigned char byte;
typedef struct colorS {
byte r;
byte g;
byte b;
} colorT;
char *replaceExtension(char *org, char *new_ext) {
char *ext;
char *tmp = strdup(org);
size_t newSize;
char *newName;
ext = strrchr(tmp , '.');
if (ext) *ext = 0;
newSize = strlen(tmp) + strlen(new_ext) + 1;
newName = malloc(newSize);
sprintf(newName, "%s%s", tmp, new_ext);
free(tmp);
return newName;
}
int main(int argc, char *argv[]) {
int x;
int y;
int w;
int h;
int n;
int colors = 0;
byte found;
byte *data;
byte *p;
colorT c;
colorT palette[256];
char *filename;
FILE *out;
if (argc != 2) {
printf("Usage: %s [imagefile]\n", argv[0]);
return 1;
}
data = stbi_load(argv[1], &w, &h, &n, 3);
if (data == NULL) {
printf("Unable to load %s!\n", argv[1]);
return 2;
}
// Build palette.
printf("Building palette...\n");
p = data;
for (y=0; y<h; y++) {
for (x=0; x<w; x++) {
c.r = *p++;
c.g = *p++;
c.b = *p++;
found = 0;
for (n=0; n<colors; n++) {
if ((palette[n].r == c.r) && (palette[n].g == c.g) && (palette[n].b == c.b)) {
found = 1;
break;
}
}
if (found == 0) {
palette[colors].r = c.r;
palette[colors].g = c.g;
palette[colors].b = c.b;
colors++;
if (colors > 256) {
free(data);
printf("More than 256 colors in the image. Aborting.\n");
return 3;
}
}
}
}
// Write out F256 CLUT.
filename = replaceExtension(argv[1], ".clut");
printf("Writing %d color palette %s...\n", colors, filename);
out = fopen(filename, "wb");
if (out == NULL) {
printf("Unable to write %s. Aborting.\n", filename);
free(data);
free(filename);
return 4;
}
for (n=0; n<colors; n++) {
fputc(palette[n].b, out);
fputc(palette[n].g, out);
fputc(palette[n].r, out);
fputc(0xff, out);
}
fclose(out);
free(filename);
// Write out raw indexed image.
filename = replaceExtension(argv[1], ".indexed");
printf("Writing %dx%d indexed image %s...\n", w, h, filename);
out = fopen(filename, "wb");
if (out == NULL) {
printf("Unable to write %s. Aborting.\n", filename);
free(data);
free(filename);
return 5;
}
p = data;
for (y=0; y<h; y++) {
for (x=0; x<w; x++) {
c.r = *p++;
c.g = *p++;
c.b = *p++;
for (n=0; n<colors; n++) {
if ((palette[n].r == c.r) && (palette[n].g == c.g) && (palette[n].b == c.b)) {
fputc(n, out);
break;
}
}
}
}
fclose(out);
free(filename);
free(data);
printf("Finished!\n");
return 0;
}