397 int width = pixGetWidth(pix);
398 int height = pixGetHeight(pix);
399 int left_pad =
MAX(rect.
left() - 2 * factor, 0) / factor;
400 int top_pad = (rect.
top() + 2 * factor + (factor - 1)) / factor;
401 top_pad =
MIN(height, top_pad);
402 int right_pad = (rect.
right() + 2 * factor + (factor - 1)) / factor;
403 right_pad =
MIN(width, right_pad);
404 int bottom_pad =
MAX(rect.
bottom() - 2 * factor, 0) / factor;
405 int width_pad = right_pad - left_pad;
406 int height_pad = top_pad - bottom_pad;
407 if (width_pad < 1 || height_pad < 1 || width_pad + height_pad < 4)
410 Box* scaled_box = boxCreate(left_pad, height - top_pad,
411 width_pad, height_pad);
412 Pix* scaled = pixClipRectangle(pix, scaled_box,
NULL);
415 STATS red_stats(0, 256);
416 STATS green_stats(0, 256);
417 STATS blue_stats(0, 256);
418 uinT32* data = pixGetData(scaled);
420 for (
int y = 0; y < height_pad; ++y) {
421 for (
int x = 0; x < width_pad; ++x, ++data) {
422 int r = GET_DATA_BYTE(data, COLOR_RED);
423 int g = GET_DATA_BYTE(data, COLOR_GREEN);
424 int b = GET_DATA_BYTE(data, COLOR_BLUE);
426 green_stats.add(g, 1);
427 blue_stats.add(b, 1);
434 int best_l8 =
static_cast<int>(red_stats.ile(0.125f));
435 int best_u8 =
static_cast<int>(ceil(red_stats.ile(0.875f)));
436 int best_i8r = best_u8 - best_l8;
437 int x_color = COLOR_RED;
438 int y1_color = COLOR_GREEN;
439 int y2_color = COLOR_BLUE;
440 int l8 =
static_cast<int>(green_stats.ile(0.125f));
441 int u8 =
static_cast<int>(ceil(green_stats.ile(0.875f)));
442 if (u8 - l8 > best_i8r) {
446 x_color = COLOR_GREEN;
447 y1_color = COLOR_RED;
449 l8 =
static_cast<int>(blue_stats.ile(0.125f));
450 u8 =
static_cast<int>(ceil(blue_stats.ile(0.875f)));
451 if (u8 - l8 > best_i8r) {
455 x_color = COLOR_BLUE;
456 y1_color = COLOR_GREEN;
457 y2_color = COLOR_RED;
462 uinT32* data = pixGetData(scaled);
463 for (
int im_y = 0; im_y < height_pad; ++im_y) {
464 for (
int im_x = 0; im_x < width_pad; ++im_x, ++data) {
465 int x = GET_DATA_BYTE(data, x_color);
466 int y1 = GET_DATA_BYTE(data, y1_color);
467 int y2 = GET_DATA_BYTE(data, y2_color);
472 double m1 = line1.
m();
473 double c1 = line1.
c(m1);
474 double m2 = line2.
m();
475 double c2 = line2.
c(m2);
476 double rms = line1.
rms(m1, c1) + line2.
rms(m2, c2);
480 color1[y1_color] =
ClipToByte(m1 * best_l8 + c1 + 0.5);
481 color1[y2_color] =
ClipToByte(m2 * best_l8 + c2 + 0.5);
484 color2[y1_color] =
ClipToByte(m1 * best_u8 + c1 + 0.5);
485 color2[y2_color] =
ClipToByte(m2 * best_u8 + c2 + 0.5);
489 color1[COLOR_RED] =
ClipToByte(red_stats.median());
490 color1[COLOR_GREEN] =
ClipToByte(green_stats.median());
491 color1[COLOR_BLUE] =
ClipToByte(blue_stats.median());
492 color1[L_ALPHA_CHANNEL] = 0;
493 memcpy(color2, color1, 4);
495 if (color_map1 !=
NULL) {
496 pixSetInRectArbitrary(color_map1, scaled_box,
499 color1[COLOR_BLUE]));
500 pixSetInRectArbitrary(color_map2, scaled_box,
503 color2[COLOR_BLUE]));
504 pixSetInRectArbitrary(rms_map, scaled_box, color1[L_ALPHA_CHANNEL]);
507 boxDestroy(&scaled_box);
double rms(double m, double c) const
void add(double x, double y)
const double kRMSFitScaling
const int kMinColorDifference
static uinT32 ComposeRGB(uinT32 r, uinT32 g, uinT32 b)
static uinT8 ClipToByte(double pixel)