1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski #ifndef INCLUDED_BASEBMP_SCALEIMAGE_HXX
25*b1cdbd2cSJim Jagielski #define INCLUDED_BASEBMP_SCALEIMAGE_HXX
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski #include <osl/diagnose.h>
28*b1cdbd2cSJim Jagielski
29*b1cdbd2cSJim Jagielski #include <vigra/tuple.hxx>
30*b1cdbd2cSJim Jagielski #include <vigra/copyimage.hxx>
31*b1cdbd2cSJim Jagielski #include <vigra/basicimage.hxx>
32*b1cdbd2cSJim Jagielski #include <vigra/iteratortraits.hxx>
33*b1cdbd2cSJim Jagielski
34*b1cdbd2cSJim Jagielski namespace basebmp
35*b1cdbd2cSJim Jagielski {
36*b1cdbd2cSJim Jagielski
37*b1cdbd2cSJim Jagielski template< class SourceIter, class SourceAcc,
38*b1cdbd2cSJim Jagielski class DestIter, class DestAcc >
scaleLine(SourceIter s_begin,SourceIter s_end,SourceAcc s_acc,DestIter d_begin,DestIter d_end,DestAcc d_acc)39*b1cdbd2cSJim Jagielski void scaleLine( SourceIter s_begin,
40*b1cdbd2cSJim Jagielski SourceIter s_end,
41*b1cdbd2cSJim Jagielski SourceAcc s_acc,
42*b1cdbd2cSJim Jagielski DestIter d_begin,
43*b1cdbd2cSJim Jagielski DestIter d_end,
44*b1cdbd2cSJim Jagielski DestAcc d_acc )
45*b1cdbd2cSJim Jagielski {
46*b1cdbd2cSJim Jagielski const int src_width = s_end - s_begin;
47*b1cdbd2cSJim Jagielski const int dest_width = d_end - d_begin;
48*b1cdbd2cSJim Jagielski
49*b1cdbd2cSJim Jagielski OSL_ASSERT( src_width > 0 && dest_width > 0 );
50*b1cdbd2cSJim Jagielski
51*b1cdbd2cSJim Jagielski if( src_width >= dest_width )
52*b1cdbd2cSJim Jagielski {
53*b1cdbd2cSJim Jagielski // shrink
54*b1cdbd2cSJim Jagielski int rem = 0;
55*b1cdbd2cSJim Jagielski while( s_begin != s_end )
56*b1cdbd2cSJim Jagielski {
57*b1cdbd2cSJim Jagielski if( rem >= 0 )
58*b1cdbd2cSJim Jagielski {
59*b1cdbd2cSJim Jagielski d_acc.set( s_acc(s_begin), d_begin );
60*b1cdbd2cSJim Jagielski
61*b1cdbd2cSJim Jagielski rem -= src_width;
62*b1cdbd2cSJim Jagielski ++d_begin;
63*b1cdbd2cSJim Jagielski }
64*b1cdbd2cSJim Jagielski
65*b1cdbd2cSJim Jagielski rem += dest_width;
66*b1cdbd2cSJim Jagielski ++s_begin;
67*b1cdbd2cSJim Jagielski }
68*b1cdbd2cSJim Jagielski }
69*b1cdbd2cSJim Jagielski else
70*b1cdbd2cSJim Jagielski {
71*b1cdbd2cSJim Jagielski // enlarge
72*b1cdbd2cSJim Jagielski int rem = -dest_width;
73*b1cdbd2cSJim Jagielski while( d_begin != d_end )
74*b1cdbd2cSJim Jagielski {
75*b1cdbd2cSJim Jagielski if( rem >= 0 )
76*b1cdbd2cSJim Jagielski {
77*b1cdbd2cSJim Jagielski rem -= dest_width;
78*b1cdbd2cSJim Jagielski ++s_begin;
79*b1cdbd2cSJim Jagielski }
80*b1cdbd2cSJim Jagielski
81*b1cdbd2cSJim Jagielski d_acc.set( s_acc(s_begin), d_begin );
82*b1cdbd2cSJim Jagielski
83*b1cdbd2cSJim Jagielski rem += src_width;
84*b1cdbd2cSJim Jagielski ++d_begin;
85*b1cdbd2cSJim Jagielski }
86*b1cdbd2cSJim Jagielski }
87*b1cdbd2cSJim Jagielski }
88*b1cdbd2cSJim Jagielski
89*b1cdbd2cSJim Jagielski /** Scale an image using zero order interpolation (pixel replication)
90*b1cdbd2cSJim Jagielski
91*b1cdbd2cSJim Jagielski Source and destination range must be at least one pixel wide and
92*b1cdbd2cSJim Jagielski high.
93*b1cdbd2cSJim Jagielski
94*b1cdbd2cSJim Jagielski @param s_begin
95*b1cdbd2cSJim Jagielski Start iterator for source image
96*b1cdbd2cSJim Jagielski
97*b1cdbd2cSJim Jagielski @param s_end
98*b1cdbd2cSJim Jagielski End iterator for source image
99*b1cdbd2cSJim Jagielski
100*b1cdbd2cSJim Jagielski @param s_acc
101*b1cdbd2cSJim Jagielski Source accessor
102*b1cdbd2cSJim Jagielski
103*b1cdbd2cSJim Jagielski @param d_begin
104*b1cdbd2cSJim Jagielski Start iterator for destination image
105*b1cdbd2cSJim Jagielski
106*b1cdbd2cSJim Jagielski @param d_end
107*b1cdbd2cSJim Jagielski End iterator for destination image
108*b1cdbd2cSJim Jagielski
109*b1cdbd2cSJim Jagielski @param d_acc
110*b1cdbd2cSJim Jagielski Destination accessor
111*b1cdbd2cSJim Jagielski
112*b1cdbd2cSJim Jagielski @param bMustCopy
113*b1cdbd2cSJim Jagielski When true, scaleImage always copies source, even when doing 1:1
114*b1cdbd2cSJim Jagielski copy
115*b1cdbd2cSJim Jagielski */
116*b1cdbd2cSJim Jagielski template< class SourceIter, class SourceAcc,
117*b1cdbd2cSJim Jagielski class DestIter, class DestAcc >
scaleImage(SourceIter s_begin,SourceIter s_end,SourceAcc s_acc,DestIter d_begin,DestIter d_end,DestAcc d_acc,bool bMustCopy=false)118*b1cdbd2cSJim Jagielski void scaleImage( SourceIter s_begin,
119*b1cdbd2cSJim Jagielski SourceIter s_end,
120*b1cdbd2cSJim Jagielski SourceAcc s_acc,
121*b1cdbd2cSJim Jagielski DestIter d_begin,
122*b1cdbd2cSJim Jagielski DestIter d_end,
123*b1cdbd2cSJim Jagielski DestAcc d_acc,
124*b1cdbd2cSJim Jagielski bool bMustCopy=false )
125*b1cdbd2cSJim Jagielski {
126*b1cdbd2cSJim Jagielski const int src_width ( s_end.x - s_begin.x );
127*b1cdbd2cSJim Jagielski const int src_height( s_end.y - s_begin.y );
128*b1cdbd2cSJim Jagielski
129*b1cdbd2cSJim Jagielski const int dest_width ( d_end.x - d_begin.x );
130*b1cdbd2cSJim Jagielski const int dest_height( d_end.y - d_begin.y );
131*b1cdbd2cSJim Jagielski
132*b1cdbd2cSJim Jagielski if( !bMustCopy &&
133*b1cdbd2cSJim Jagielski src_width == dest_width &&
134*b1cdbd2cSJim Jagielski src_height == dest_height )
135*b1cdbd2cSJim Jagielski {
136*b1cdbd2cSJim Jagielski // no scaling involved, can simply copy
137*b1cdbd2cSJim Jagielski vigra::copyImage( s_begin, s_end, s_acc,
138*b1cdbd2cSJim Jagielski d_begin, d_acc );
139*b1cdbd2cSJim Jagielski return;
140*b1cdbd2cSJim Jagielski }
141*b1cdbd2cSJim Jagielski
142*b1cdbd2cSJim Jagielski typedef vigra::BasicImage<typename SourceAcc::value_type> TmpImage;
143*b1cdbd2cSJim Jagielski typedef typename TmpImage::traverser TmpImageIter;
144*b1cdbd2cSJim Jagielski
145*b1cdbd2cSJim Jagielski TmpImage tmp_image(src_width,
146*b1cdbd2cSJim Jagielski dest_height);
147*b1cdbd2cSJim Jagielski TmpImageIter t_begin = tmp_image.upperLeft();
148*b1cdbd2cSJim Jagielski
149*b1cdbd2cSJim Jagielski // scale in y direction
150*b1cdbd2cSJim Jagielski for( int x=0; x<src_width; ++x, ++s_begin.x, ++t_begin.x )
151*b1cdbd2cSJim Jagielski {
152*b1cdbd2cSJim Jagielski typename SourceIter::column_iterator s_cbegin = s_begin.columnIterator();
153*b1cdbd2cSJim Jagielski typename TmpImageIter::column_iterator t_cbegin = t_begin.columnIterator();
154*b1cdbd2cSJim Jagielski
155*b1cdbd2cSJim Jagielski scaleLine(s_cbegin, s_cbegin+src_height, s_acc,
156*b1cdbd2cSJim Jagielski t_cbegin, t_cbegin+dest_height, tmp_image.accessor());
157*b1cdbd2cSJim Jagielski }
158*b1cdbd2cSJim Jagielski
159*b1cdbd2cSJim Jagielski t_begin = tmp_image.upperLeft();
160*b1cdbd2cSJim Jagielski
161*b1cdbd2cSJim Jagielski // scale in x direction
162*b1cdbd2cSJim Jagielski for( int y=0; y<dest_height; ++y, ++d_begin.y, ++t_begin.y )
163*b1cdbd2cSJim Jagielski {
164*b1cdbd2cSJim Jagielski typename DestIter::row_iterator d_rbegin = d_begin.rowIterator();
165*b1cdbd2cSJim Jagielski typename TmpImageIter::row_iterator t_rbegin = t_begin.rowIterator();
166*b1cdbd2cSJim Jagielski
167*b1cdbd2cSJim Jagielski scaleLine(t_rbegin, t_rbegin+src_width, tmp_image.accessor(),
168*b1cdbd2cSJim Jagielski d_rbegin, d_rbegin+dest_width, d_acc);
169*b1cdbd2cSJim Jagielski }
170*b1cdbd2cSJim Jagielski }
171*b1cdbd2cSJim Jagielski
172*b1cdbd2cSJim Jagielski /** Scale an image, range tuple version
173*b1cdbd2cSJim Jagielski
174*b1cdbd2cSJim Jagielski @param bMustCopy
175*b1cdbd2cSJim Jagielski When true, scaleImage always copies source, even when doing 1:1
176*b1cdbd2cSJim Jagielski copy
177*b1cdbd2cSJim Jagielski */
178*b1cdbd2cSJim Jagielski template< class SourceIter, class SourceAcc,
179*b1cdbd2cSJim Jagielski class DestIter, class DestAcc >
scaleImage(vigra::triple<SourceIter,SourceIter,SourceAcc> const & src,vigra::triple<DestIter,DestIter,DestAcc> const & dst,bool bMustCopy=false)180*b1cdbd2cSJim Jagielski inline void scaleImage( vigra::triple<SourceIter,SourceIter,SourceAcc> const& src,
181*b1cdbd2cSJim Jagielski vigra::triple<DestIter,DestIter,DestAcc> const& dst,
182*b1cdbd2cSJim Jagielski bool bMustCopy=false )
183*b1cdbd2cSJim Jagielski {
184*b1cdbd2cSJim Jagielski scaleImage(src.first,src.second,src.third,
185*b1cdbd2cSJim Jagielski dst.first,dst.second,dst.third,
186*b1cdbd2cSJim Jagielski bMustCopy);
187*b1cdbd2cSJim Jagielski }
188*b1cdbd2cSJim Jagielski
189*b1cdbd2cSJim Jagielski }
190*b1cdbd2cSJim Jagielski
191*b1cdbd2cSJim Jagielski #endif /* INCLUDED_BASEBMP_SCALEIMAGE_HXX */
192