rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@1
|
2 #include <stdlib.h>
|
nuclear@1
|
3 #include <string.h>
|
nuclear@1
|
4 #include <errno.h>
|
nuclear@1
|
5 #include <dirent.h>
|
nuclear@1
|
6 #include <imago2.h>
|
nuclear@1
|
7 #include "opengl.h"
|
nuclear@1
|
8 #include "thumbs.h"
|
nuclear@1
|
9
|
nuclear@1
|
10 struct thumbnail *create_thumbs(const char *dirpath)
|
nuclear@1
|
11 {
|
nuclear@1
|
12 DIR *dir;
|
nuclear@1
|
13 struct dirent *dent;
|
nuclear@1
|
14 struct thumbnail *list = 0;
|
nuclear@1
|
15
|
nuclear@2
|
16 unsigned int intfmt = GL_COMPRESSED_RGB;
|
nuclear@2
|
17 if(!GLEW_ARB_texture_compression) {
|
nuclear@2
|
18 printf("warning, no texture compression available.\n");
|
nuclear@2
|
19 intfmt = GL_RGB;
|
nuclear@2
|
20 }
|
nuclear@2
|
21
|
nuclear@1
|
22 if(!(dir = opendir(dirpath))) {
|
nuclear@1
|
23 fprintf(stderr, "failed to open directory: %s: %s\n", dirpath, strerror(errno));
|
nuclear@1
|
24 return 0;
|
nuclear@1
|
25 }
|
nuclear@1
|
26
|
nuclear@1
|
27 while((dent = readdir(dir))) {
|
nuclear@1
|
28 int xsz, ysz;
|
nuclear@1
|
29 unsigned char *pixels;
|
nuclear@1
|
30 struct thumbnail *node;
|
nuclear@1
|
31
|
nuclear@1
|
32 if(!(node = malloc(sizeof *node))) {
|
nuclear@1
|
33 perror("failed to allocate thumbnail list node");
|
nuclear@1
|
34 continue;
|
nuclear@1
|
35 }
|
nuclear@1
|
36
|
nuclear@1
|
37 if(!(node->fname = malloc(strlen(dirpath) + strlen(dent->d_name) + 2))) {
|
nuclear@1
|
38 free(node);
|
nuclear@1
|
39 continue;
|
nuclear@1
|
40 }
|
nuclear@1
|
41 strcpy(node->fname, dirpath);
|
nuclear@1
|
42 if(dirpath[strlen(dirpath) - 1] != '/') {
|
nuclear@1
|
43 strcat(node->fname, "/");
|
nuclear@1
|
44 }
|
nuclear@1
|
45 strcat(node->fname, dent->d_name);
|
nuclear@1
|
46
|
nuclear@1
|
47 if(!(pixels = img_load_pixels(node->fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
|
nuclear@1
|
48 free(node->fname);
|
nuclear@1
|
49 free(node);
|
nuclear@1
|
50 continue;
|
nuclear@1
|
51 }
|
nuclear@1
|
52
|
nuclear@1
|
53 printf("loaded image: %s\n", node->fname);
|
nuclear@1
|
54
|
nuclear@1
|
55 glGenTextures(1, &node->tex);
|
nuclear@1
|
56 glBindTexture(GL_TEXTURE_2D, node->tex);
|
nuclear@1
|
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@1
|
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@2
|
59 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
nuclear@1
|
60 img_free_pixels(pixels);
|
nuclear@1
|
61
|
nuclear@1
|
62 node->aspect = (float)xsz / (float)ysz;
|
nuclear@1
|
63
|
nuclear@1
|
64 node->next = list;
|
nuclear@1
|
65 list = node;
|
nuclear@1
|
66 }
|
nuclear@1
|
67 closedir(dir);
|
nuclear@1
|
68
|
nuclear@1
|
69 return list;
|
nuclear@1
|
70 }
|
nuclear@1
|
71
|
nuclear@1
|
72 void free_thumbs(struct thumbnail *thumbs)
|
nuclear@1
|
73 {
|
nuclear@1
|
74 if(!thumbs) return;
|
nuclear@1
|
75
|
nuclear@1
|
76 while(thumbs) {
|
nuclear@1
|
77 struct thumbnail *tmp = thumbs;
|
nuclear@1
|
78 thumbs = thumbs->next;
|
nuclear@1
|
79
|
nuclear@1
|
80 free(tmp->fname);
|
nuclear@1
|
81 free(tmp);
|
nuclear@1
|
82 }
|
nuclear@1
|
83 }
|
nuclear@1
|
84
|
nuclear@1
|
85
|
nuclear@1
|
86 void draw_thumbs(struct thumbnail *thumbs, float thumb_sz, float start_y)
|
nuclear@1
|
87 {
|
nuclear@1
|
88 int vp[4];
|
nuclear@1
|
89 float gap = thumb_sz / 4.0;
|
nuclear@1
|
90 float x = gap;
|
nuclear@1
|
91 float y = gap + start_y;
|
nuclear@1
|
92 float view_aspect;
|
nuclear@1
|
93
|
nuclear@1
|
94 glGetIntegerv(GL_VIEWPORT, vp);
|
nuclear@1
|
95 view_aspect = (float)(vp[2] - vp[0]) / (vp[3] - vp[1]);
|
nuclear@1
|
96
|
nuclear@1
|
97 glMatrixMode(GL_PROJECTION);
|
nuclear@1
|
98 glPushMatrix();
|
nuclear@1
|
99 glLoadIdentity();
|
nuclear@1
|
100 glOrtho(0, 1, 1.0 / view_aspect, 0, -1, 1);
|
nuclear@1
|
101
|
nuclear@1
|
102 glMatrixMode(GL_MODELVIEW);
|
nuclear@1
|
103
|
nuclear@1
|
104 while(thumbs) {
|
nuclear@1
|
105
|
nuclear@1
|
106 glPushMatrix();
|
nuclear@1
|
107 glTranslatef(x, y, 0);
|
nuclear@1
|
108
|
nuclear@1
|
109 glScalef(thumb_sz, thumb_sz, 1);
|
nuclear@1
|
110
|
nuclear@1
|
111 glBegin(GL_QUADS);
|
nuclear@1
|
112 glColor3f(0.25, 0.25, 0.25);
|
nuclear@1
|
113 glTexCoord2f(0, 0); glVertex2f(0, 0);
|
nuclear@1
|
114 glTexCoord2f(1, 0); glVertex2f(1, 0);
|
nuclear@1
|
115 glTexCoord2f(1, 1); glVertex2f(1, 1);
|
nuclear@1
|
116 glTexCoord2f(0, 1); glVertex2f(0, 1);
|
nuclear@1
|
117 glEnd();
|
nuclear@1
|
118
|
nuclear@1
|
119 if(thumbs->aspect >= 1.0) {
|
nuclear@1
|
120 glTranslatef(0, 0.5 - 0.5 / thumbs->aspect, 0);
|
nuclear@1
|
121 glScalef(1, 1.0 / thumbs->aspect, 1);
|
nuclear@1
|
122 } else {
|
nuclear@1
|
123 glTranslatef(0.5 - thumbs->aspect / 2.0, 0, 0);
|
nuclear@1
|
124 glScalef(thumbs->aspect, 1, 1);
|
nuclear@1
|
125 }
|
nuclear@1
|
126
|
nuclear@1
|
127 glEnable(GL_TEXTURE_2D);
|
nuclear@1
|
128 glBindTexture(GL_TEXTURE_2D, thumbs->tex);
|
nuclear@1
|
129
|
nuclear@1
|
130 glBegin(GL_QUADS);
|
nuclear@1
|
131 glColor3f(1, 1, 1);
|
nuclear@1
|
132 glTexCoord2f(0, 0); glVertex2f(0, 0);
|
nuclear@1
|
133 glTexCoord2f(1, 0); glVertex2f(1, 0);
|
nuclear@1
|
134 glTexCoord2f(1, 1); glVertex2f(1, 1);
|
nuclear@1
|
135 glTexCoord2f(0, 1); glVertex2f(0, 1);
|
nuclear@1
|
136 glEnd();
|
nuclear@1
|
137
|
nuclear@1
|
138 glPopMatrix();
|
nuclear@1
|
139 glDisable(GL_TEXTURE_2D);
|
nuclear@1
|
140
|
nuclear@1
|
141 thumbs->layout_pos[0] = x;
|
nuclear@1
|
142 thumbs->layout_pos[1] = y;
|
nuclear@1
|
143 thumbs->layout_size[0] = thumb_sz;
|
nuclear@1
|
144 thumbs->layout_size[1] = thumb_sz;
|
nuclear@1
|
145
|
nuclear@1
|
146 x += thumb_sz + gap;
|
nuclear@1
|
147 if(x >= 1.0 - thumb_sz) {
|
nuclear@1
|
148 x = gap;
|
nuclear@1
|
149 y += thumb_sz + gap;
|
nuclear@1
|
150 }
|
nuclear@1
|
151
|
nuclear@1
|
152 thumbs = thumbs->next;
|
nuclear@1
|
153 }
|
nuclear@1
|
154
|
nuclear@1
|
155 glMatrixMode(GL_PROJECTION);
|
nuclear@1
|
156 glPopMatrix();
|
nuclear@1
|
157 glMatrixMode(GL_MODELVIEW);
|
nuclear@1
|
158 }
|