Some reorganizing. imageconvert sprite support started.

This commit is contained in:
Scott Duensing 2024-01-19 20:17:23 -06:00
parent 0983eb73ab
commit 76ffdf3677
16 changed files with 418 additions and 216 deletions

5
.gitignore vendored
View file

@ -12,6 +12,7 @@ software/
*.elf *.elf
*.lst *.lst
*.hex *.hex
*.sym
*.o *.o
makemap makemap
@ -36,8 +37,8 @@ skia/
aseprite/ aseprite/
aseprite-source/ aseprite-source/
intelFPGA_lite/ intelFPGA_lite/
header/header /header
imageconvert/imageconvert /imageconvert
include/ include/
# Dumb QtCreator junk # Dumb QtCreator junk

View file

@ -22,12 +22,16 @@
# SOFTWARE. # SOFTWARE.
# #
pushd tools/shared
pushd header cc -c util.c
cc header.c -o header -lm
popd popd
pushd imageconvert pushd tools/header
cc imageconvert.c -o imageconvert -lm cc header.c ../shared/util.o -o ../../header -lm
popd
pushd tools/imageconvert
cc imageconvert.c ../shared/util.o -o ../../imageconvert -lm
popd popd

View file

@ -41,7 +41,7 @@ ${CLANG} -o ${PROJECT} ${PROJECT}.o f256.o
mv -f ${PROJECT} ${PROJECT}.bin mv -f ${PROJECT} ${PROJECT}.bin
${F256}/header/header \ ${F256}/header \
pgz 24 \ pgz 24 \
${PROJECT}.pgz \ ${PROJECT}.pgz \
${START} \ ${START} \

View file

@ -42,7 +42,7 @@ ${CLANG} -o ${PROJECT} ${PROJECT}.o f256.o
mv -f ${PROJECT} ${PROJECT}.bin mv -f ${PROJECT} ${PROJECT}.bin
${F256}/header/header \ ${F256}/header \
pgz 24 \ pgz 24 \
${PROJECT}.pgz \ ${PROJECT}.pgz \
${START} \ ${START} \

View file

@ -34,7 +34,7 @@ echo "__f256_start = ${START};" > ${SETTINGS}
mos-f256k-clang -o kuptest -Os kuptest.c mos-f256k-clang -o kuptest -Os kuptest.c
mv -f kuptest kuptest.bin mv -f kuptest kuptest.bin
${F256}/header/header \ ${F256}/header \
kup \ kup \
kuptest.bin kuptest.kup \ kuptest.bin kuptest.kup \
1 ${START} \ 1 ${START} \

View file

@ -41,7 +41,7 @@ ${CLANG} -o ${PROJECT} ${PROJECT}.o f256.o
mv -f ${PROJECT} ${PROJECT}.bin mv -f ${PROJECT} ${PROJECT}.bin
${F256}/header/header \ ${F256}/header \
pgz 24 \ pgz 24 \
${PROJECT}.pgz \ ${PROJECT}.pgz \
${START} \ ${START} \

View file

@ -41,7 +41,7 @@ ${CLANG} -o ${PROJECT} ${PROJECT}.o f256.o
mv -f ${PROJECT} ${PROJECT}.bin mv -f ${PROJECT} ${PROJECT}.bin
${F256}/header/header \ ${F256}/header \
pgz 24 \ pgz 24 \
${PROJECT}.pgz \ ${PROJECT}.pgz \
${START} \ ${START} \

View file

@ -41,7 +41,7 @@ ${CLANG} -o ${PROJECT} ${PROJECT}.o f256.o
mv -f ${PROJECT} ${PROJECT}.bin mv -f ${PROJECT} ${PROJECT}.bin
${F256}/header/header \ ${F256}/header \
pgz 24 \ pgz 24 \
${PROJECT}.pgz \ ${PROJECT}.pgz \
${START} \ ${START} \

View file

