2 // "$Id: fl_jpeg_image.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $"
4 // fl_draw_image test program for the Fast Light Tool Kit (FLTK).
6 // Be sure to try every visual with the -v switch and try -m (monochrome)
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"
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
40 // Please report all bugs and problems on the following page:
42 // http://www.fltk.org/str.php
46 #include <FL/fl_draw.H>
50 void readtheimage(const char *name
); // below
57 ////////////////////////////////////////////////////////////////
59 #include <FL/Fl_Window.H>
62 class image_window
: public Fl_Window
{
65 image_window(int w
,int h
) : Fl_Window(w
,h
) {box(FL_NO_BOX
);}
68 void image_window::draw() {
70 fl_draw_image_mono(ibuffer
+1,0,0,width
,height
,depth
,linedelta
);
72 fl_draw_image(ibuffer
,0,0,width
,height
,depth
,linedelta
);
75 ////////////////////////////////////////////////////////////////
78 #include "list_visuals.cxx"
80 ////////////////////////////////////////////////////////////////
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]);
96 int main(int argc
, char ** argv
) {
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"
108 readtheimage(argv
[i
]);
109 image_window
*window
= new image_window(width
,height
);
113 XVisualInfo templt
; int num
;
114 templt
.visualid
= visid
;
115 fl_visual
= XGetVisualInfo(fl_display
, VisualIDMask
, &templt
, &num
);
117 fprintf(stderr
, "No visual with id %d, use one of:\n",visid
);
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
);
130 ////////////////////////////////////////////////////////////////
132 // Read using jpeg library:
138 void readtheimage(const char *name
) {
139 struct jpeg_decompress_struct cinfo
;
140 struct jpeg_error_mgr jerr
;
141 FILE * infile
= fopen(name
, "rb");
143 fprintf(stderr
, "can't open %s\n", name
);
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
];
156 for (int i
=0; i
<height
; i
++) {
157 jpeg_read_scanlines(&cinfo
, &rp
, 1);
160 jpeg_finish_decompress(&cinfo
);
161 jpeg_destroy_decompress(&cinfo
);
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
);
180 if (DDImage::haderror
) {
181 fprintf(stderr
,"%s\n",DDImage::errormsg());
184 // swap it around into RGBA order:
185 for (uchar
*p
= ibuffer
+width
*height
*4-4; p
>= ibuffer
; p
-=4) {
195 // make it bottom-to-top:
196 ibuffer
= ibuffer
+ width
*(height
-1)*depth
;
197 linedelta
= -(width
*depth
);
202 // End of "$Id: fl_jpeg_image.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $".