2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
24 #include "base_image.h"
27 /*---------------------------------------------------------------------------*/
29 void image_snap(const char *filename)
32 png_structp writep = NULL;
33 png_infop infop = NULL;
34 png_bytep *bytep = NULL;
36 int w = config_get_d(CONFIG_WIDTH);
37 int h = config_get_d(CONFIG_HEIGHT);
40 unsigned char *p = NULL;
42 /* Initialize all PNG export data structures. */
44 if (!(filep = fopen(filename, FMODE_WB)))
46 if (!(writep = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
48 if (!(infop = png_create_info_struct(writep)))
51 /* Enable the default PNG error handler. */
53 if (setjmp(png_jmpbuf(writep)) == 0)
55 /* Initialize the PNG header. */
57 png_init_io (writep, filep);
58 png_set_IHDR(writep, infop, w, h, 8,
61 PNG_COMPRESSION_TYPE_DEFAULT,
62 PNG_FILTER_TYPE_DEFAULT);
64 /* Allocate the pixel buffer and copy pixels there. */
66 if ((p = (unsigned char *) malloc(w * h * 3)))
68 glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, p);
70 /* Allocate and initialize the row pointers. */
72 if ((bytep = (png_bytep *) png_malloc(writep, h * sizeof (png_bytep))))
74 for (i = 0; i < h; ++i)
75 bytep[h - i - 1] = (png_bytep) (p + i * w * 3);
77 png_set_rows (writep, infop, bytep);
79 /* Write the PNG image file. */
81 png_write_info(writep, infop);
82 png_write_png (writep, infop, 0, NULL);
90 /* Release all resources. */
92 png_destroy_write_struct(&writep, &infop);
96 /*---------------------------------------------------------------------------*/
99 * Create an OpenGL texture object using the given image buffer.
101 static GLuint make_texture(const void *p, int w, int h, int b)
103 static const GLenum format[] =
104 { 0, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
108 /* Scale the image as configured, or to fit the OpenGL limitations. */
110 #ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
111 int a = config_get_d(CONFIG_ANISO);
113 int m = config_get_d(CONFIG_MIPMAP);
114 int k = config_get_d(CONFIG_TEXTURES);
122 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
124 while (w / k > (int) max || h / k > (int) max)
128 q = image_scale(p, w, h, b, &W, &H, k);
130 /* Generate and configure a new OpenGL texture. */
132 glGenTextures(1, &o);
133 glBindTexture(GL_TEXTURE_2D, o);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
141 #ifdef GL_GENERATE_MIPMAP_SGIS
144 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
146 GL_LINEAR_MIPMAP_LINEAR);
149 #ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
150 if (a) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, a);
153 /* Copy the image to an OpenGL texture. */
155 glTexImage2D(GL_TEXTURE_2D, 0,
157 format[b], GL_UNSIGNED_BYTE, q ? q : p);
166 * Load an image from the named file. Return an OpenGL texture object.
168 GLuint make_image_from_file(const char *filename)
176 /* Load the image. */
178 if ((p = image_load(config_data(filename), &w, &h, &b)))
180 o = make_texture(p, w, h, b);
187 /*---------------------------------------------------------------------------*/
190 * Render the given string using the given font. Transfer the image
191 * to a surface of power-of-2 size large enough to fit the string.
192 * Return an OpenGL texture object.
194 GLuint make_image_from_font(int *W, int *H,
196 const char *text, TTF_Font *font)
200 /* Render the text. */
202 if (text && strlen(text) > 0)
204 SDL_Color col = { 0xFF, 0xFF, 0xFF, 0xFF };
207 if ((orig = TTF_RenderUTF8_Blended(font, text, col)))
212 int b = orig->format->BitsPerPixel / 8;
224 if ((src = SDL_ConvertSurface(orig, &fmt, orig->flags)) == NULL)
226 fprintf(stderr, _("Failed to convert SDL_ttf surface: %s\n"),
229 /* Pretend everything's just fine. */
234 SDL_FreeSurface(orig);
236 /* Pad the text to power-of-two. */
238 p = image_next2(src->pixels, src->w, src->h, b, &w2, &h2);
245 /* Saturate the color channels. Modulate ONLY in alpha. */
247 image_white(p, w2, h2, b);
249 /* Create the OpenGL texture object. */
251 o = make_texture(p, w2, h2, b);
254 SDL_FreeSurface(src);
270 /*---------------------------------------------------------------------------*/
273 * Load an image from the named file. Return an SDL surface.
275 SDL_Surface *load_surface(const char *filename)
282 SDL_Surface *srf = NULL;
289 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
301 if ((p = image_load(config_data(filename), &w, &h, &b)))
305 if ((q = image_flip(p, w, h, b, 0, 1)))
306 srf = SDL_CreateRGBSurfaceFrom(q, w, h, b * 8, w * b,
307 rmask, gmask, bmask, amask);
313 /*---------------------------------------------------------------------------*/