libresman
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/examples/imgthumbs/src/thumbs.c Fri Jan 31 03:17:24 2014 +0200 1.3 @@ -0,0 +1,152 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include <dirent.h> 1.9 +#include <imago2.h> 1.10 +#include "opengl.h" 1.11 +#include "thumbs.h" 1.12 + 1.13 +struct thumbnail *create_thumbs(const char *dirpath) 1.14 +{ 1.15 + DIR *dir; 1.16 + struct dirent *dent; 1.17 + struct thumbnail *list = 0; 1.18 + 1.19 + if(!(dir = opendir(dirpath))) { 1.20 + fprintf(stderr, "failed to open directory: %s: %s\n", dirpath, strerror(errno)); 1.21 + return 0; 1.22 + } 1.23 + 1.24 + while((dent = readdir(dir))) { 1.25 + int xsz, ysz; 1.26 + unsigned char *pixels; 1.27 + struct thumbnail *node; 1.28 + 1.29 + if(!(node = malloc(sizeof *node))) { 1.30 + perror("failed to allocate thumbnail list node"); 1.31 + continue; 1.32 + } 1.33 + 1.34 + if(!(node->fname = malloc(strlen(dirpath) + strlen(dent->d_name) + 2))) { 1.35 + free(node); 1.36 + continue; 1.37 + } 1.38 + strcpy(node->fname, dirpath); 1.39 + if(dirpath[strlen(dirpath) - 1] != '/') { 1.40 + strcat(node->fname, "/"); 1.41 + } 1.42 + strcat(node->fname, dent->d_name); 1.43 + 1.44 + if(!(pixels = img_load_pixels(node->fname, &xsz, &ysz, IMG_FMT_RGBA32))) { 1.45 + free(node->fname); 1.46 + free(node); 1.47 + continue; 1.48 + } 1.49 + 1.50 + printf("loaded image: %s\n", node->fname); 1.51 + 1.52 + glGenTextures(1, &node->tex); 1.53 + glBindTexture(GL_TEXTURE_2D, node->tex); 1.54 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.55 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.56 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 1.57 + img_free_pixels(pixels); 1.58 + 1.59 + node->aspect = (float)xsz / (float)ysz; 1.60 + 1.61 + node->next = list; 1.62 + list = node; 1.63 + } 1.64 + closedir(dir); 1.65 + 1.66 + return list; 1.67 +} 1.68 + 1.69 +void free_thumbs(struct thumbnail *thumbs) 1.70 +{ 1.71 + if(!thumbs) return; 1.72 + 1.73 + while(thumbs) { 1.74 + struct thumbnail *tmp = thumbs; 1.75 + thumbs = thumbs->next; 1.76 + 1.77 + free(tmp->fname); 1.78 + free(tmp); 1.79 + } 1.80 +} 1.81 + 1.82 + 1.83 +void draw_thumbs(struct thumbnail *thumbs, float thumb_sz, float start_y) 1.84 +{ 1.85 + int vp[4]; 1.86 + float gap = thumb_sz / 4.0; 1.87 + float x = gap; 1.88 + float y = gap + start_y; 1.89 + float view_aspect; 1.90 + 1.91 + glGetIntegerv(GL_VIEWPORT, vp); 1.92 + view_aspect = (float)(vp[2] - vp[0]) / (vp[3] - vp[1]); 1.93 + 1.94 + glMatrixMode(GL_PROJECTION); 1.95 + glPushMatrix(); 1.96 + glLoadIdentity(); 1.97 + glOrtho(0, 1, 1.0 / view_aspect, 0, -1, 1); 1.98 + 1.99 + glMatrixMode(GL_MODELVIEW); 1.100 + 1.101 + while(thumbs) { 1.102 + 1.103 + glPushMatrix(); 1.104 + glTranslatef(x, y, 0); 1.105 + 1.106 + glScalef(thumb_sz, thumb_sz, 1); 1.107 + 1.108 + glBegin(GL_QUADS); 1.109 + glColor3f(0.25, 0.25, 0.25); 1.110 + glTexCoord2f(0, 0); glVertex2f(0, 0); 1.111 + glTexCoord2f(1, 0); glVertex2f(1, 0); 1.112 + glTexCoord2f(1, 1); glVertex2f(1, 1); 1.113 + glTexCoord2f(0, 1); glVertex2f(0, 1); 1.114 + glEnd(); 1.115 + 1.116 + if(thumbs->aspect >= 1.0) { 1.117 + glTranslatef(0, 0.5 - 0.5 / thumbs->aspect, 0); 1.118 + glScalef(1, 1.0 / thumbs->aspect, 1); 1.119 + } else { 1.120 + glTranslatef(0.5 - thumbs->aspect / 2.0, 0, 0); 1.121 + glScalef(thumbs->aspect, 1, 1); 1.122 + } 1.123 + 1.124 + glEnable(GL_TEXTURE_2D); 1.125 + glBindTexture(GL_TEXTURE_2D, thumbs->tex); 1.126 + 1.127 + glBegin(GL_QUADS); 1.128 + glColor3f(1, 1, 1); 1.129 + glTexCoord2f(0, 0); glVertex2f(0, 0); 1.130 + glTexCoord2f(1, 0); glVertex2f(1, 0); 1.131 + glTexCoord2f(1, 1); glVertex2f(1, 1); 1.132 + glTexCoord2f(0, 1); glVertex2f(0, 1); 1.133 + glEnd(); 1.134 + 1.135 + glPopMatrix(); 1.136 + glDisable(GL_TEXTURE_2D); 1.137 + 1.138 + thumbs->layout_pos[0] = x; 1.139 + thumbs->layout_pos[1] = y; 1.140 + thumbs->layout_size[0] = thumb_sz; 1.141 + thumbs->layout_size[1] = thumb_sz; 1.142 + 1.143 + x += thumb_sz + gap; 1.144 + if(x >= 1.0 - thumb_sz) { 1.145 + x = gap; 1.146 + y += thumb_sz + gap; 1.147 + } 1.148 + 1.149 + thumbs = thumbs->next; 1.150 + } 1.151 + 1.152 + glMatrixMode(GL_PROJECTION); 1.153 + glPopMatrix(); 1.154 + glMatrixMode(GL_MODELVIEW); 1.155 +}