-/*
+/*
* 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_config.h"
+#include "base_image.h"
+#include "config.h"
/*---------------------------------------------------------------------------*/
void image_snap(char *filename, int w, int h)
{
- 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);
+ FILE *filep = NULL;
+ png_structp writep = NULL;
+ png_infop infop = NULL;
+ png_bytep *bytep = NULL;
- glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, buf->pixels);
-
- for (i = 0; i < h; i++)
- memcpy((GLubyte *) img->pixels + 3 * w * i,
- (GLubyte *) buf->pixels + 3 * w * (h - i), 3 * w);
+ int i;
- SDL_SaveBMP(img, filename);
+ unsigned char *p = NULL;
- 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;
}
/*---------------------------------------------------------------------------*/
*/
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);
if (h) *h = src->h;
/* Create a new destination surface. */
-
+
if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
RMASK, GMASK, BMASK, AMASK)))
{
if (h) *h = src->h;
/* Create a new destination surface. */
-
+
if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
RMASK, GMASK, BMASK, AMASK)))
{