libresman

view examples/imgthumbs/src/thumbs.c @ 8:86465d3d05d2

some leftover unsaved changes
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 03 Feb 2014 05:24:22 +0200
parents 026cdd1737ff
children bebc065a941f
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <dirent.h>
6 #include <imago2.h>
7 #include "opengl.h"
8 #include "thumbs.h"
10 #ifndef GL_COMPRESSED_RGB
11 #define GL_COMPRESSED_RGB 0x84ed
12 #endif
14 struct thumbnail *create_thumbs(const char *dirpath)
15 {
16 DIR *dir;
17 struct dirent *dent;
18 struct thumbnail *list = 0;
20 unsigned int intfmt = GL_COMPRESSED_RGB;
21 if(!strstr(glGetString(GL_EXTENSIONS), "GL_ARB_texture_compression")) {
22 printf("warning, no texture compression available.\n");
23 intfmt = GL_RGB;
24 }
26 if(!(dir = opendir(dirpath))) {
27 fprintf(stderr, "failed to open directory: %s: %s\n", dirpath, strerror(errno));
28 return 0;
29 }
31 while((dent = readdir(dir))) {
32 int xsz, ysz;
33 unsigned char *pixels;
34 struct thumbnail *node;
36 if(!(node = malloc(sizeof *node))) {
37 perror("failed to allocate thumbnail list node");
38 continue;
39 }
41 if(!(node->fname = malloc(strlen(dirpath) + strlen(dent->d_name) + 2))) {
42 free(node);
43 continue;
44 }
45 strcpy(node->fname, dirpath);
46 if(dirpath[strlen(dirpath) - 1] != '/') {
47 strcat(node->fname, "/");
48 }
49 strcat(node->fname, dent->d_name);
51 if(!(pixels = img_load_pixels(node->fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
52 free(node->fname);
53 free(node);
54 continue;
55 }
57 printf("loaded image: %s\n", node->fname);
59 glGenTextures(1, &node->tex);
60 glBindTexture(GL_TEXTURE_2D, node->tex);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
63 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
64 img_free_pixels(pixels);
66 node->aspect = (float)xsz / (float)ysz;
68 node->next = list;
69 list = node;
70 }
71 closedir(dir);
73 return list;
74 }
76 void free_thumbs(struct thumbnail *thumbs)
77 {
78 if(!thumbs) return;
80 while(thumbs) {
81 struct thumbnail *tmp = thumbs;
82 thumbs = thumbs->next;
84 free(tmp->fname);
85 free(tmp);
86 }
87 }
90 void draw_thumbs(struct thumbnail *thumbs, float thumb_sz, float start_y)
91 {
92 int vp[4];
93 float gap = thumb_sz / 4.0;
94 float x = gap;
95 float y = gap + start_y;
96 float view_aspect;
98 glGetIntegerv(GL_VIEWPORT, vp);
99 view_aspect = (float)(vp[2] - vp[0]) / (vp[3] - vp[1]);
101 glMatrixMode(GL_PROJECTION);
102 glPushMatrix();
103 glLoadIdentity();
104 glOrtho(0, 1, 1.0 / view_aspect, 0, -1, 1);
106 glMatrixMode(GL_MODELVIEW);
108 while(thumbs) {
110 glPushMatrix();
111 glTranslatef(x, y, 0);
113 glScalef(thumb_sz, thumb_sz, 1);
115 glBegin(GL_QUADS);
116 glColor3f(0.25, 0.25, 0.25);
117 glTexCoord2f(0, 0); glVertex2f(0, 0);
118 glTexCoord2f(1, 0); glVertex2f(1, 0);
119 glTexCoord2f(1, 1); glVertex2f(1, 1);
120 glTexCoord2f(0, 1); glVertex2f(0, 1);
121 glEnd();
123 if(thumbs->aspect >= 1.0) {
124 glTranslatef(0, 0.5 - 0.5 / thumbs->aspect, 0);
125 glScalef(1, 1.0 / thumbs->aspect, 1);
126 } else {
127 glTranslatef(0.5 - thumbs->aspect / 2.0, 0, 0);
128 glScalef(thumbs->aspect, 1, 1);
129 }
131 glEnable(GL_TEXTURE_2D);
132 glBindTexture(GL_TEXTURE_2D, thumbs->tex);
134 glBegin(GL_QUADS);
135 glColor3f(1, 1, 1);
136 glTexCoord2f(0, 0); glVertex2f(0, 0);
137 glTexCoord2f(1, 0); glVertex2f(1, 0);
138 glTexCoord2f(1, 1); glVertex2f(1, 1);
139 glTexCoord2f(0, 1); glVertex2f(0, 1);
140 glEnd();
142 glPopMatrix();
143 glDisable(GL_TEXTURE_2D);
145 thumbs->layout_pos[0] = x;
146 thumbs->layout_pos[1] = y;
147 thumbs->layout_size[0] = thumb_sz;
148 thumbs->layout_size[1] = thumb_sz;
150 x += thumb_sz + gap;
151 if(x >= 1.0 - thumb_sz) {
152 x = gap;
153 y += thumb_sz + gap;
154 }
156 thumbs = thumbs->next;
157 }
159 glMatrixMode(GL_PROJECTION);
160 glPopMatrix();
161 glMatrixMode(GL_MODELVIEW);
162 }