File: GFilter.h

package info (click to toggle)
kdc2tiff 0.35-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 556 kB
  • sloc: cpp: 3,107; ansic: 832; sh: 330; makefile: 23
file content (347 lines) | stat: -rw-r--r-- 8,815 bytes parent folder | download | duplicates (8)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#ifndef _GFilter_H
#define _GFilter_H

#include "GImage.h"

/* GFilter is an abstract decendant of GImage.  Derivatives of this class can
 * be used to filter an image in some way (scale, rotate, etc).  Filters
 * defined in this file include:
 *
 *     GFilter_GammaCorrection
 *     GFilter_Scale
 *     GFilter_Rotate (0, 90, 180, and 270 degrees only)
 *     GFilter_ContrastAdjustment
 *     GFilter_WhiteBalance
 * 
 * Written by:  Chris Studholme
 * Last Update: 25-May-2000
 * Copyright:   GPL (http://www.fsf.org/copyleft/gpl.html)
 */

class GFilter : public GImage {
 protected:
  GImage& src;
  
 protected:
  GFilter(GImage& image) : src(image) {
  }
  GImage* getSourceImage() {
    return &src;
  }

  /* These get???() routines are just defaults.  They are all virtual and 
   * should be overridden where necessary.
   */
  const char* getComments() {
    return src.getComments();
  }
  int getWidth() {
    return src.getWidth();
  }
  int getHeight() {
    return src.getHeight();
  }
  int getPreferredWidth() {
    return src.getPreferredWidth();
  }
  int getPreferredHeight() {
    return src.getPreferredHeight();
  }
  double getAspectRatio() {
    return src.getAspectRatio();
  }
};


class GFilter_GammaCorrection : public GFilter {
  /*
   * gamma correction filter
   */

 protected:
  int min, range;
  short* table;

 public:
  GFilter_GammaCorrection(GImage& image, float gamma=1)
    : GFilter(image) {
    table=NULL;
    min=-16384;
    range=32768;
    setGammaCorrection(gamma);
  }

  ~GFilter_GammaCorrection() {
    if (table)
      delete[] table;
  }

  void setGammaCorrection(float gamma=1);

