24 #include "allheaders.h"
93 Pix* pix = pixConvertTo8(input,
false);
96 int width = pixGetWidth(input);
97 int height = pixGetHeight(input);
104 input = pixErodeGray(pix, 3, 3);
109 pix = pixBlockconv(input, 1, 1);
112 if (rotation !=
nullptr) {
113 float radians_clockwise = 0.0f;
115 radians_clockwise = *rotation;
116 }
else if (randomizer !=
nullptr) {
120 input = pixRotate(pix, radians_clockwise,
121 L_ROTATE_AREA_MAP, L_BRING_IN_WHITE,
124 *rotation = radians_clockwise;
130 if (exposure >= 3 || exposure == 1) {
136 input = pixErodeGray(pix, 3, 3);
142 int erosion_offset = 0;
154 l_uint32* data = pixGetData(input);
155 for (
int y = 0; y < height; ++y) {
156 for (
int x = 0; x < width; ++x) {
157 int pixel = GET_DATA_BYTE(data, x);
158 if (randomizer !=
nullptr)
161 pixel -= (2*x + y) * 32 / (height + width);
162 pixel += erosion_offset;
167 SET_DATA_BYTE(data, x, pixel);
180 bool white_noise,
bool smooth_noise,
bool blur,
181 int box_reduction, TRand* randomizer,
183 Pix* distorted = pixCopy(
nullptr, const_cast<Pix*>(pix));
185 if ((white_noise || smooth_noise) && randomizer->SignedRand(1.0) > 0.0) {
188 srand(randomizer->IntRand());
189 Pix* pixn = pixAddGaussianNoise(distorted, 8.0);
190 pixDestroy(&distorted);
192 distorted = pixBlockconv(pixn, 1, 1);
198 if (blur && randomizer->SignedRand(1.0) > 0.0) {
199 Pix* blurred = pixBlockconv(distorted, 1, 1);
200 pixDestroy(&distorted);
205 if (boxes !=
nullptr) {
206 for (
int b = 0; b < boxes->
size(); ++b) {
207 (*boxes)[b].scale(1.0f / box_reduction);
208 if ((*boxes)[b].width() <= 0)
209 (*boxes)[b].set_right((*boxes)[b].left() + 1);
212 if (invert && randomizer->SignedRand(1.0) < -0)
213 pixInvert(distorted, distorted);
222 if (pix !=
nullptr && *pix !=
nullptr) {
223 width = pixGetWidth(*pix);
224 height = pixGetHeight(*pix);
226 float* im_coeffs =
nullptr;
227 float* box_coeffs =
nullptr;
230 if (pix !=
nullptr && *pix !=
nullptr) {
232 Pix* transformed = pixProjective(*pix, im_coeffs, incolor);
233 if (transformed ==
nullptr) {
234 tprintf(
"Projective transformation failed!!\n");
240 if (boxes !=
nullptr) {
242 for (
int b = 0; b < boxes->
size(); ++b) {
244 const TBOX& box = (*boxes)[b];
245 projectiveXformSampledPt(box_coeffs, box.
left(), height - box.
top(), &x1,
247 projectiveXformSampledPt(box_coeffs, box.
right(), height - box.
bottom(),
249 TBOX new_box1(x1, height - y2, x2, height - y1);
250 projectiveXformSampledPt(box_coeffs, box.
left(), height - box.
bottom(),
252 projectiveXformSampledPt(box_coeffs, box.
right(), height - box.
top(), &x2,
254 TBOX new_box2(x1, height - y1, x2, height - y2);
255 (*boxes)[b] = new_box1.bounding_union(new_box2);
267 float** im_coeffs,
float** box_coeffs) {
269 Pta* src_pts = ptaCreate(4);
270 ptaAddPt(src_pts, 0.0f, 0.0f);
271 ptaAddPt(src_pts, width, 0.0f);
272 ptaAddPt(src_pts, width, height);
273 ptaAddPt(src_pts, 0.0f, height);
281 shear = randomizer->SignedRand(0.5 / 3.0);
282 shear = shear >= 0.0 ? shear * shear : -shear * shear;
284 if (shear < -factors[
FN_X0]) shear = -factors[
FN_X0];
285 if (shear > factors[
FN_X1]) shear = factors[
FN_X1];
288 factors[i] = fabs(randomizer->SignedRand(1.0));
290 factors[i] *= 5.0 / 8.0;
293 factors[i] *= factors[i];
297 Pta* dest_pts = ptaCreate(4);
298 ptaAddPt(dest_pts, factors[
FN_X0] * width, factors[
FN_Y0] * height);
299 ptaAddPt(dest_pts, (1.0f - factors[
FN_X1]) * width, factors[
FN_Y1] * height);
300 ptaAddPt(dest_pts, (1.0f - factors[
FN_X1] + shear) * width,
301 (1 - factors[
FN_Y2]) * height);
302 ptaAddPt(dest_pts, (factors[
FN_X0] + shear) * width,
303 (1 - factors[
FN_Y3]) * height);
304 getProjectiveXformCoeffs(dest_pts, src_pts, im_coeffs);
305 getProjectiveXformCoeffs(src_pts, dest_pts, box_coeffs);
306 ptaDestroy(&src_pts);
307 ptaDestroy(&dest_pts);
308 return factors[
FN_INCOLOR] > 0.5f ? L_BRING_IN_WHITE : L_BRING_IN_BLACK;