scale2x.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/graphics/scale2x/scale2x.hpp"
30 
31 namespace nom {
32  namespace priv {
33 
34 bool scale2x ( void* source_buffer, void* destination_buffer,
35  const int32 source_width, const int32 source_height,
36  const int32 depth,
37  const int16 source_pitch, int16 destination_pitch
38  )
39 {
40  // Save a temporary copy of the *existing* width & height for scaling
41  // calculation.
42  int32 width = source_width;
43  int32 height = source_height;
44 
45  // The existing video surface pitch (width) is used for scaling calculations.
46  uint16 srcpitch = source_pitch;
47 
48  // We must use the new video surface configuration for computing the pitch as
49  // this is dependent upon width & height parameters.
50  uint16 dstpitch = destination_pitch;
51 
52  // Existing & resulting pixel arrays
53  uint8* srcpix = static_cast<uint8*> ( source_buffer );
54  uint8* dstpix = static_cast<uint8*> ( destination_buffer );
55 
56  // Use the existing video surface BPP for choosing scaling algorithm.
57  switch ( depth )
58  {
59  default: // Err, we could not determine a valid color depth!
60  {
61  return false;
62  break;
63  } // end unsupported color depth
64 
65  case 8:
66  {
67  uint8 E0, E1, E2, E3, B, D, E, F, H;
68 
69  for ( int32 looph = 0; looph < height; ++looph)
70  {
71  for ( int32 loopw = 0; loopw < width; ++loopw)
72  {
73  B = *(uint8*)(srcpix + (std::max ( 0, looph - 1 ) * srcpitch ) + ( 1 * loopw ) );
74  D = *(uint8*)(srcpix + ( looph * srcpitch ) + ( 1 * std::max ( 0,loopw - 1 ) ) );
75  E = *(uint8*)(srcpix + ( looph * srcpitch ) + ( 1 * loopw ) );
76  F = *(uint8*)(srcpix + ( looph * srcpitch ) + ( 1 * std::min ( width - 1,loopw + 1 ) ) );
77  H = *(uint8*)(srcpix + ( std::min ( height - 1, looph + 1 ) * srcpitch ) + ( 1 * loopw ) );
78 
79  E0 = D == B && B != F && D != H ? D : E;
80  E1 = B == F && B != D && F != H ? F : E;
81  E2 = D == H && D != B && H != F ? D : E;
82  E3 = H == F && D != H && B != F ? F : E;
83 
84  *(uint8*)(dstpix + looph*2*dstpitch + loopw*2*1) = E0;
85  *(uint8*)(dstpix + looph*2*dstpitch + (loopw*2+1)*1) = E1;
86  *(uint8*)(dstpix + (looph*2+1)*dstpitch + loopw*2*1) = E2;
87  *(uint8*)(dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*1) = E3;
88  } // for width loop
89  } // for height loop
90  break;
91  } // end case 8
92 
93  case 16:
94  {
95  uint16 E0, E1, E2, E3, B, D, E, F, H;
96 
97  for ( int32 looph = 0; looph < height; ++looph)
98  {
99  for ( int32 loopw = 0; loopw < width; ++loopw)
100  {
101  B = *(uint16*)(srcpix + ( std::max ( 0, looph - 1 ) * srcpitch ) + ( 2 * loopw ) );
102  D = *(uint16*)(srcpix + ( looph * srcpitch ) + ( 2 * std::max ( 0, loopw - 1 ) ) );
103  E = *(uint16*)(srcpix + ( looph * srcpitch ) + ( 2 * loopw ) );
104  F = *(uint16*)(srcpix + ( looph * srcpitch ) + ( 2 * std::min ( width - 1, loopw + 1 ) ) );
105  H = *(uint16*)(srcpix + ( std::min ( height - 1, looph + 1 ) * srcpitch ) + ( 2 * loopw ) );
106 
107  E0 = D == B && B != F && D != H ? D : E;
108  E1 = B == F && B != D && F != H ? F : E;
109  E2 = D == H && D != B && H != F ? D : E;
110  E3 = H == F && D != H && B != F ? F : E;
111 
112  *(uint16*)(dstpix + looph*2*dstpitch + loopw*2*2) = E0;
113  *(uint16*)(dstpix + looph*2*dstpitch + (loopw*2+1)*2) = E1;
114  *(uint16*)(dstpix + (looph*2+1)*dstpitch + loopw*2*2) = E2;
115  *(uint16*)(dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*2) = E3;
116  } // for width loop
117  } // for height loop
118  break;
119  } // end case 16
120 
121  case 24:
122  {
123  int32 E0, E1, E2, E3, B, D, E, F, H;
124 
125  for ( int32 looph = 0; looph < height; ++looph)
126  {
127  for ( int32 loopw = 0; loopw < width; ++loopw)
128  {
129  B = SCALE2x_READINT24(srcpix + (std::max(0,looph-1)*srcpitch) + (3*loopw));
130  D = SCALE2x_READINT24(srcpix + (looph*srcpitch) + (3*std::max(0,loopw-1)));
131  E = SCALE2x_READINT24(srcpix + (looph*srcpitch) + (3*loopw));
132  F = SCALE2x_READINT24(srcpix + (looph*srcpitch) + (3*std::min(width-1,loopw+1)));
133  H = SCALE2x_READINT24(srcpix + (std::min(height-1,looph+1)*srcpitch) + (3*loopw));
134 
135  E0 = D == B && B != F && D != H ? D : E;
136  E1 = B == F && B != D && F != H ? F : E;
137  E2 = D == H && D != B && H != F ? D : E;
138  E3 = H == F && D != H && B != F ? F : E;
139 
140  SCALE2x_WRITEINT24((dstpix + looph*2*dstpitch + loopw*2*3), E0);
141  SCALE2x_WRITEINT24((dstpix + looph*2*dstpitch + (loopw*2+1)*3), E1);
142  SCALE2x_WRITEINT24((dstpix + (looph*2+1)*dstpitch + loopw*2*3), E2);
143  SCALE2x_WRITEINT24((dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*3), E3);
144  } // for width loop
145  } // for height loop
146  break;
147  } // end case 24
148 
149  case 32:
150  {
151  uint32 E0, E1, E2, E3, B, D, E, F, H;
152 
153  for ( int32 looph = 0; looph < height; ++looph)
154  {
155  for ( int32 loopw = 0; loopw < width; ++loopw)
156  {
157  B = *(uint32*)(srcpix + (std::max(0,looph-1)*srcpitch) + (4*loopw));
158  D = *(uint32*)(srcpix + (looph*srcpitch) + (4*std::max(0,loopw-1)));
159  E = *(uint32*)(srcpix + (looph*srcpitch) + (4*loopw));
160  F = *(uint32*)(srcpix + (looph*srcpitch) + (4*std::min(width-1,loopw+1)));
161  H = *(uint32*)(srcpix + (std::min(height-1,looph+1)*srcpitch) + (4*loopw));
162 
163  E0 = D == B && B != F && D != H ? D : E;
164  E1 = B == F && B != D && F != H ? F : E;
165  E2 = D == H && D != B && H != F ? D : E;
166  E3 = H == F && D != H && B != F ? F : E;
167 
168  *(uint32*)(dstpix + looph*2*dstpitch + loopw*2*4) = E0;
169  *(uint32*)(dstpix + looph*2*dstpitch + (loopw*2+1)*4) = E1;
170  *(uint32*)(dstpix + (looph*2+1)*dstpitch + loopw*2*4) = E2;
171  *(uint32*)(dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*4) = E3;
172  } // for width loop
173  } // for height loop
174  } // end case 32
175  break;
176  } // end switch (BytesPerPixel)
177 
178  return true;
179 }
180 
181 
182  } // namespace priv
183 } // namespace nom