You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.4KB

  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).
  12. //
  13. // To get the jpeg library:
  14. //
  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.
  19. //
  20. // The makefile assummes you decompressed and build these in a directory
  21. // called "jpeg-6a" in the same location as the "FL" directory.
  22. //
  23. // Copyright 1998-2010 by Bill Spitzak and others.
  24. //
  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.
  29. //
  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.
  34. //
  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.
  39. //
  40. // Please report all bugs and problems on the following page:
  41. //
  42. // http://www.fltk.org/str.php
  43. //
  44. #include <FL/Fl.H>
  45. #include <FL/fl_draw.H>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. void readtheimage(const char *name); // below
  49. int width;
  50. int height;
  51. int depth;
  52. int linedelta;
  53. uchar *ibuffer;
  54. ////////////////////////////////////////////////////////////////
  55. #include <FL/Fl_Window.H>
  56. int mono;
  57. class image_window : public Fl_Window {
  58. void draw();
  59. public:
  60. image_window(int w,int h) : Fl_Window(w,h) {box(FL_NO_BOX);}
  61. };
  62. void image_window::draw() {
  63. if (mono)
  64. fl_draw_image_mono(ibuffer+1,0,0,width,height,depth,linedelta);
  65. else
  66. fl_draw_image(ibuffer,0,0,width,height,depth,linedelta);
  67. }
  68. ////////////////////////////////////////////////////////////////
  69. #include <FL/x.H>
  70. #include "list_visuals.cxx"
  71. ////////////////////////////////////////////////////////////////
  72. int visid = -1;
  73. int arg(int argc, char **argv, int &i) {
  74. if (argv[i][1] == 'm') {mono = 1; i++; return 1;}
  75. if (argv[i][1] == 'v') {
  76. if (i+1 >= argc) return 0;
  77. visid = atoi(argv[i+1]);
  78. i += 2;
  79. return 2;
  80. }
  81. return 0;
  82. }
  83. int main(int argc, char ** argv) {
  84. int i = 1;
  85. if (!Fl::args(argc,argv,i,arg) || i != argc-1) {
  86. fprintf(stderr,"usage: %s <switches> image_file\n"
  87. " -v # : use visual\n"
  88. " -m : monochrome\n"
  89. "%s\n",
  90. argv[0],Fl::help);
  91. exit(1);
  92. }
  93. readtheimage(argv[i]);
  94. image_window *window = new image_window(width,height);
  95. if (visid>=0) {
  96. fl_open_display();
  97. XVisualInfo templt; int num;
  98. templt.visualid = visid;
  99. fl_visual = XGetVisualInfo(fl_display, VisualIDMask, &templt, &num);
  100. if (!fl_visual) {
  101. fprintf(stderr, "No visual with id %d, use one of:\n",visid);
  102. list_visuals();
  103. exit(1);
  104. }
  105. fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
  106. fl_visual->visual, AllocNone);
  107. fl_xpixel(0); // make sure black is allocated
  108. }
  109. window->show(argc,argv);
  110. return Fl::run();
  111. }
  112. ////////////////////////////////////////////////////////////////
  113. #ifndef DD_LIBRARY
  114. // Read using jpeg library:
  115. extern "C" {
  116. #include "jpeglib.h"
  117. }
  118. void readtheimage(const char *name) {
  119. struct jpeg_decompress_struct cinfo;
  120. struct jpeg_error_mgr jerr;
  121. FILE * infile = fopen(name, "rb");
  122. if (!infile) {
  123. fprintf(stderr, "can't open %s\n", name);
  124. exit(1);
  125. }
  126. cinfo.err = jpeg_std_error(&jerr);
  127. jpeg_create_decompress(&cinfo);
  128. jpeg_stdio_src(&cinfo, infile);
  129. jpeg_read_header(&cinfo, TRUE);
  130. jpeg_start_decompress(&cinfo);
  131. width = cinfo.output_width;
  132. height = cinfo.output_height;
  133. depth = cinfo.output_components;
  134. ibuffer = new uchar[width*height*depth];
  135. uchar *rp = ibuffer;
  136. for (int i=0; i<height; i++) {
  137. jpeg_read_scanlines(&cinfo, &rp, 1);
  138. rp += width*depth;
  139. }
  140. jpeg_finish_decompress(&cinfo);
  141. jpeg_destroy_decompress(&cinfo);
  142. fclose(infile);
  143. }
  144. ////////////////////////////////////////////////////////////////
  145. #else // Digital Domain in-house library
  146. #include "DDNewImage/DDImageOp.H"
  147. #include "DDNewImage/DDImgRead.H"
  148. #include "DDNewImage/DDImgToBuffer.H"
  149. void readtheimage(const char *name) {
  150. DDImgRead reader(name);
  151. width = reader.xsize();
  152. height = reader.ysize();
  153. depth = 4; // reader.zsize();
  154. ibuffer = new uchar[width*height*depth];
  155. DDImgToBuffer b(&reader,depth,ibuffer,0,0,width,height);
  156. b.execute();
  157. if (DDImage::haderror) {
  158. fprintf(stderr,"%s\n",DDImage::errormsg());
  159. exit(1);
  160. }
  161. // swap it around into RGBA order:
  162. for (uchar *p = ibuffer+width*height*4-4; p >= ibuffer; p-=4) {
  163. uchar r = p[3];
  164. uchar g = p[2];
  165. uchar b = p[1];
  166. uchar a = p[0];
  167. p[0] = r;
  168. p[1] = g;
  169. p[2] = b;
  170. p[3] = a;
  171. }
  172. // make it bottom-to-top:
  173. ibuffer = ibuffer + width*(height-1)*depth;
  174. linedelta = -(width*depth);
  175. }
  176. #endif
  177. //
  178. // End of "$Id: fl_jpeg_image.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $".
  179. //