updated URL of forum and table in readme file
[neverball] / share / image.c
index b5af04d..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 "config.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);
-
-    glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, buf->pixels);
+    unsigned char *p = NULL;
 
-    for (i = 0; i < h; i++)
-        memcpy((GLubyte *) img->pixels + 3 * w * i,
-               (GLubyte *) buf->pixels + 3 * w * (h - i), 3 * w);
-
-    SDL_SaveBMP(img, filename);
-
-    SDL_FreeSurface(img);
-    SDL_FreeSurface(buf);
-}
-
-void image_size(int *W, int *H, int w, int h)
-{
-    *W = 1;
-    *H = 1;
+    /* Initialize all PNG export data structures. */
 
-    while (*W < w) *W *= 2;
-    while (*H < h) *H *= 2;
-}
+    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. */
 
-void image_swab(SDL_Surface *src)
-{
-    int i, j, b = (src->format->BitsPerPixel == 32) ? 4 : 3;
-    
-    SDL_LockSurface(src);
+    if (setjmp(png_jmpbuf(writep)) == 0)
     {
-        unsigned char *s = (unsigned char *) src->pixels;
-        unsigned char  t;
+        /* Initialize the PNG header. */
 
-        /* Iterate over each pixel of the image. */
+        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);
 
-        for (i = 0; i < src->h; i++)
-            for (j = 0; j < src->w; j++)
-            {
-                int k = (i * src->w + j) * b;
+        /* Allocate the pixel buffer and copy pixels there. */
 
-                /* Swap the red and blue channels of each. */
+        if ((p = (unsigned char *) malloc(w * h * 4)))
+        {
+            glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, p);
 
-                t        = s[k + 2];
-                s[k + 2] = s[k + 0];
-                s[k + 0] =        t;
-            }
-    }
-    SDL_UnlockSurface(src);
-}
+            /* Allocate and initialize the row pointers. */
 
-void image_white(SDL_Surface *src)
-{
-    int i, j, b = (src->format->BitsPerPixel == 32) ? 4 : 3;
-    
-    SDL_LockSurface(src);
-    {
-        unsigned char *s = (unsigned char *) src->pixels;
+            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);
 
-        /* Iterate over each pixel of the image. */
+                png_set_rows (writep, infop, bytep);
 
-        for (i = 0; i < src->h; i++)
-            for (j = 0; j < src->w; j++)
-            {
-                int k = (i * src->w + j) * b;
+                /* Write the PNG image file. */
 
-                /* Whiten the RGB channels without touching any Alpha. */
+                png_write_info(writep, infop);
+                png_write_png (writep, infop, 0, NULL);
 
-                s[k + 0] = 0xFF;
-                s[k + 1] = 0xFF;
-                s[k + 2] = 0xFF;
+                free(bytep);
             }
+            free(p);
+        }
     }
-    SDL_UnlockSurface(src);
-}
-
-SDL_Surface *image_scale(SDL_Surface *src, int n)
-{
-    int si, di;
-    int sj, dj;
-    int k, b = (src->format->BitsPerPixel == 32) ? 4 : 3;
-
-    SDL_Surface *dst = SDL_CreateRGBSurface(SDL_SWSURFACE,
-                                            src->w / n,
-                                            src->h / n,
-                                            src->format->BitsPerPixel,
-                                            src->format->Rmask,
-                                            src->format->Gmask,
-                                            src->format->Bmask,
-                                            src->format->Amask);
-    if (dst)
-    {
-        SDL_LockSurface(src);
-        SDL_LockSurface(dst);
-        {
-            unsigned char *s = (unsigned char *) src->pixels;
-            unsigned char *d = (unsigned char *) dst->pixels;
-
-            /* Iterate each component of each distination pixel. */
-
-            for (di = 0; di < src->h / n; di++)
-                for (dj = 0; dj < src->w / n; dj++)
-                    for (k = 0; k < b; k++)
-                    {
-                        int c = 0;
 
-                        /* Average the NxN source pixel block for each. */
+    /* Release all resources. */
 
-                        for (si = di * n; si < (di + 1) * n; si++)
-                            for (sj = dj * n; sj < (dj + 1) * n; sj++)
-                                c += s[(si * src->w + sj) * b + k];
+    png_destroy_write_struct(&writep, &infop);
+    fclose(filep);
+}
 
-                        d[(di * dst->w + dj) * b + k] =
-                            (unsigned char) (c / (n * n));
-                    }
-        }
-        SDL_UnlockSurface(dst);
-        SDL_UnlockSurface(src);
-    }
+void image_size(int *W, int *H, int w, int h)
+{
+    *W = 1;
+    *H = 1;
 
-    return dst;
+    while (*W < w) *W *= 2;
+    while (*H < h) *H *= 2;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -238,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)))
         {
@@ -281,7 +228,7 @@ GLuint make_image_from_font(int *W, int *H,
 
     if (text && strlen(text) > 0)
     {
-        if ((src = TTF_RenderText_Blended(font, text, fg)))
+        if ((src = TTF_RenderUTF8_Blended(font, text, fg)))
         {
             int w2;
             int h2;
@@ -292,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)))
             {