Skip to content

Commit

Permalink
tga + python inline code export
Browse files Browse the repository at this point in the history
  • Loading branch information
counter185 committed Jun 2, 2024
1 parent 7d112c2 commit b181457
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
60 changes: 60 additions & 0 deletions freesprite/FileIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,37 @@ bool writeCaveStoryPBM(PlatformNativePathString path, Layer* data) {
return false;
}

bool writeTGA(PlatformNativePathString path, Layer* data) {
FILE* nfile = platformOpenFile(path, PlatformFileModeWB);
if (nfile != NULL) {
//copilot hallucinated all of this code i don't know if it even works
uint8_t header[18];
memset(header, 0, 18);
header[2] = 2; // uncompressed RGB
header[12] = data->w & 0xff; // width
header[13] = (data->w >> 8) & 0xff;
header[14] = data->h & 0xff; // height
header[15] = (data->h >> 8) & 0xff;
header[16] = 24; // 24 bit bitmap
header[17] = 0x20; // top-down, non-interlaced
fwrite(header, 18, 1, nfile);
for (int y = 0; y < data->h; y++) {
for (int x = 0; x < data->w; x++) {
uint32_t pxx = data->getPixelAt(XY{ x,y });
uint8_t px[3];
px[0] = pxx & 0xff;
px[1] = (pxx >> 8) & 0xff;
px[2] = (pxx >> 16) & 0xff;
fwrite(px, 3, 1, nfile);
}
}

fclose(nfile);
return true;
}
return false;
}

bool writeCHeader(PlatformNativePathString path, Layer* data)
{
FILE* outfile = platformOpenFile(path, PlatformFileModeWB);
Expand Down Expand Up @@ -1577,3 +1608,32 @@ bool writeCHeader(PlatformNativePathString path, Layer* data)
}
return false;
}

bool writePythonNPArray(PlatformNativePathString path, Layer* data)
{
FILE* outfile = platformOpenFile(path, PlatformFileModeWB);
if (outfile != NULL) {
fprintf(outfile, "import numpy as np\n\n");
fprintf(outfile, "#from PIL import Image\n\n");
uint64_t dataPixelPointer = 0;
fprintf(outfile, "voidsprite_image = np.array([\n");
for (int y = 0; y < data->h; y++){
fprintf(outfile, " [");
for (int x = 0; x < data->w; x++){
uint8_t b = data->pixelData[dataPixelPointer++];
uint8_t g = data->pixelData[dataPixelPointer++];
uint8_t r = data->pixelData[dataPixelPointer++];
uint8_t a = data->pixelData[dataPixelPointer++];
fprintf(outfile, "[%i,%i,%i,%i],", r,g,b,a);
}
fprintf(outfile, "],\n");
}
fprintf(outfile, "], dtype=np.uint8)\n\n\n");

fprintf(outfile, "#pilimage = Image.fromarray(voidsprite_image, mode='RGBA')");

fclose(outfile);
return true;
}
return false;
}
8 changes: 8 additions & 0 deletions freesprite/FileIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ bool writeVOIDSNv2(PlatformNativePathString path, MainEditor* editor);
bool writeOpenRaster(PlatformNativePathString path, MainEditor* data);
bool writeXYZ(PlatformNativePathString path, Layer* data);
bool writeBMP(PlatformNativePathString path, Layer* data);
bool writeTGA(PlatformNativePathString path, Layer* data);
bool writeCaveStoryPBM(PlatformNativePathString path, Layer* data);
bool writeCHeader(PlatformNativePathString path, Layer* data);
bool writePythonNPArray(PlatformNativePathString path, Layer* data);

struct FileSessionImportNPath {
std::string name;
Expand Down Expand Up @@ -91,11 +93,17 @@ inline std::vector<FileExportFlatNPath> g_fileExportersFlatNPaths = {
{
"BMP (EasyBMP)", ".bmp", &writeBMP
},
{
"TGA (voidsprite custom)", ".tga", &writeTGA
},
{
"CaveStory PBM (EasyBMP)", ".pbm", &writeCaveStoryPBM
},
{
"C Header (voidsprite custom)", ".h", &writeCHeader
},
{
"Python NumPy array (voidsprite custom)", ".py", &writePythonNPArray
}
};
inline std::vector<FileExportMultiLayerNPath> g_fileExportersMLNPaths = {
Expand Down

0 comments on commit b181457

Please sign in to comment.