Continuous distribution
[clouds.git] / debug.hpp
index debe65e6eab44875f8ed97e02b8f837ae6a8aeaf..6774a1a66c6724fc0e9d35c0516e23dd28e2c5e5 100644 (file)
--- a/debug.hpp
+++ b/debug.hpp
@@ -20,7 +20,7 @@ inline void dump(glm::vec2 v) { fprintf(stderr, "{%f,%f}\n", v.x, v.y); }
 typedef struct {
   char head[12];
   short dx /* Width */, dy /* Height */, head2;
-  char pic[600 * 400][3];
+  char pic[600 * 400 * 3];
 } TGA;
 char tgahead[12] = {0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
@@ -33,9 +33,36 @@ void saveFBO() {
   tga->dy = height;
   tga->head2 = 0x2018;
   // TODO: flip -- the image is upside down so far
-  glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, tga->pic[0]);
+  glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, tga->pic);
   FILE *cc = fopen("fbo.tga", "wb");
   fwrite(tga, 1, (18 + 3 * width * height), cc);
   fclose(cc);
   free(tga);
 }
+
+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);
+}