Blame simple/neural/tga.inc.cpp
|
|
53488e |
#ifndef TGA_INC_CPP
|
|
|
53488e |
#define TGA_INC_CPP
|
|
|
53488e |
|
|
|
53488e |
|
|
|
53488e |
#include <cstdio></cstdio>
|
|
|
53488e |
|
|
|
53488e |
|
|
|
53488e |
bool tgaSave(const char *filename, const unsigned char *data, int w, int h, int ch) {
|
|
|
53488e |
if (!data || w <= 0 || h <= 0 || w > 0xffff || h > 0xffff || (ch != 3 && ch != 4)) {
|
|
|
53488e |
printf("ERROR: cannot save image (bad image): %s\n", filename);
|
|
|
53488e |
return false;
|
|
|
53488e |
}
|
|
|
53488e |
|
|
|
53488e |
FILE *f = fopen(filename, "wb");
|
|
|
53488e |
if (!f) {
|
|
|
53488e |
printf("ERROR: cannot open file: %s\n", filename);
|
|
|
53488e |
return false;
|
|
|
53488e |
}
|
|
|
53488e |
|
|
|
53488e |
#pragma pack(push,1)
|
|
|
53488e |
struct Header {
|
|
|
53488e |
unsigned char idLength;
|
|
|
53488e |
unsigned char colormapType;
|
|
|
53488e |
unsigned char imageType;
|
|
|
53488e |
unsigned char colormapIndex[2];
|
|
|
53488e |
unsigned char colormapLength[2];
|
|
|
53488e |
unsigned char colormapSize;
|
|
|
53488e |
unsigned char xOrigin[2];
|
|
|
53488e |
unsigned char yOrigin[2];
|
|
|
53488e |
unsigned char width[2];
|
|
|
53488e |
unsigned char height[2];
|
|
|
53488e |
unsigned char pixelSize;
|
|
|
53488e |
unsigned char attributes;
|
|
|
53488e |
};
|
|
|
53488e |
#pragma pack(pop)
|
|
|
53488e |
Header header = {};
|
|
|
53488e |
header.imageType = 2;
|
|
|
53488e |
header.width[0] = w;
|
|
|
53488e |
header.width[1] = w >> 8;
|
|
|
53488e |
header.height[0] = h;
|
|
|
53488e |
header.height[1] = h >> 8;
|
|
|
53488e |
header.pixelSize = ch == 4 ? 32 : 24;
|
|
|
53488e |
fwrite(&header, sizeof(header), 1, f);
|
|
|
53488e |
|
|
|
53488e |
int rowSize = w*ch;
|
|
|
53488e |
int size = h*rowSize;
|
|
|
53488e |
const unsigned char *row = data + size;
|
|
|
53488e |
for(unsigned short r = h; r; --r, row -= rowSize) {
|
|
|
53488e |
for(const unsigned char *c = row - rowSize; c < row; c += ch) {
|
|
|
53488e |
fputc(c[2], f);
|
|
|
53488e |
fputc(c[1], f);
|
|
|
53488e |
fputc(c[0], f);
|
|
|
53488e |
if (ch == 4) fputc(c[3], f);
|
|
|
53488e |
}
|
|
|
53488e |
}
|
|
|
53488e |
fclose(f);
|
|
|
53488e |
|
|
|
53488e |
return true;
|
|
|
53488e |
}
|
|
|
53488e |
|
|
|
53488e |
|
|
|
53488e |
#endif
|