libresman

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