summaryrefslogtreecommitdiff
path: root/DevIL/test/Fltk/fltk.cpp
blob: 80c614f410f1c0ace89a16982f32179645dd06f8 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// "$Id: fltk.cpp,v 1.1 2002-01-30 23:51:19 edgomez Exp $"
//
// fl_draw_image test program for the Fast Light Tool Kit (FLTK).
//
// Be sure to try every visual with the -v switch and try -m (monochrome)
// on each of them.
//
// This program requires either the libjpeg.a library or an internal DD
// library to read images (this is chosen by the presence of the "DD"
// #define).
//
// To get the jpeg library:
//
// The "official" archive site for this software is ftp.uu.net (Internet
// address 192.48.96.9).  The most recent released version can always be
// found there in directory graphics/jpeg.  This particular version will
// be archived as graphics/jpeg/jpegsrc.v6a.tar.gz.
//
// The makefile assummes you decompressed and build these in a directory
// called "jpeg-6a" in the same location as the "FL" directory.
//
// Copyright 1998-2000 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".
//

#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <stdio.h>
#include <stdlib.h>

void readtheimage(const char *name); // below
int width;
int height;
int depth;
int linedelta;
uchar *ibuffer;


#pragma comment(lib, "libjpeg.lib")


////////////////////////////////////////////////////////////////

#include <FL/Fl_Window.H>
int mono;

class image_window : public Fl_Window {
  void draw();
public:
  image_window(int w,int h) : Fl_Window(w,h) {box(FL_NO_BOX);}
};

void image_window::draw() {
  if (mono)
    fl_draw_image_mono(ibuffer+1,0,0,width,height,depth,linedelta);
  else
    fl_draw_image(ibuffer,0,0,width,height,depth,linedelta);
}

////////////////////////////////////////////////////////////////

#include <FL/x.H>
#include "list_visuals.cxx"

////////////////////////////////////////////////////////////////

int visid = -1;
int arg(int argc, char **argv, int &i) {
  if (argv[i][1] == 'm') {mono = 1; i++; return 1;}

  if (argv[i][1] == 'v') {
    if (i+1 >= argc) return 0;
    visid = atoi(argv[i+1]);
    i += 2;
    return 2;
  }

  return 0;
}

int main(int argc, char ** argv) {

  int i = 1;
  if (!Fl::args(argc,argv,i,arg) || i != argc-1) {
    fprintf(stderr,"usage: %s <switches> image_file\n"
" -v # : use visual\n"
" -m : monochrome\n"
"%s\n",
	    argv[0],Fl::help);
    exit(1);
  }

  readtheimage(argv[i]);
  image_window *window = new image_window(width,height);

  if (visid>=0) {
    /*fl_open_display();
    XVisualInfo templt; int num;
    templt.visualid = visid;
    fl_visual = XGetVisualInfo(fl_display, VisualIDMask, &templt, &num);
    if (!fl_visual) {
      fprintf(stderr, "No visual with id %d, use one of:\n",visid);
      //list_visuals();
      exit(1);
    }
    fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
				fl_visual->visual, AllocNone);
    fl_xpixel(FL_BLACK); // make sure black is allocated*/
  }

  window->show(argc,argv);
  return Fl::run();
}

////////////////////////////////////////////////////////////////

extern "C" {
#include "jpeglib.h"
}

void readtheimage(const char *name) {
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;
  FILE * infile = fopen(name, "rb");
  if (!infile) {
    fprintf(stderr, "can't open %s\n", name);
    exit(1);
  }
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_decompress(&cinfo);
  jpeg_stdio_src(&cinfo, infile);
  jpeg_read_header(&cinfo, TRUE);
  jpeg_start_decompress(&cinfo);
  width = cinfo.output_width;
  height = cinfo.output_height;
  depth = cinfo.output_components;
  ibuffer = new uchar[width*height*depth];
  uchar *rp = ibuffer;
  //  for (int i=0; i<height; i++) {
  for (int i=height; i--; ) {
    jpeg_read_scanlines(&cinfo, &rp, 1);
    rp += width*depth;
  }
  jpeg_finish_decompress(&cinfo);
  jpeg_destroy_decompress(&cinfo);
  fclose(infile);
}