+
+void saveImage(char *img, short width, short height, const char *file) {
+ TGA *tga = (TGA *)malloc(sizeof(TGA));
+ memcpy(tga->head, tgahead, 12);
+ tga->dx = width;
+ tga->dy = height;
+ tga->head2 = 0x2018;
+ memcpy(tga->pic, img, width * height * 3);
+ // TODO: flip -- the image is upside down so far
+ FILE *cc = fopen(file, "wb");
+ fwrite(tga, 1, (18 + 3 * width * height), cc);
+ fclose(cc);
+ free(tga);
+}
+
+void saveGrayscale(float *img, short width, short height, const char *file) {
+ char *bytes = (char*)malloc(sizeof(char) * width * height * 3);
+ for (int i = 0; i < width; i++) {
+ for (int j = 0; j < height; j++) {
+ for (int z = 0; z < 3; z++) {
+ bytes[i * 3 + j * width * 3 + z] = (int)(img[i + j * width] * 255.f * 2);
+ }
+ }
+ }
+ saveImage(bytes, width, height, file);
+ free(bytes);
+}