summaryrefslogtreecommitdiffstats
path: root/texture.c
blob: 2d6112b24ca329cfcc710a6667eddc6604255b87 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/gl.h>
#include <zoom/texture.h>
#include <png.h>


int nTex;
TEXLIST *texlist;


GLuint LoadTexture(char *filename) {
  FILE *file;
  GLuint tex;
  char *name;
  png_byte header[8];
  png_structp png_ptr;
  png_infop info_ptr;
  png_bytep *row_pointers;
  png_bytep data;
  png_uint_32 width, height;
  int bit_depth, color_type, i;
  GLenum format;
  
  
  for(i = 0; i < nTex; i++) {
    if(strcasecmp(filename, texlist[i].name) == 0) return texlist[i].id;
  }
  
  name = malloc(strlen(filename)+5);
  strcpy(name, "tex/");
  strcat(name, filename);
  
  file = fopen(name, "rb");
  
  free(name);
  
  if(!file) return 0;
  
  fread(&header, 1, sizeof(header), file);
  if(png_sig_cmp(header, 0, sizeof(header))) {
    fclose(file);
    return 0;
  }
  
  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if(!png_ptr) {
    fclose(file);
    return 0;
  }
  
  info_ptr = png_create_info_struct(png_ptr);
  if(!info_ptr) {
    png_destroy_read_struct(&png_ptr, NULL, NULL);
    fclose(file);
    return 0;
  }
  
  if(setjmp(png_jmpbuf(png_ptr))) {
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    fclose(file);
    return 0;
  }
  
  png_init_io(png_ptr, file);
  png_set_sig_bytes(png_ptr, sizeof(header));
  
  png_read_info(png_ptr, info_ptr);
  
  width = png_get_image_width(png_ptr, info_ptr);
  height = png_get_image_height(png_ptr, info_ptr);
  bit_depth = png_get_bit_depth(png_ptr, info_ptr);
  color_type = png_get_color_type(png_ptr, info_ptr);
  
  if(color_type == PNG_COLOR_TYPE_PALETTE)
    png_set_palette_to_rgb(png_ptr);
  if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
    png_set_expand_gray_1_2_4_to_8(png_ptr);
  if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
    png_set_tRNS_to_alpha(png_ptr);
  if(bit_depth < 8)
    png_set_packing(png_ptr);
  
  png_read_update_info(png_ptr, info_ptr);
  
  bit_depth = png_get_bit_depth(png_ptr, info_ptr);
  color_type = png_get_color_type(png_ptr, info_ptr);
  
  if((bit_depth != 8 && bit_depth != 16) || color_type & PNG_COLOR_MASK_PALETTE) {
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    fclose(file);
    return 0;
  }
  
  data = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)*height);
  row_pointers = png_malloc(png_ptr, height*png_sizeof(png_bytep));
  for(i = 0; i < height; i++)
    row_pointers[i] = data + png_get_rowbytes(png_ptr, info_ptr)*i;
  
  png_read_image(png_ptr, row_pointers);
  
  switch(color_type) {
    case PNG_COLOR_TYPE_GRAY:
      format = GL_LUMINANCE;
      break;
    case PNG_COLOR_TYPE_GRAY_ALPHA:
      format = GL_LUMINANCE_ALPHA;
      break;
    case PNG_COLOR_TYPE_RGB:
      format = GL_RGB;
      break;
    case PNG_COLOR_TYPE_RGBA:
      format = GL_RGBA;
      break;
    default:
      png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
      fclose(file);
      return 0;
  }
  
  glGenTextures(1, &tex);
  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);
  glTexImage2D(GL_TEXTURE_2D, 0, png_get_channels(png_ptr, info_ptr), width, height, 0,
               format, (bit_depth == 8) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT, data);
  
  png_free(png_ptr, row_pointers);
  png_free(png_ptr, data);
  
  png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  
  fclose(file);
  
  nTex++;
  
  if(nTex == 1) texlist = malloc(sizeof(TEXLIST));
  else texlist = realloc(texlist, sizeof(TEXLIST)*nTex);
  
  strcpy(texlist[nTex-1].name, filename);
  texlist[nTex-1].id = tex;
  
  return tex;
}