updated URL of forum and table in readme file
[neverball] / share / image.c
index 18f2992..3cbb83a 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * Copyright (C) 2003 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
 #include <SDL_image.h>
 #include <string.h>
 #include <math.h>
+#include <png.h>
+#include <stdlib.h>
 
 #include "glext.h"
 #include "image.h"
 #include "base_image.h"
-#include "base_config.h"
+#include "config.h"
 
 /*---------------------------------------------------------------------------*/
 
-void image_snap(char *filename, int w, int h)
+void image_snap(char *filename)
 {
+    FILE       *filep  = NULL;
+    png_structp writep = NULL;
+    png_infop   infop  = NULL;
+    png_bytep  *bytep  = NULL;
+
+    int w = config_get_d(CONFIG_WIDTH);
+    int h = config_get_d(CONFIG_HEIGHT);
     int i;
 
-    SDL_Surface *buf = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 24,
-                                            RMASK, GMASK, BMASK, 0);
-    SDL_Surface *img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 24,
-                                            RMASK, GMASK, BMASK, 0);
+    unsigned char *p = NULL;
+
+    /* Initialize all PNG export data structures. */
 
-    glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, buf->pixels);
+    if (!(filep = fopen(filename, FMODE_WB)))
+        return;
+    if (!(writep = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
+        return;
+    if (!(infop = png_create_info_struct(writep)))
+        return;
+
+    /* Enable the default PNG error handler. */
+
+    if (setjmp(png_jmpbuf(writep)) == 0)
+    {
+        /* Initialize the PNG header. */
 
-    for (i = 0; i < h; i++)
-        memcpy((GLubyte *) img->pixels + 3 * w * i,
-               (GLubyte *) buf->pixels + 3 * w * (h - i), 3 * w);
+        png_init_io (writep, filep);
+        png_set_IHDR(writep, infop, w, h, 8,
+                     PNG_COLOR_TYPE_RGB,
+                     PNG_INTERLACE_NONE,
+                     PNG_COMPRESSION_TYPE_DEFAULT,
+                     PNG_FILTER_TYPE_DEFAULT);
 
-    SDL_SaveBMP(img, filename);
+        /* Allocate the pixel buffer and copy pixels there. */
+
+        if ((p = (unsigned char *) malloc(w * h * 4)))
+        {
+            glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, p);
+
+            /* Allocate and initialize the row pointers. */
+
+            if ((bytep = png_malloc(writep, h * sizeof (png_bytep))))
+            {
+                for (i = 0; i < h; ++i)
+                    bytep[h - i - 1] = (png_bytep) (p + i * w * 3);
 
-    SDL_FreeSurface(img);
-    SDL_FreeSurface(buf);
+                png_set_rows (writep, infop, bytep);
+
+                /* Write the PNG image file. */
+
+                png_write_info(writep, infop);
+                png_write_png (writep, infop, 0, NULL);
+
+                free(bytep);
+            }
+            free(p);
+        }
+    }
+
+    /* Release all resources. */
+
+    png_destroy_write_struct(&writep, &infop);
+    fclose(filep);
 }
 
 void image_size(int *W, int *H, int w, int h)
@@ -64,8 +112,7 @@ void image_size(int *W, int *H, int w, int h)
  */
 GLuint make_image_from_surf(int *w, int *h, SDL_Surface *s)
 {
-    /*int    t = config_get_d(CONFIG_TEXTURES);*/
-    int    t = 0; /* TODO: Fint a way to revert CONFIG_TEXTURES */
+    int    t = config_get_d(CONFIG_TEXTURES);
     GLuint o = 0;
 
     glGenTextures(1, &o);
@@ -138,7 +185,7 @@ GLuint make_image_from_file(int *W, int *H,
         if (h) *h = src->h;
 
         /* Create a new destination surface. */
-        
+
         if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
                                         RMASK, GMASK, BMASK, AMASK)))
         {
@@ -192,7 +239,7 @@ GLuint make_image_from_font(int *W, int *H,
             if (h) *h = src->h;
 
             /* Create a new destination surface. */
-            
+
             if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
                                             RMASK, GMASK, BMASK, AMASK)))
             {