1 /*
2  * imagesource_greyscale.cpp
3  *
4  * Supports RGB data
5  * Supports random access
6  *
7  * Copyright (c) 2008 by Alastair M. Robinson
8  * Distributed under the terms of the GNU General Public License -
9  * see the file named "COPYING" for more details.
10  *
11  */
12 
13 #include <iostream>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <math.h>
17 
18 #include "imagesource_greyscale.h"
19 
20 using namespace std;
21 
~ImageSource_Greyscale()22 ImageSource_Greyscale::~ImageSource_Greyscale()
23 {
24 	if(source)
25 		delete source;
26 }
27 
28 
GetRow(int row)29 ISDataType *ImageSource_Greyscale::GetRow(int row)
30 {
31 	int i;
32 
33 	if(row==currentrow)
34 		return(rowbuffer);
35 
36 	ISDataType *srcdata=source->GetRow(row);
37 
38 	switch(samplesperpixel)
39 	{
40 		case 1:
41 			for(i=0;i<width;++i)
42 			{
43 				int a=(srcdata[i*3]+srcdata[i*3+1]+srcdata[i*3+2])/3;
44 				rowbuffer[i]=a;
45 			}
46 			break;
47 		case 2:
48 			for(i=0;i<width;++i)
49 			{
50 				int a=(srcdata[i*4]+srcdata[i*4+1]+srcdata[i*4+2])/3;
51 				rowbuffer[i*2]=a;
52 				rowbuffer[i*2+1]=srcdata[i*4+3];
53 			}
54 			break;
55 	}
56 
57 	currentrow=row;
58 
59 	return(rowbuffer);
60 }
61 
62 
ImageSource_Greyscale(struct ImageSource * source)63 ImageSource_Greyscale::ImageSource_Greyscale(struct ImageSource *source)
64 	: ImageSource(source), source(source)
65 {
66 	switch(type)
67 	{
68 		case IS_TYPE_RGB:
69 			type=IS_TYPE_GREY;
70 			samplesperpixel=1;
71 			break;
72 		case IS_TYPE_RGBA:
73 			type=IS_TYPE_GREYA;
74 			samplesperpixel=2;
75 			break;
76 		default:
77 			throw "ImageSource_Greyscale - Unsupported type - only RGB is currently supported";
78 			break;
79 	}
80 	MakeRowBuffer();
81 }
82