1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/************************************************************************
This is a very simplistic image viewer. It uses libgfx routines to
read a set of image files specified on the command line. These images
are then displayed in a window using OpenGL image operations.
by Michael Garland, 2000.
$Id: t-glimg.cxx 426 2004-09-27 04:34:55Z garland $
************************************************************************/
#include <gfx/gfx.h>
#include <gfx/gui.h>
#include <gfx/raster.h>
#include <string>
#include <vector>
#include <FL/filename.H>
class GUI : public MxGUI
{
public:
std::vector<ByteRaster *> images;
std::vector<std::string> image_names;
int current_image;
virtual ~GUI();
virtual void update_animation();
virtual void cmdline_file(const char *file);
virtual void setup_for_drawing();
virtual void draw_contents();
void set_image(int i);
};
GUI gui;
GUI::~GUI()
{
for(int i=0; i<images.size(); i++)
if( images[i] )
delete images[i];
}
static void cb_image(Fl_Widget *, long i)
{ gui.set_image(i); gui.canvas->redraw(); }
void GUI::set_image(int i)
{
current_image = i;
if(current_image<0 || current_image>=images.size()) return;
ByteRaster *img = images[current_image];
int width = img->width();
int height = img->height();
gui.status(image_names[current_image].c_str());
if( width!=canvas->w() || height!=canvas->h() )
{
unlock_size();
resize_canvas(width, height);
lock_size();
}
}
void GUI::cmdline_file(const char *namestring)
{
if(!namestring) return; // Don't support reading from stdin
static std::string img_menu("&Images/");
std::string filename(fl_filename_name(namestring)); // Strip directories
ByteRaster *img = read_image(namestring);
if( img )
{
int id = images.size();
images.push_back(img);
image_names.push_back(namestring);
int key = id<9 ? FL_CTRL+'1'+id : 0;
gui.menu_bar->add((img_menu+filename).c_str(), key,
(Fl_Callback *)cb_image, (void *)id, FL_MENU_RADIO);
}
}
void GUI::update_animation()
{
set_image((current_image+1) % images.size());
}
void GUI::setup_for_drawing()
{
glClearColor(0.9f, 0.0f, 0.0f, 0.0f);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glPixelZoom(1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void draw_image(const ByteRaster *img)
{
GLenum format;
if(img->channels()==1) format = GL_LUMINANCE;
else if(img->channels()==3) format = GL_RGB;
else if(img->channels()==4) format = GL_RGBA;
else return; // Don't draw unsupported formats
glPushAttrib(GL_PIXEL_MODE_BIT);
glRasterPos2f(0, img->height());
glDrawPixels(img->width(), img->height(),
format, GL_UNSIGNED_BYTE, img->head());
glPopAttrib();
}
void GUI::draw_contents()
{
glClear(GL_COLOR_BUFFER_BIT);
if(current_image<0 || current_image>=images.size()) return;
ByteRaster *img = images[current_image];
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, img->width(), 0, img->height());
draw_image(img);
}
int main(int argc, char **argv)
{
gui.initialize(argc, argv);
if(gui.images.size() == 0) return 0; // Exit if no images read
gui.title("OpenGL Image Viewer");
gui.default_fps = 1.0f;
gui.set_image(0);
return gui.run();
}
|