ntk-chtheme: Add new color scheme. Save/restore selection color.
[ntk.git] / test / fl_jpeg_image.cxx
blob92e37ab1818132bc2d53c1f7da09bea773f22527
1 //
2 // "$Id: fl_jpeg_image.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $"
3 //
4 // fl_draw_image test program for the Fast Light Tool Kit (FLTK).
5 //
6 // Be sure to try every visual with the -v switch and try -m (monochrome)
7 // on each of them.
8 //
9 // This program requires either the libjpeg.a library or an internal DD
10 // library to read images (this is chosen by the presence of the "DD"
11 // #define).
13 // To get the jpeg library:
15 // The "official" archive site for this software is ftp.uu.net (Internet
16 // address 192.48.96.9). The most recent released version can always be
17 // found there in directory graphics/jpeg. This particular version will
18 // be archived as graphics/jpeg/jpegsrc.v6a.tar.gz.
20 // The makefile assummes you decompressed and build these in a directory
21 // called "jpeg-6a" in the same location as the "FL" directory.
23 // Copyright 1998-2010 by Bill Spitzak and others.
25 // This library is free software; you can redistribute it and/or
26 // modify it under the terms of the GNU Library General Public
27 // License as published by the Free Software Foundation; either
28 // version 2 of the License, or (at your option) any later version.
30 // This library is distributed in the hope that it will be useful,
31 // but WITHOUT ANY WARRANTY; without even the implied warranty of
32 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 // Library General Public License for more details.
35 // You should have received a copy of the GNU Library General Public
36 // License along with this library; if not, write to the Free Software
37 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 // USA.
40 // Please report all bugs and problems on the following page:
42 // http://www.fltk.org/str.php
45 #include <FL/Fl.H>
46 #include <FL/fl_draw.H>
47 #include <stdio.h>
48 #include <stdlib.h>
50 void readtheimage(const char *name); // below
51 int width;
52 int height;
53 int depth;
54 int linedelta;
55 uchar *ibuffer;
57 ////////////////////////////////////////////////////////////////
59 #include <FL/Fl_Window.H>
60 int mono;
62 class image_window : public Fl_Window {
63 void draw();
64 public:
65 image_window(int w,int h) : Fl_Window(w,h) {box(FL_NO_BOX);}
68 void image_window::draw() {
69 if (mono)
70 fl_draw_image_mono(ibuffer+1,0,0,width,height,depth,linedelta);
71 else
72 fl_draw_image(ibuffer,0,0,width,height,depth,linedelta);
75 ////////////////////////////////////////////////////////////////
77 #include <FL/x.H>
78 #include "list_visuals.cxx"
80 ////////////////////////////////////////////////////////////////
82 int visid = -1;
83 int arg(int argc, char **argv, int &i) {
84 if (argv[i][1] == 'm') {mono = 1; i++; return 1;}
86 if (argv[i][1] == 'v') {
87 if (i+1 >= argc) return 0;
88 visid = atoi(argv[i+1]);
89 i += 2;
90 return 2;
93 return 0;
96 int main(int argc, char ** argv) {
98 int i = 1;
99 if (!Fl::args(argc,argv,i,arg) || i != argc-1) {
100 fprintf(stderr,"usage: %s <switches> image_file\n"
101 " -v # : use visual\n"
102 " -m : monochrome\n"
103 "%s\n",
104 argv[0],Fl::help);
105 exit(1);
108 readtheimage(argv[i]);
109 image_window *window = new image_window(width,height);
111 if (visid>=0) {
112 fl_open_display();
113 XVisualInfo templt; int num;
114 templt.visualid = visid;
115 fl_visual = XGetVisualInfo(fl_display, VisualIDMask, &templt, &num);
116 if (!fl_visual) {
117 fprintf(stderr, "No visual with id %d, use one of:\n",visid);
118 list_visuals();
119 exit(1);
121 fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
122 fl_visual->visual, AllocNone);
123 fl_xpixel(0); // make sure black is allocated
126 window->show(argc,argv);
127 return Fl::run();
130 ////////////////////////////////////////////////////////////////
131 #ifndef DD_LIBRARY
132 // Read using jpeg library:
134 extern "C" {
135 #include "jpeglib.h"
138 void readtheimage(const char *name) {
139 struct jpeg_decompress_struct cinfo;
140 struct jpeg_error_mgr jerr;
141 FILE * infile = fopen(name, "rb");
142 if (!infile) {
143 fprintf(stderr, "can't open %s\n", name);
144 exit(1);
146 cinfo.err = jpeg_std_error(&jerr);
147 jpeg_create_decompress(&cinfo);
148 jpeg_stdio_src(&cinfo, infile);
149 jpeg_read_header(&cinfo, TRUE);
150 jpeg_start_decompress(&cinfo);
151 width = cinfo.output_width;
152 height = cinfo.output_height;
153 depth = cinfo.output_components;
154 ibuffer = new uchar[width*height*depth];
155 uchar *rp = ibuffer;
156 for (int i=0; i<height; i++) {
157 jpeg_read_scanlines(&cinfo, &rp, 1);
158 rp += width*depth;
160 jpeg_finish_decompress(&cinfo);
161 jpeg_destroy_decompress(&cinfo);
162 fclose(infile);
165 ////////////////////////////////////////////////////////////////
166 #else // Digital Domain in-house library
168 #include "DDNewImage/DDImageOp.H"
169 #include "DDNewImage/DDImgRead.H"
170 #include "DDNewImage/DDImgToBuffer.H"
172 void readtheimage(const char *name) {
173 DDImgRead reader(name);
174 width = reader.xsize();
175 height = reader.ysize();
176 depth = 4; // reader.zsize();
177 ibuffer = new uchar[width*height*depth];
178 DDImgToBuffer b(&reader,depth,ibuffer,0,0,width,height);
179 b.execute();
180 if (DDImage::haderror) {
181 fprintf(stderr,"%s\n",DDImage::errormsg());
182 exit(1);
184 // swap it around into RGBA order:
185 for (uchar *p = ibuffer+width*height*4-4; p >= ibuffer; p-=4) {
186 uchar r = p[3];
187 uchar g = p[2];
188 uchar b = p[1];
189 uchar a = p[0];
190 p[0] = r;
191 p[1] = g;
192 p[2] = b;
193 p[3] = a;
195 // make it bottom-to-top:
196 ibuffer = ibuffer + width*(height-1)*depth;
197 linedelta = -(width*depth);
199 #endif
202 // End of "$Id: fl_jpeg_image.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $".