File: Image.h

package info (click to toggle)
primrose 6%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,300 kB
  • ctags: 3,385
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 95; perl: 67
file content (639 lines) | stat: -rw-r--r-- 16,360 bytes parent folder | download | duplicates (15)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
 * Modification History
 *
 * 2000-December-21		Jason Rohrer
 * Created. 
 *
 * 2001-January-6		Jason Rohrer
 * Fixed a bug in filter( ChannelFilter * ).
 * Set mSelection to NULL by default.
 *
 * 2001-January-10		Jason Rohrer
 * Made class serializable. 
 *
 * 2001-January-15		Jason Rohrer
 * Made copy() not-virtual, so it can be overridden by subclasses 
 * while allowing pointer type to determine which function 
 * implementation is invoked. 
 *
 * 2001-January-31		Jason Rohrer
 * Fixed a bug in copy().
 *
 * 2001-February-3		Jason Rohrer
 * Updated serialization code to use new interfaces. 
 *
 * 2001-February-4		Jason Rohrer
 * Rewrote the serialization code to send the image across as a byte
 * array with one byte per channel.  This will reduce the transfer size by
 * a factor of 8.  Keeping images in double format is convennient for image
 * creation, but the added quality never affects the end user anyway, so
 * there's no point in sending the extra data to a stream.
 * Removed an unused array allocation.
 *
 * 2005-February-21		Jason Rohrer
 * Made destructor virtual to avoid compiler warnings.
 *
 * 2006-August-25		Jason Rohrer
 * Made zero init of pixels optional (for speed).
 *
 * 2008-September-25		Jason Rohrer
 * Added a sub-image function and setting/getting color functions.
 */
 
 
#ifndef IMAGE_INCLUDED
#define IMAGE_INCLUDED

#include <stdio.h> 
#include <string.h>
 
#include "ChannelFilter.h"
#include "Color.h"

#include "minorGems/io/Serializable.h"

 
/**
 * A multi-channel, double-valued image.
 *
 * Is Serializable.  Note that a serialized image doesn't have a selection.
 *
 * @author Jason Rohrer 
 */
class Image : public Serializable { 
	
	public:
		
		/**
		 * Constructs an image.
		 *
		 * @param inWidth width of image in pixels.
		 * @param inHeight height of image in pixels.
		 * @param inNumChannels number of channels in image.
         * @param inStartPixelsAtZero true to initialize all pixels
         *   to zero, or false to leave default memory values (garbage)
         *   in place (pixels must be initialized by caller in this case).
         *   Defaults to true.
		 */
		Image( int inWidth, int inHeight, int inNumChannels,
               char inStartPixelsAtZero = true );
		
		
		virtual ~Image();
		
		
		// gets the dimensions of this image.
		virtual long getWidth();
		virtual long getHeight();
		virtual long getNumChannels();
		
		/**
		 * Gets the values of a particular channel.
		 *
		 * Values are not copied.
		 *
		 * @param inChannel the channel to get.
		 * 
		 * @return the values of the specified channel in row-major order.
		 */
		virtual double *getChannel( int inChannel );
        


        /**
         * Gets the 3- or 4-channel color value at a given location in the 
         * image.
         *
         * @param inIndex the image index.
         *
         * @return a color object.
         */
        virtual Color getColor( int inIndex );
        

        
        /**
         * Sets the 3- or 4-channel color value at a given location in the 
         * image.
         *
         * @param inIndex the image index.
         * @param inColor the new color to set.
         */
        virtual void setColor( int inIndex, Color inColor );
        

		
		/**
		 * Selects a region of the image.  Default is a clear selection,
		 * which means all regions of image are affected by an applied
		 * filter.
		 *
		 * @param inSelection the image to use as the selection mask.
		 *   Values of 0 indicate pixels that are not selection, and 1
		 *   indicate pixels that are selected, with selection amount
		 *   varying linearly between 0 and 1.
		 *   If inSelection is a single channel, then that channel is 
		 *   used as a selection mask for all channels in this image.
		 *   If inSelection contains the same number of channels as this 
		 *   image, then the corresponding channels of inSelection are
		 *   are used to mask each channel of this image.
		 *   If inSelection contains a number of channels different
		 *   from the number in this image, the first channel of inSelection
		 *   is used to mask each channel in this image.
		 *
		 * Note that inSelection is not copied or destroyed by this class.
		 * Thus, modifying inSelection after calling setSelection will
		 * modify the selection in this image.
		 */
		virtual void setSelection( Image *inSelection );
		
		
		
		/**
		 * Gets the selection for this image.
		 *
		 * @return the selection for this image.  Returns NULL
		 *   if there is no selection.  Must not be destroyed
		 *   by caller before calling clearSelection.
		 */
		virtual Image *getSelection();
		
		
		
		/**
		 * Clears the selection.  Effectively selects the entire image.
		 */
		virtual void clearSelection();
		
		
		/**
		 * Applies a filter to the selected region of 
		 * a specified channel of this image.
		 *
		 * @param inFilter the filter to apply.
		 * @param inChannel the channel to filter.
		 */
		virtual void filter( ChannelFilter *inFilter, int inChannel );
		
		 
		/**
		 * Applies a filter to the selected region of 
		 * all channels of this image.
		 *
		 * @param inFilter the filter to apply.
		 */
		virtual void filter( ChannelFilter *inFilter );
		
		
		/**
		 * Copies the selected region of this image.  Not virtual,
		 * so can be overridden by subclasses while allowing pointer
		 * type to determine which function implementation is invoked.
		 *
		 * @return a new image with the same number of channels
		 *   as this image, each containing the selected region
		 *   from each corresponding channel of this image.  Unselected
		 *   regions are set to black.  Returned image has no selection.
		 */
		Image *copy();
		
		
		/**
		 * Pastes the selected region from another image into
		 * the selected region of this image.
		 *
		 * @param inImage the image to paste.  Let c be the number
		 *   of channels in this image, and cp be the number
		 *   of channels in the image being pasted.
		 *   If c<cp, then only the first c channels of inImage
		 *   are pasted.  If c>cp, then only the first cp channels
		 *   of this image are pasted into.
		 */
		virtual void paste( Image *inImage );
		
		
		/**
		 * Copies the data from the selected region of a channel.
		 *
		 * @param inChannel the channel to copy.
		 *
		 * @return a copy of the channel data.  Must be destroyed
		 *   by the caller.
		 */
		virtual double *copyChannel( int inChannel );
		
		
		
		/**
		 * Pastes channel data into the selected region of a specified channel.
		 *
		 * @param inChannelData an array containing the channel 
		 *   data to be pasted.  Must be destroyed by caller.
		 * @param inChannel the channel to paste into.
		 */
		virtual void pasteChannel( double *inChannelData, int inChannel );
		
	
		/**
		 * Gets the mask for a specified channel.
		 *
		 * @param inChannel the channel to get a mask for.
		 *
		 * @return the mask data for the specified channel.
		 *   If selection has the same number of channels as this image
		 *   then a different mask is returned for each channel.  Otherwise,
		 *   the first channel from the selection is returned as the
		 *   mask for every channel.  Returns NULL if there is no selection.
		 */
		virtual double *getChannelSelection( int inChannel );

		

        /**
         * Extracts a smaller sub-image from this image.
         *
         * Ignores current selection.
         *
         * @param inStartX, inStartY, inWidth, inHeight
         *   coordinates for the top left corner pixel of the sub-image
         *   and the width and height of the sub-image.
         *
         * @return the sub-image as a new image.  Must be destoryed by caller.
         */ 
        Image *getSubImage( int inStartX, int inStartY, 
                            int inWidth, int inHeight );
        

		
		// implement the Serializable interface
		virtual int serialize( OutputStream *inOutputStream );
		virtual int deserialize( InputStream *inInputStream );
	
	protected:
		long mWide, mHigh, mNumPixels, mNumChannels;
		
		double **mChannels;
		
		// NULL if nothing selected.
		Image *mSelection;
		
		
		
		
		
		/**
		 * Pastes masked channel data into the selected region of a 
		 * specified channel.
		 *
		 * @param inChannelData an array containing the channel 
		 *   data to be pasted.  Must be destroyed by caller.
		 * @param inMask the selection mask to use for passed-in channel.
		 *   Set to NULL for no mask.
		 * @param inChannel the channel to paste into.
		 */
		virtual void pasteChannel( double *inChannelData, double *inMask,
			int inChannel );
		
	};



inline Image::Image( int inWidth, int inHeight, int inNumChannels,
                     char inStartPixelsAtZero ) 
	: mWide( inWidth ), mHigh( inHeight ), mNumPixels( inWidth * inHeight ),
	mNumChannels( inNumChannels ), mChannels( new double*[inNumChannels] ),
	mSelection( NULL ) {
	
	// initialize all channels
	for( int i=0; i<mNumChannels; i++ ) {
		mChannels[i] = new double[ mNumPixels ];
        if( inStartPixelsAtZero ) {
            for( int j=0; j<mNumPixels; j++ ) {
                mChannels[i][j] = 0.0;
                }
            }
		}
	}
		
		
		
inline Image::~Image() {
	for( int i=0; i<mNumChannels; i++ ) {
		delete [] mChannels[i];
		}
	delete [] mChannels;	
	}
		


inline long Image::getWidth() {
	return mWide;
	}
	
	
	
inline long Image::getHeight() {
	return mHigh;
	}



inline long Image::getNumChannels() {
	return mNumChannels;
	}
	
	
		
inline double *Image::getChannel( int inChannel ) {
	return mChannels[ inChannel ];
	}
	


inline Color Image::getColor( int inIndex ) {
    Color c;
    
    for( int i=0; i<mNumChannels && i < 4; i++ ) {
        c[i] = (float)( mChannels[i][inIndex] );
        }
    
    return c;
    }



inline void Image::setColor( int inIndex, Color inColor ) {
    for( int i=0; i<mNumChannels && i < 4; i++ ) {
        mChannels[i][inIndex] = (double)( inColor[i] );
        }
    }

		 
		
inline void Image::setSelection( Image *inSelection ) {
	mSelection = inSelection;
	}



inline Image *Image::getSelection() {
	return mSelection;
	}
		
		
		
inline void Image::clearSelection() {
	mSelection = NULL;
	}
	
	
		
inline void Image::filter( ChannelFilter *inFilter, int inChannel ) {

	if( mSelection == NULL ) {
		inFilter->apply( mChannels[ inChannel ], mWide, mHigh );
		}
	else { // part of image selected
		// turn selection off and filter channel entirely
		Image *tempSelection = mSelection;
		mSelection = NULL;
		// filter a copy of the channel
		double *filteredChannel = copyChannel( inChannel );
		inFilter->apply( filteredChannel, mWide, mHigh );
		
		// now paste filtered channel back into selected region
		mSelection = tempSelection;
		pasteChannel( filteredChannel, inChannel );
		}
	}
		
	
		
inline void Image::filter( ChannelFilter *inFilter ) {
	for( int i=0; i<mNumChannels; i++ ) {
		filter( inFilter, i );
		}	
	}



inline Image *Image::copy() {
	Image *copiedImage = new Image( mWide, mHigh, mNumChannels );
	copiedImage->paste( this );
	
	return copiedImage;
	}
		
		
		
inline void Image::paste( Image *inImage ) {
	// copy paste in the min number of channels only
	int numChannelsToPaste = mNumChannels;
	if( numChannelsToPaste > inImage->getNumChannels() ) {
		numChannelsToPaste = inImage->getNumChannels();
		}
	
	for( int i=0; i<numChannelsToPaste; i++ ) {
		pasteChannel( inImage->getChannel(i), 
			inImage->getChannelSelection(i), i );
		}
	}



inline double *Image::copyChannel( int inChannel ) {
	
	// first, copy the channel
	double *copiedChannel = new double[mNumPixels];
	memcpy( copiedChannel, 
		mChannels[inChannel], sizeof( double ) * mNumPixels );
	
	
	if( mSelection != NULL ) {
		// apply selection to copied channel
		double *selection = getChannelSelection( inChannel );	
		
		// scale copied channel with selection
		for( int i=0; i<mNumPixels; i++ ) {
			copiedChannel[i] = copiedChannel[i] * selection[i];
			}
		}
	
	return copiedChannel;	
	}



inline double *Image::getChannelSelection( int inChannel ) {
	if( mSelection == NULL ) {
		return NULL;
		}
	else {
		if( mSelection->getNumChannels() == mNumChannels ) {
			// use separate selection for each channel
			return mSelection->getChannel( inChannel );
			}
		else {
			// use first channel of selection for all channels
			return mSelection->getChannel( 0 );
			}
		}
	}


		
inline void Image::pasteChannel( double *inChannelData, int inChannel ) {
	pasteChannel( inChannelData, NULL, inChannel );
	}



