KSquare Utilities
MorphOpStretcher.cpp
Go to the documentation of this file.
1 /* MorphOpStretcher.cpp -- Stretches image by a specified factor.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 
6 #include "FirstIncludes.h"
7 #include <stdlib.h>
8 #include <iostream>
9 #include <map>
10 #include <vector>
11 #include <math.h>
12 #include "MemoryDebug.h"
13 using namespace std;
14 
15 #include "MorphOp.h"
16 #include "MorphOpStretcher.h"
17 #include "Raster.h"
18 using namespace KKB;
19 
20 
21 
23 {
24 public:
26  destCellCount (0),
27  destCellIdxs (NULL),
28  destCellFracts (NULL)
29  {}
30 
31 
33  {
34  delete destCellIdxs; destCellIdxs = NULL;
35  delete destCellFracts; destCellFracts = NULL;
36  }
37 
38 
40  float factor
41  )
42  {
43  float cellStart = (float)cellIdx * factor;
44  float cellEnd = (float)(cellIdx + 1) * factor;
45 
46  kkuint32 cellStartIdx = (kkuint32)floor (cellStart);
47  kkuint32 cellEndIdx = (kkuint32)floor (cellEnd);
48  destCellCount = cellEndIdx - cellStartIdx;
49  if (cellEnd > floor(cellEnd))
50  ++destCellCount;
51 
52  delete destCellIdxs;
54 
55  delete destCellFracts;
56  destCellFracts = new float[destCellCount];
57 
58  float cellValue = cellStart;
59  float nextCellValue = cellStart;
60  for (kkuint32 idx = 0; idx < destCellCount; ++idx)
61  {
62  if (cellValue < ceil (cellValue))
63  nextCellValue = Min (cellEnd, ceilf (cellValue));
64  else
65  nextCellValue = Min (cellEnd, ceilf (cellValue + 1.0f));
66 
67  float fract = nextCellValue - cellValue;
68  destCellIdxs[idx] = cellStartIdx + idx;
69  destCellFracts[idx] = fract;
70  cellValue = nextCellValue;
71  }
72  }
73 
77 }; /* UpdateForSourceCellIdx */
78 
79 
80 
82  float _colFactor
83  ):
84  MorphOp (),
85  rowFactor (_rowFactor),
86  colFactor (_colFactor),
87  rowFactorsCount (0),
88  rowFactors (NULL),
89  colFactorsCount (0),
90  colFactors (NULL)
91 {
92 }
93 
94 
95 
97 {
98  delete[] rowFactors; rowFactors = NULL;
99  delete[] colFactors; colFactors = NULL;
100 }
101 
102 
103 
105 {
106  kkint32 result = sizeof (*this) +
107  (kkint32)(rowFactorsCount * sizeof (CellFactor) * rowFactor) +
108  (kkint32)(colFactorsCount * sizeof (CellFactor) * colFactor);
109  return result;
110 }
111 
112 
113 
114 RasterPtr MorphOpStretcher::PerformOperation (RasterConstPtr _image)
115 {
116  this->SetSrcRaster (_image);
117 
118  kkuint32 destHeight = (kkuint32)ceil (0.5f + (float)srcHeight * rowFactor);
119  kkuint32 destWidth = (kkuint32)ceil (0.5f + (float)srcWidth * colFactor);
120 
121  bool color = _image->Color ();
122 
123  UpdateFactors (srcHeight, srcWidth);
124 
125  RasterPtr result = new Raster (destHeight, destWidth, color);
126 
130 
131  uchar** destRed = result->Red ();
132  uchar** destGreen = result->Green ();
133  uchar** destBlue = result->Blue ();
134 
135  for (kkint32 srcRow = 0; srcRow < srcHeight; ++srcRow)
136  {
137  uchar* srcRedRow = NULL;
138  uchar* srcGreenRow = srcGreen[srcRow];
139  uchar* srcBlueRow = NULL;
140  if (color)
141  {
142  srcRedRow = srcRed [srcRow];
143  srcBlueRow = srcBlue[srcRow];
144  }
145 
146  CellFactor& rowFactor = rowFactors[srcRow];
147 
148  for (kkuint32 rowFactorIdx = 0; rowFactorIdx < rowFactor.destCellCount; ++rowFactorIdx)
149  {
150  kkuint32 destRow = rowFactor.destCellIdxs [rowFactorIdx];
151  float destRowFract = rowFactor.destCellFracts[rowFactorIdx];
152 
153  for (kkint32 srcCol = 0; srcCol < srcWidth; ++srcCol)
154  {
155  uchar srcPixelRed = 0;
156  uchar srcPixelGreen = srcGreenRow[srcCol];
157  uchar srcPixelBlue = 0;
158  if (color)
159  {
160  srcPixelRed = srcRedRow [srcCol];
161  srcPixelBlue = srcBlueRow[srcCol];
162  }
163 
164  CellFactor& colFactor = colFactors[srcCol];
165 
166  for (kkuint32 colFactorIdx = 0; colFactorIdx < colFactor.destCellCount; ++colFactorIdx)
167  {
168  kkuint32 destCol = colFactor.destCellIdxs [colFactorIdx];
169  float destColFract = colFactor.destCellFracts[colFactorIdx];
170 
171  destGreen[destRow][destCol] += (uchar)Min (255.0f, srcPixelGreen * destRowFract * destColFract);
172  if (color)
173  {
174  destRed [destRow][destCol] += (uchar)Min (255.0f, srcPixelRed * destRowFract * destColFract);
175  destBlue[destRow][destCol] += (uchar)Min (255.0f, srcPixelBlue * destRowFract * destColFract);
176  }
177  }
178  }
179  }
180  }
181 
182  return result;
183 } /* PerformOperation */
184 
185 
186 
187 
188 MorphOpStretcher::CellFactorPtr
189  MorphOpStretcher::BuildCellFactors (float factor,
190  kkuint32 cellFactorsCount
191  )
192 {
193  CellFactorPtr cellFactors = new CellFactor[cellFactorsCount];
194 
195  for (kkuint32 x = 0; x < cellFactorsCount; ++x)
196  {
197  cellFactors[x].UpdateForSourceCellIdx (x, factor);
198  }
199 
200  return cellFactors;
201 } /* BuildCellFactors */
202 
203 
204 
205 void MorphOpStretcher::UpdateFactors (kkuint32 height,
206  kkuint32 width
207  )
208 {
209  if ((height + 1) > rowFactorsCount)
210  {
211  delete[] rowFactors;
212  rowFactors = NULL;
213  rowFactorsCount = 0;
214  rowFactors = BuildCellFactors (rowFactor, height + 5);
215  rowFactorsCount = height + 5;
216  }
217 
218  if ((width + 1) > colFactorsCount)
219  {
220  delete[] colFactors;
221  colFactors = NULL;
222  colFactors = BuildCellFactors (colFactor, width + 5);
223  colFactorsCount = width + 5;
224  }
225 } /* UpdateFactors */
void SetSrcRaster(RasterConstPtr _srcRaster)
Definition: MorphOp.cpp:149
__int32 kkint32
Definition: KKBaseTypes.h:88
void BackgroundPixelTH(uchar _backgroundPixelTH)
Definition: Raster.h:341
void BackgroundPixelValue(uchar _backgroundPixelValue)
Definition: Raster.h:342
uchar BackgroundPixelValue() const
Definition: Raster.h:336
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
A class that is used by to represent a single image in memory.
Definition: Raster.h:108
bool Color() const
Definition: Raster.h:310
uchar ** Red() const
Definition: Raster.h:326
kkint32 srcHeight
Definition: MorphOp.h:123
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
virtual RasterPtr PerformOperation(RasterConstPtr _image)
uchar ForegroundPixelValue() const
Definition: Raster.h:337
KKTHread * KKTHreadPtr
void UpdateForSourceCellIdx(kkuint32 cellIdx, float factor)
void ForegroundPixelValue(uchar _foregroundPixelValue)
Definition: Raster.h:345
uchar *const * srcRed
Definition: MorphOp.h:118
kkint32 srcWidth
Definition: MorphOp.h:124
MorphOpStretcher(float _rowFactor, float _colFactor)
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
uchar BackgroundPixelTH() const
Definition: Raster.h:335
uchar *const * srcBlue
Definition: MorphOp.h:120
Raster(kkint32 _height, kkint32 _width, bool _color)
Constructs a blank image with given dimensions.
Definition: Raster.cpp:356
Base class for all Morphological operations.
Definition: MorphOp.h:44
uchar ** Blue() const
Definition: Raster.h:328
uchar *const * srcGreen
Definition: MorphOp.h:119
uchar ** Green() const
Definition: Raster.h:327