@ -38,24 +38,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "../../tools/shared/util.h"
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 main(int argc, char *argv[]) {
@ -78,7 +61,7 @@ int main(int argc, char *argv[]) {
return 2; return 2;
} }
filename = replaceExtension(argv[1], ".tiles"); filename = utilReplaceExtension(argv[1], ".tiles");
printf("Creating %s...\n", filename); printf("Creating %s...\n", filename);
out = fopen(filename, "wb"); out = fopen(filename, "wb");
if (out == NULL) { if (out == NULL) {

View file

@ -29,16 +29,36 @@
#define OFF_SPR_POS_Y_L 6 #define OFF_SPR_POS_Y_L 6
void spriteDefine(byte s, uint32_t address, byte size, byte CLUT, byte layer) { static byte _spriteCtl[64];
uint16_t sprite = VKY_SP0_CTRL + (s << 3);
POKE(sprite, (size << 5) | (layer << 3) | (CLUT << 1));
void spriteDefine(byte s, uint32_t address, byte size, byte CLUT, byte layer) {
uint16_t sprite = VKY_SP0_CTRL + (s * 8);
byte sz;
switch (size) {
case 8:
sz = 3;
break;
case 16:
sz = 2;
break;
case 24:
sz = 1;
break;
case 32:
sz = 0;
break;
}
_spriteCtl[s] = (sz << 5) | (layer << 3) | (CLUT << 1);
POKE(sprite, _spriteCtl[s]);
POKEA(sprite + OFF_SPR_ADL_L, address); POKEA(sprite + OFF_SPR_ADL_L, address);
} }
void spriteSetPosition(byte s, uint16_t x, uint16_t y) { void spriteSetPosition(byte s, uint16_t x, uint16_t y) {
uint16_t sprite = VKY_SP0_CTRL + (s << 3); uint16_t sprite = VKY_SP0_CTRL + (s * 8);
POKEW(sprite + OFF_SPR_POS_X_L, x); POKEW(sprite + OFF_SPR_POS_X_L, x);
POKEW(sprite + OFF_SPR_POS_Y_L, y); POKEW(sprite + OFF_SPR_POS_Y_L, y);
@ -46,9 +66,9 @@ void spriteSetPosition(byte s, uint16_t x, uint16_t y) {
void spriteSetVisible(byte s, bool v) { void spriteSetVisible(byte s, bool v) {
uint16_t sprite = VKY_SP0_CTRL + (s << 3); uint16_t sprite = VKY_SP0_CTRL + (s * 8);
POKE(sprite, (PEEK(sprite) & 0xfe) | v); POKE(sprite, _spriteCtl[s] | v);
} }

View file

@ -1,167 +0,0 @@
/*
* 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;
}

View file

@ -26,25 +26,16 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h>
#include <math.h> #include <math.h>
#include "../shared/util.h"
/* /*
* This program assumes it is being run on a little endian machine. * This program assumes it is being run on a little endian machine.
*/ */
int utilStricmp(char *a, char *b) {
for (;; a++, b++) {
int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
if (d != 0 || !*a) {
return d;
}
}
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
FILE *in = NULL; FILE *in = NULL;
FILE *out = NULL; FILE *out = NULL;

View file

@ -0,0 +1,236 @@
/*
* 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 <stdbool.h>
#define STB_IMAGE_IMPLEMENTATION
#include "../shared/stb_image.h"
#include "../shared/util.h"
int writeImage(byte *data, colorT *palette, int colors, char *filename, int xs, int ys, int width, int height) {
int x;
int y;
int n;
byte *p;
colorT c;
FILE *out;
printf("Writing %dx%d indexed image %s...\n", width, height, filename);
out = fopen(filename, "wb");
if (out == NULL) {
printf("Unable to write %s. Aborting.\n", filename);
return 5;
}
for (y=ys; y<ys+height; y++) {
for (x=xs; x<xs+width; x++) {
p = &data[(y * width + x) * 3];
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;
}
}
if (n == colors) {
printf("Color (%d, %d, %d) not found at %dx%d! Aborting.\n", c.r, c.g, c.b, x, y);
printf("Palette:\n");
for (n=0; n<colors; n++) {
printf("Color %d = (%d, %d, %d)\n", n, palette[n].r, palette[n].g, palette[n].b);
}
return 9;
}
}
}
fclose(out);
return 0;
}
int main(int argc, char *argv[]) {
int x;
int y;
int w;
int h;
int i;
int j;
int n;
int colors = 0;
byte found;
byte *data;
byte *p;
byte spriteSize = 0;
colorT c;
colorT palette[256];
char *filename;
char *temp;
FILE *out;
char *inputImage;
char *outputType;
bool okay;
if (argc < 3 || argc > 4) {
printf("Usage: %s [type] [imagefile]\n", argv[0]);
printf("Where: [type] is the output type:\n");
printf(" - image (creates CLUT and indexed image)\n");
printf(" - sprite (creates CLUT and indexed sprites)\n");
return 1;
}
outputType = argv[1];
inputImage = argv[2];
// Are we making sprites?
if (utilStricmp(outputType, "sprite") == 0) {
okay = false;
if (argc == 4) {
inputImage = argv[3];
spriteSize = atoi(argv[2]);
if (spriteSize == 8 || spriteSize == 16 || spriteSize == 24 || spriteSize == 32) {
okay = true;
}
}
if (!okay) {
printf("Usage: %s sprite [size] [imagefile]\n", argv[0]);
printf("Where: [size] is 8, 16, 24, or 32\n");
return 6;
}
}
// Load image, check for valid size.
data = stbi_load(inputImage, &w, &h, &n, 3);
if (data == NULL) {
printf("Unable to load %s!\n", inputImage);
return 2;
}
if (spriteSize > 0) {
// Sprites must be multiples of 8, 16, 24, or 32.
okay = false;
if (((int)(w / 8) == (w / 8.0)) && ((int)(h / 8) == (h / 8.0))) okay = true;
if (((int)(w / 16) == (w / 16.0)) && ((int)(h / 16) == (h / 16.0))) okay = true;
if (((int)(w / 24) == (w / 24.0)) && ((int)(h / 24) == (h / 24.0))) okay = true;
if (((int)(w / 32) == (w / 32.0)) && ((int)(h / 32) == (h / 32.0))) okay = true;
if (!okay) {
printf("Height and width must be divisible 8, 16, 24, or 32!\n");
return 8;
}
} else {
// Images are more lax - we'll let anything of the right width through.
if (w != 320) {
free(data);
printf("Images must be 320 pixels in width!\n");
return 7;
}
}
// 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 = utilReplaceExtension(inputImage, ".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);
if (spriteSize == 0) {
// Write out raw indexed image.
filename = utilReplaceExtension(inputImage, ".indexed");
n = writeImage(data, palette, colors, filename, 0, 0, w, h);
free(filename);
if (n != 0) {
free(data);
return n;
}
} else {
// Split image into multiple sprite images.
temp = utilReplaceExtension(inputImage, "");
i = 1;
j = 1;
for (y=0; y<h; y+=spriteSize) {
for (x=0; x<w; x+=spriteSize) {
filename = utilCreateString("%s-%dx%d.indexed", temp, i++, j);
printf("Writing from %dx%d to %dx%d for %d bytes\n", x, y, x+spriteSize, y+spriteSize, spriteSize*spriteSize);
n = writeImage(data, palette, colors, filename, x, y, spriteSize, spriteSize);
free(filename);
if (n != 0) {
free(temp);
free(data);
return n;
}
}
i = 1;
j++;
}
free(temp);
}
free(data);
printf("Finished!\n");
return 0;
}

91
tools/shared/util.c Normal file
View file

@ -0,0 +1,91 @@
/*
* 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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "util.h"
char *utilCreateString(char *format, ...) {
va_list args;
char *string;
va_start(args, format);
string = utilCreateStringVArgs(format, args);
va_end(args);
return string;
}
__attribute__((__format__(__printf__, 1, 0)))
char *utilCreateStringVArgs(char *format, va_list args) {
va_list argsCopy;
int32_t size = 0;
char *buffer = NULL;
va_copy(argsCopy, args);
size = vsnprintf(NULL, 0, format, argsCopy) + 1;
va_end(argsCopy);
buffer = calloc(1, (size_t)size);
if (buffer) {
vsnprintf(buffer, (size_t)size, format, args);
}
return buffer;
}
char *utilReplaceExtension(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 utilStricmp(char *a, char *b) {
for (;; a++, b++) {
int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
if (d != 0 || !*a) {
return d;
}
}
}

43
tools/shared/util.h Normal file
View file

@ -0,0 +1,43 @@
/*
* 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.
*/
#ifndef UTIL_H
#define UTIL_H
typedef unsigned char byte;
typedef struct colorS {
byte r;
byte g;
byte b;
} colorT;
char *utilCreateString(char *format, ...);
char *utilCreateStringVArgs(char *format, va_list args);
char *utilReplaceExtension(char *org, char *new_ext);
int utilStricmp(char *a, char *b);
#endif // UTIL_H