summaryrefslogtreecommitdiff
path: root/load_texture.c
blob: d83d0effdf292fe7b7cee187950261bffa7cbcb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright © 2009 Eric Anholt
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <err.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <getopt.h>

#include <png.h>

#include "glass.h"

/* libpng sucks and I hope I never have to read this file again. */

GLuint load_texture_rgb(const char *filename)
{
	GLuint tex;
	FILE *f;
	png_uint_32 w, h;
	int depth;
	unsigned char *data;
	png_structp png;
	png_infop png_info;
	int png_type;
	int stride;
	png_bytep *row_pointers;
	int i;
	GLenum format;

	glGenTextures(1, &tex);

	f = fopen(filename, "rb");

	png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
					 NULL);
	assert(png);

	png_info = png_create_info_struct(png);
	assert(png_info);

	if (setjmp(png->jmpbuf)) {
		png_destroy_read_struct(&png, &png_info, NULL);
		abort();
	}

	png_init_io(png, f);
	png_read_info(png, png_info);
	png_get_IHDR(png, png_info, &w, &h, &depth, &png_type,
		     NULL, NULL, NULL);

	/* Give us sensible data.  This API is terrible */
	png_set_expand(png);
	/* No, really.  Sensible data. */
	if (depth == 16)
		png_set_strip_16(png);
	/* Not even kidding. */
	png_set_gray_to_rgb(png);

	png_read_update_info(png, png_info);

	stride = png_get_rowbytes(png, png_info);
	data = malloc(h * stride);
	assert(data);

	/* So, you've told us what the stride needs to be, but you make
	 * us give you pointers to the data?  Brilliant.
	 */
	row_pointers = malloc(sizeof(png_bytep) * h);
	assert(row_pointers);
	for (i = 0; i < h; i++)
		row_pointers[i] = data + i * stride;

	png_read_image(png, row_pointers);

	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	switch (png_info->color_type) {
	case PNG_COLOR_TYPE_RGB:
		format = GL_RGB;
		break;
	case PNG_COLOR_TYPE_RGBA:
		format = GL_RGBA;
		break;
	default:
		abort();
	}
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
		     format, GL_UNSIGNED_BYTE, data);

	free(data);
	free(row_pointers);

	png_destroy_read_struct(&png, &png_info, NULL);

	return tex;
}