  void getPixel(short& red, short& green, short& blue,
		float x1, float y1, float x2, float y2);
  void getScanLineHorz(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
  void getScanLineVert(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
};


class GFilter_ContrastAdjustment : public GFilter {
  /*
   * contrast adjustment filter
   */

 protected:
  int min;
  int diff;

 public:
  GFilter_ContrastAdjustment(GImage& image, int min, int max)
    : GFilter(image) {
    setContrastAdjustment(min,max);
  }

  /* [min,max] is translated to [-16384,+16383]
   */
  void setContrastAdjustment(int mn, int mx) {
    min=mn;
    diff=mx-mn;
  }

  void getPixel(short& red, short& green, short& blue,
		float x1, float y1, float x2, float y2);
  void getScanLineHorz(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
  void getScanLineVert(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
};


class GFilter_WhiteBalance : public GFilter {
  /*
   * contrast adjustment filter
   */

 protected:
  int rdiff,gdiff,bdiff;

 public:
  GFilter_WhiteBalance(GImage& image, float red=1, float blue=1)
    : GFilter(image) {
    setWhiteLevel(red,blue);
  }

  /* green is implied
   */
  void setWhiteLevel(float red, float blue) {
    float green=1;
    if (red>1) {
      green/=red;
      blue/=red;
      red=1;
    }
    if (blue>1) {
      red/=blue;
      green/=blue;
      blue=1;
    }
    rdiff=(int)(32678*red);
    gdiff=(int)(32678*green);
    bdiff=(int)(32678*blue);
  }

  void getPixel(short& red, short& green, short& blue,
		float x1, float y1, float x2, float y2);
  void getScanLineHorz(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
  void getScanLineVert(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
};

class GFilter_Scale : public GFilter {
  /*
   * scaling filter
   */

 protected:
  double aspectratio;
  int w,h;

  float xfactor,xoffset;
  float yfactor,yoffset;

 public:
  GFilter_Scale(GImage& image)
    : GFilter(image) {
    aspectratio=src.getAspectRatio();
    w=src.getWidth();
    h=src.getHeight();
    calculateConstants();
  }

  /* if either of width or height are zero, it will be calculated;
   * both must not be zero
   */
  GFilter_Scale(GImage& image, int width, int height, double aspect=1)
    : GFilter(image) {
    w=width;
    h=height;
    aspectratio=aspect;
    if (w==0)
      w = (int)((h*src.getWidth()*src.getAspectRatio())/(src.getHeight()*aspectratio));
    else if (h==0)
      h = (int)((w*src.getHeight()*aspectratio)/(src.getWidth()*src.getAspectRatio()));
    calculateConstants();
  }

  /* changing the aspect ratio does not change the image dimensions;
   * therefore, the image may end up cropped if the dimensions are not reset
   */
  void setAspectRatio(double ratio) {
    aspectratio=ratio;
    calculateConstants();
  }

  /* sets the maximum dimensions the image may have; one dimension may be
   * set smaller to maintain aspect ratio and not crop the image
   */
  void setMaximumDimensions(int width, int height) {
    w=width;
    h=height;
    double temp = (src.getWidth()*src.getAspectRatio())/(src.getHeight()*aspectratio);
    int ow = (int)(h*temp);
    if (ow<w)
      w=ow;
    else {
      int oh = (int)(w/temp);
      if (oh<h)
	h=oh;
    }
    calculateConstants();
  }

  /* sets the minimum dimensions the image may have; one dimension may be
   * set larger to maintain aspect ratio and not crop the image;
   * if you wish to only set one dimension, use this method by specifying
   * the other as zero (it will be automatically set as appropriate)
   */
  void setMinimumDimensions(int width, int height) {
    w=width;
    h=height;
    double temp = (src.getWidth()*src.getAspectRatio())/(src.getHeight()*aspectratio);
    int ow = (int)(h*temp);
    if (ow>w)
      w=ow;
    else {
      int oh = (int)(w/temp);
      if (oh>h)
	h=oh;
    }
    calculateConstants();
  }

  /* sets the image dimensions exactly; if crop=true, the image may be
   * cropped to maintain aspect ratio; if crop=false, the aspect ratio
   * will be changed to ensure that the image is not cropped
   */
  void setDimensions(int width, int height, bool crop=true) {
    w=width;
    h=height;
    if (!crop)
      aspectratio=h*src.getWidth()*src.getAspectRatio()/(w*src.getHeight());
    calculateConstants();
  }

  int getWidth() {
    return w;
  }
  int getUncropWidth() {
    int ow=(int)((h*src.getWidth()*src.getAspectRatio())/(src.getHeight()*aspectratio));
    return ow>w?ow:w;
  }
  int getHeight() {
    return h;
  }
  int getUncropHeight() {
    int oh=(int)((w*src.getHeight()*aspectratio)/(src.getWidth()*src.getAspectRatio()));
    return oh>h?oh:h;
  }
  int getPreferredWidth() {
    return w;
  }
  int getPreferredHeight() {
    return h;
  }
  double getAspectRatio() {
    return aspectratio;
  }

  void getPixel(short& red, short& green, short& blue,
		float x1, float y1, float x2, float y2);
  void getScanLineHorz(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
  void getScanLineVert(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
  
 private:
  void calculateConstants();

};


class GFilter_Rotate : public GFilter {
  /*
   * rotate filter
   */

 protected:
  int degree;

 public:
  GFilter_Rotate(GImage& image, int angle=0)
    : GFilter(image) {
    degree=(360+angle)%360;
    if (degree%90!=0)
      fprintf(stderr,"GFilter_Rotate: rotate angle must be multiple of 90 degrees %d\n",degree);
  }

  void setClockwiseRotate(int angle=90) {
    degree=(360+angle)%360;
    if (degree%90!=0)
      fprintf(stderr,"GFilter_Rotate: rotate angle must be multiple of 90 degrees %d\n",degree);
  }

  void setCounterClockwiseRotate(int angle=90) {
    degree=(360-angle)%360;
    if (degree%90!=0)
      fprintf(stderr,"GFilter_Rotate: rotate angle must be multiple of 90 degrees %d\n",degree);
  }

  int getWidth() {
    return (degree==90)||(degree==270) ? src.getHeight() : src.getWidth();
  }
  int getHeight() {
    return (degree==90)||(degree==270) ? src.getWidth() : src.getHeight();
  }
  int getPreferredWidth() {
    return (degree==90)||(degree==270) ? src.getPreferredHeight() : src.getPreferredWidth();
  }
  int getPreferredHeight() {
    return (degree==90)||(degree==270) ? src.getPreferredWidth() : src.getPreferredHeight();
  }
  double getAspectRatio() {
    return (degree==90)||(degree==270) ? 1/src.getAspectRatio() : src.getAspectRatio();
  }

  void getPixel(short& red, short& green, short& blue,
		float x1, float y1, float x2, float y2);
  void getScanLineHorz(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
  void getScanLineVert(short* red, short* green, short* blue,
		       float x1, float y1, float x2, float y2, int npixels);
  
};

#endif