libresman

view examples/imgthumbs/src/thumbs.c @ 1:469ce01809bc

rudimentary imgthumbs "example program". doesn't use libresman yet
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 31 Jan 2014 03:17:24 +0200
parents
children 026cdd1737ff
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 struct thumbnail *create_thumbs(const char *dirpath)
11 {
12 DIR *dir;
13 struct dirent *dent;
14 struct thumbnail *list = 0;
16 if(!(dir = opendir(dirpath))) {
17 fprintf(stderr, "failed to open directory: %s: %s\n", dirpath, strerror(errno));
18 return 0;
19 }
21 while((dent = readdir(dir))) {
22 int xsz, ysz;
23 unsigned char *pixels;
24 struct thumbnail *node;
26 if(!(node = malloc(sizeof *node))) {
27 perror("failed to allocate thumbnail list node");
28 continue;
29 }
31 if(!(node->fname = malloc(strlen(dirpath) + strlen(dent->d_name) + 2))) {
32 free(node);
33 continue;
34 }
35 strcpy(node->fname, dirpath);
36 if(dirpath[strlen(dirpath) - 1] != '/') {
37 strcat(node->fname, "/");
38 }
39 strcat(node->fname, dent->d_name);
41 if(!(pixels = img_load_pixels(node->fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
42 free(node->fname);
43 free(node);
44 continue;
45 }
47 printf("loaded image: %s\n", node->fname);
49 glGenTextures(1, &node->tex);
50 glBindTexture(GL_TEXTURE_2D, node->tex);
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
53 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
54 img_free_pixels(pixels);
56 node->aspect = (float)xsz / (float)ysz;
58 node->next = list;
59 list = node;
60 }
61 closedir(dir);
63 return list;
64 }
66 void free_thumbs(struct thumbnail *thumbs)
67 {
68 if(!thumbs) return;
70 while(thumbs) {
71 struct thumbnail *tmp = thumbs;
72 thumbs = thumbs->next;
74 free(tmp->fname);
75 free(tmp);
76 }
77 }
80 void draw_thumbs(struct thumbnail *thumbs, float thumb_sz, float start_y)
81 {
82 int vp[4];
83 float gap = thumb_sz / 4.0;
84 float x = gap;
85 float y = gap + start_y;
86 float view_aspect;
88 glGetIntegerv(GL_VIEWPORT, vp);
89 view_aspect = (float)(vp[2] - vp[0]) / (vp[3] - vp[1]);
91 glMatrixMode(GL_PROJECTION);
92 glPushMatrix();
93 glLoadIdentity();
94 glOrtho(0, 1, 1.0 / view_aspect, 0, -1, 1);
96 glMatrixMode(GL_MODELVIEW);
98 while(thumbs) {
100 glPushMatrix();
101 glTranslatef(x, y, 0);
103 glScalef(thumb_sz, thumb_sz, 1);
105 glBegin(GL_QUADS);
106 glColor3f(0.25, 0.25, 0.25);
107 glTexCoord2f(0, 0); glVertex2f(0, 0);
108 glTexCoord2f(1, 0); glVertex2f(1, 0);
109 glTexCoord2f(1, 1); glVertex2f(1, 1);
110 glTexCoord2f(0, 1); glVertex2f(0, 1);
111 glEnd();
113 if(thumbs->aspect >= 1.0) {
114 glTranslatef(0, 0.5 - 0.5 / thumbs->aspect, 0);
115 glScalef(1, 1.0 / thumbs->aspect, 1);
116 } else {
117 glTranslatef(0.5 - thumbs->aspect / 2.0, 0, 0);
118 glScalef(thumbs->aspect, 1, 1);
119 }
121 glEnable(GL_TEXTURE_2D);
122 glBindTexture(GL_TEXTURE_2D, thumbs->tex);
124 glBegin(GL_QUADS);
125 glColor3f(1, 1, 1);
126 glTexCoord2f(0, 0); glVertex2f(0, 0);
127 glTexCoord2f(1, 0); glVertex2f(1, 0);
128 glTexCoord2f(1, 1); glVertex2f(1, 1);
129 glTexCoord2f(0, 1); glVertex2f(0, 1);
130 glEnd();
132 glPopMatrix();
133 glDisable(GL_TEXTURE_2D);
135 thumbs->layout_pos[0] = x;
136 thumbs->layout_pos[1] = y;
137 thumbs->layout_size[0] = thumb_sz;
138 thumbs->layout_size[1] = thumb_sz;
140 x += thumb_sz + gap;
141 if(x >= 1.0 - thumb_sz) {
142 x = gap;
143 y += thumb_sz + gap;
144 }
146 thumbs = thumbs->next;
147 }
149 glMatrixMode(GL_PROJECTION);
150 glPopMatrix();
151 glMatrixMode(GL_MODELVIEW);
152 }