// We've abstracted away the complexity in the other fuctions,
// but it all seemed to filter down into this function, which
// is very messy.
inline void Image::pasteChannel( double *inChannelData, double *inMask,
	int inChannel ) {
	
	double *thisChannel = mChannels[inChannel];
	if( mSelection != NULL ) {
		// scale incoming data with this selection
		double *selection = getChannelSelection(inChannel);
		
		if( inMask != NULL ) {
			// scale incoming data with both masks
			for( int i=0; i<mNumPixels; i++ ) {
				thisChannel[i] = ( thisChannel[i] * (1-selection[i])
					+ 
					( thisChannel[i] * (1-inMask[i]) 
						+ inChannelData[i] * inMask[i] ) * selection[i] );
				}
			}
		else {	
			// scale incomming data with this selecition only
			for( int i=0; i<mNumPixels; i++ ) {
				thisChannel[i] = ( thisChannel[i] * (1-selection[i])
					+ inChannelData[i] * selection[i] );
				}
			}
		}
	else {	// no selection in this image
		if( inMask != NULL ) {
			// scale incoming data with its masks
			for( int i=0; i<mNumPixels; i++ ) {
				thisChannel[i] = thisChannel[i] * (1-inMask[i]) 
					+ inChannelData[i] * inMask[i];
				}
			}
		else {
			// copy channel directly, with no mask
			memcpy( thisChannel, inChannelData, sizeof(double) * mNumPixels );
			}
		}	
	}



inline Image *Image::getSubImage( int inStartX, int inStartY, 
                           int inWidth, int inHeight ) {
    
    int endY = inStartY + inHeight;
    
    Image *destImage = new Image( inWidth, inHeight, mNumChannels, false );

    for( int c=0; c<mNumChannels; c++ ) {
        double *destChannel = destImage->getChannel( c );
        double *sourceChannel = mChannels[c];
        
        int destY=0;
        for( int y=inStartY; y<endY; y++ ) {
            
            // copy row
            memcpy( &( destChannel[ destY * inWidth ] ),
                    &( sourceChannel[ y * mWide + inStartX ] ),
                    sizeof( double ) * inWidth );
            
            destY ++;
            }
        }
    
    return destImage;
    }

    

inline int Image::serialize( OutputStream *inOutputStream ) {
	// first output width and height
	
	int numBytes = 0;
	
	numBytes += inOutputStream->writeLong( mWide );
	numBytes += inOutputStream->writeLong( mHigh );
	
	// then output number of channels
	numBytes += inOutputStream->writeLong( mNumChannels );
	
	// now output each channel
	for( int i=0; i<mNumChannels; i++ ) {
		unsigned char *byteArray = new unsigned char[mNumPixels];
		
		// convert each 8-bit double pixel to one byte
		for( int p=0; p<mNumPixels; p++ ) {
			//numBytes += inOutputStream->writeDouble( mChannels[i][p] );
			byteArray[p] = (unsigned char)( mChannels[i][p] * 255 );
			}
		
		numBytes += inOutputStream->write( byteArray, mNumPixels );
			
		delete [] byteArray;	
		}
	
	return numBytes;
	}
	
	
	
inline int Image::deserialize( InputStream *inInputStream ) {
	int i;
	// first delete old image channels
	for( i=0; i<mNumChannels; i++ ) {
		delete [] mChannels[i];
		}
	delete [] mChannels;
	
	
	// input width and height
	
	int numBytes = 0;
	
	numBytes += inInputStream->readLong( &mWide );
	numBytes += inInputStream->readLong( &mHigh );
	
	mNumPixels = mWide * mHigh;
	
	// then input number of channels
	numBytes += inInputStream->readLong( &mNumChannels );
	
	mChannels = new double*[mNumChannels];
	
	// now input each channel
	for( i=0; i<mNumChannels; i++ ) {
		mChannels[i] = new double[mNumPixels];
		
		unsigned char *byteArray = new unsigned char[mNumPixels];
		numBytes += inInputStream->read( byteArray, mNumPixels );
		
		// convert each byte to an 8-bit double pixel
		for( int p=0; p<mNumPixels; p++ ) {
			//numBytes += inInputStream->readDouble( &( mChannels[i][p] ) );
			mChannels[i][p] = (double)( byteArray[p] ) / 255.0;
			}
		
		delete [] byteArray;	
		}
	
	return numBytes;
	}


	
#endif