~ubuntu-branches/ubuntu/karmic/photoprint/karmic

« back to all changes in this revision

Viewing changes to imagesource/imagesource_desaturate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Milan Zamazal
  • Date: 2007-05-01 16:32:13 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070501163213-ni1933khtg9fdvn5
Tags: 0.3.5-2
Move to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * imagesource_desaturate.cpp
 
3
 *
 
4
 * Supports Greyscale and RGB data
 
5
 * Supports random access
 
6
 *
 
7
 * Copyright (c) 2007 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
 * TODO: Support CMYK Data
 
12
 *
 
13
 */
 
14
 
 
15
#include <iostream>
 
16
#include <stdlib.h>
 
17
#include <string.h>
 
18
#include <math.h>
 
19
 
 
20
#include "imagesource_desaturate.h"
 
21
 
 
22
using namespace std;
 
23
 
 
24
ImageSource_Desaturate::~ImageSource_Desaturate()
 
25
{
 
26
        if(source)
 
27
                delete source;
 
28
}
 
29
 
 
30
 
 
31
ISDataType *ImageSource_Desaturate::GetRow(int row)
 
32
{
 
33
        if(row==currentrow)
 
34
                return(rowbuffer);
 
35
 
 
36
        ISDataType *srcdata=source->GetRow(row);
 
37
 
 
38
        switch(type)
 
39
        {
 
40
                case IS_TYPE_GREY:
 
41
                case IS_TYPE_GREYA:
 
42
                        // Grey images are already desaturated by definition
 
43
                        // so no-op.
 
44
                        return(srcdata);
 
45
                        break;
 
46
                case IS_TYPE_RGB:
 
47
                        {
 
48
                                for(int s=0;s<width*samplesperpixel;s+=samplesperpixel)
 
49
                                {
 
50
                                        int t=(srcdata[s]+srcdata[s+1]+srcdata[s+2])/3;
 
51
                                        rowbuffer[s]=rowbuffer[s+1]=rowbuffer[s+2]=t;
 
52
                                }
 
53
                        }
 
54
                        break;
 
55
                case IS_TYPE_RGBA:
 
56
                        {
 
57
                                for(int s=0;s<width*samplesperpixel;s+=samplesperpixel)
 
58
                                {
 
59
                                        int t=(srcdata[s]+srcdata[s+1]+srcdata[s+2])/3;
 
60
                                        rowbuffer[s]=rowbuffer[s+1]=rowbuffer[s+2]=t;
 
61
                                        rowbuffer[s+3]=srcdata[s+3];
 
62
                                }
 
63
                        }
 
64
                        break;
 
65
                default:
 
66
                        throw "Desaturate: type not (yet) handled";
 
67
        }
 
68
        
 
69
        currentrow=row;
 
70
 
 
71
        return(rowbuffer);
 
72
}
 
73
 
 
74
 
 
75
ImageSource_Desaturate::ImageSource_Desaturate(ImageSource *source)
 
76
        : ImageSource(source), source(source)
 
77
{
 
78
        if(STRIP_ALPHA(source->type)==IS_TYPE_CMYK)
 
79
                throw "Desaturate: CMYK Images not yet supported.";
 
80
 
 
81
        MakeRowBuffer();
 
82
}