~ubuntu-branches/ubuntu/feisty/photoprint/feisty

« back to all changes in this revision

Viewing changes to imagesource/imagesource_bilinear.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Milan Zamazal
  • Date: 2006-09-29 12:18:16 UTC
  • Revision ID: james.westby@ubuntu.com-20060929121816-6t2iz9zaymixd3om
Tags: upstream-0.3.3
ImportĀ upstreamĀ versionĀ 0.3.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * imagesource_bilinear.cpp - Interpolated scaling filter
 
3
 * Implements bilinear scaling
 
4
 *
 
5
 * Supports Greyscale, RGB and CMYK data
 
6
 * Doesn't (yet) support random access
 
7
 *
 
8
 * Copyright (c) 2004 by Alastair M. Robinson
 
9
 * Distributed under the terms of the GNU General Public License -
 
10
 * see the file named "COPYING" for more details.
 
11
 *
 
12
 */
 
13
 
 
14
#include <iostream>
 
15
#include <stdlib.h>
 
16
#include <string.h>
 
17
#include <math.h>
 
18
 
 
19
#include "imagesource_bilinear.h"
 
20
 
 
21
using namespace std;
 
22
 
 
23
 
 
24
ImageSource_Bilinear::~ImageSource_Bilinear()
 
25
{
 
26
        if(source)
 
27
                delete source;
 
28
 
 
29
        if(lastrow)
 
30
                free(lastrow);
 
31
}
 
32
 
 
33
 
 
34
ISDataType *ImageSource_Bilinear::GetRow(int row)
 
35
{
 
36
        int i;
 
37
 
 
38
        if(row==currentrow)
 
39
                return(rowbuffer);
 
40
 
 
41
        int srow1=(row*source->height)/height;
 
42
        int srow2=srow1+1;
 
43
        if(srow2>=source->height)
 
44
                srow2=srow1;
 
45
 
 
46
        ISDataType *src1,*src2;
 
47
 
 
48
        if(srow1==cachedrow)
 
49
        {
 
50
                src1=lastrow;
 
51
        }
 
52
        else
 
53
        {
 
54
                src1=source->GetRow(srow1);
 
55
                for(int i=0;i<source->width*source->samplesperpixel;++i)
 
56
                        lastrow[i]=src1[i];
 
57
                cachedrow=srow1;
 
58
                src1=lastrow;
 
59
        }
 
60
 
 
61
        src2=source->GetRow(srow2);
 
62
 
 
63
        double yfactor=row*source->height;
 
64
        yfactor/=height;
 
65
        yfactor-=srow1;
 
66
 
 
67
        for(i=0;i<width;++i)
 
68
        {
 
69
                int x1=(i*source->width)/width;
 
70
                int x2=x1+1;
 
71
                if(x2 >= source->width)
 
72
                        x2=x1;
 
73
                float xfactor=(i*source->width);
 
74
                xfactor/=width;
 
75
                xfactor-=x1;
 
76
 
 
77
                x1*=samplesperpixel;
 
78
                x2*=samplesperpixel;
 
79
 
 
80
                for(int s=0;s<samplesperpixel;++s)
 
81
                {
 
82
                        double s1=src1[x1+s];
 
83
                        double s2=src1[x2+s];
 
84
                        double s3=src2[x1+s];
 
85
                        double s4=src2[x2+s];
 
86
 
 
87
                        double a1=(1.0-xfactor)*s1+xfactor*s2;
 
88
                        double a2=(1.0-xfactor)*s3+xfactor*s4;
 
89
                        double a3=(1.0-yfactor)*a1+yfactor*a2;
 
90
 
 
91
                        rowbuffer[i*samplesperpixel+s]=int(a3);
 
92
                }
 
93
        }
 
94
 
 
95
        currentrow=row;
 
96
 
 
97
        return(rowbuffer);
 
98
}
 
99
 
 
100
 
 
101
ImageSource_Bilinear::ImageSource_Bilinear(struct ImageSource *source,int width,int height)
 
102
        : ImageSource(source), source(source), lastrow(NULL), cachedrow(-1)
 
103
{
 
104
        this->width=width; this->height=height;
 
105
        xres=(source->xres*width); xres/=source->width;
 
106
        yres=(source->yres*height); yres/=source->height;
 
107
 
 
108
        lastrow=(ISDataType *)malloc(source->width*source->samplesperpixel*sizeof(ISDataType));
 
109
        MakeRowBuffer();
 
110
        randomaccess=false;
 
111
}