419 ASSERT_HOST(pix !=
nullptr && pixGetDepth(pix) == 32);
422 int width = pixGetWidth(pix);
423 int height = pixGetHeight(pix);
424 int left_pad = std::max(rect.
left() - 2 * factor, 0) / factor;
425 int top_pad = (rect.
top() + 2 * factor + (factor - 1)) / factor;
426 top_pad = std::min(height, top_pad);
427 int right_pad = (rect.
right() + 2 * factor + (factor - 1)) / factor;
428 right_pad = std::min(width, right_pad);
429 int bottom_pad = std::max(rect.
bottom() - 2 * factor, 0) / factor;
430 int width_pad = right_pad - left_pad;
431 int height_pad = top_pad - bottom_pad;
432 if (width_pad < 1 || height_pad < 1 || width_pad + height_pad < 4)
435 Box* scaled_box = boxCreate(left_pad, height - top_pad,
436 width_pad, height_pad);
437 Pix* scaled = pixClipRectangle(pix, scaled_box,
nullptr);
440 STATS red_stats(0, 256);
441 STATS green_stats(0, 256);
442 STATS blue_stats(0, 256);
443 uint32_t* data = pixGetData(scaled);
445 for (
int y = 0; y < height_pad; ++y) {
446 for (
int x = 0; x < width_pad; ++x, ++data) {
447 int r = GET_DATA_BYTE(data, COLOR_RED);
448 int g = GET_DATA_BYTE(data, COLOR_GREEN);
449 int b = GET_DATA_BYTE(data, COLOR_BLUE);
451 green_stats.add(g, 1);
452 blue_stats.add(b, 1);
459 int best_l8 =
static_cast<int>(red_stats.ile(0.125f));
460 int best_u8 =
static_cast<int>(ceil(red_stats.ile(0.875f)));
461 int best_i8r = best_u8 - best_l8;
462 int x_color = COLOR_RED;
463 int y1_color = COLOR_GREEN;
464 int y2_color = COLOR_BLUE;
465 int l8 =
static_cast<int>(green_stats.ile(0.125f));
466 int u8 =
static_cast<int>(ceil(green_stats.ile(0.875f)));
467 if (u8 - l8 > best_i8r) {
471 x_color = COLOR_GREEN;
472 y1_color = COLOR_RED;
474 l8 =
static_cast<int>(blue_stats.ile(0.125f));
475 u8 =
static_cast<int>(ceil(blue_stats.ile(0.875f)));
476 if (u8 - l8 > best_i8r) {
480 x_color = COLOR_BLUE;
481 y1_color = COLOR_GREEN;
482 y2_color = COLOR_RED;
487 uint32_t* data = pixGetData(scaled);
488 for (
int im_y = 0; im_y < height_pad; ++im_y) {
489 for (
int im_x = 0; im_x < width_pad; ++im_x, ++data) {
490 int x = GET_DATA_BYTE(data, x_color);
491 int y1 = GET_DATA_BYTE(data, y1_color);
492 int y2 = GET_DATA_BYTE(data, y2_color);
497 double m1 = line1.
m();
498 double c1 = line1.
c(m1);
499 double m2 = line2.
m();
500 double c2 = line2.
c(m2);
501 double rms = line1.
rms(m1, c1) + line2.
rms(m2, c2);
505 color1[y1_color] =
ClipToByte(m1 * best_l8 + c1 + 0.5);
506 color1[y2_color] =
ClipToByte(m2 * best_l8 + c2 + 0.5);
509 color2[y1_color] =
ClipToByte(m1 * best_u8 + c1 + 0.5);
510 color2[y2_color] =
ClipToByte(m2 * best_u8 + c2 + 0.5);
514 color1[COLOR_RED] =
ClipToByte(red_stats.median());
515 color1[COLOR_GREEN] =
ClipToByte(green_stats.median());
516 color1[COLOR_BLUE] =
ClipToByte(blue_stats.median());
517 color1[L_ALPHA_CHANNEL] = 0;
518 memcpy(color2, color1, 4);
520 if (color_map1 !=
nullptr) {
521 pixSetInRectArbitrary(color_map1, scaled_box,
524 color1[COLOR_BLUE]));
525 pixSetInRectArbitrary(color_map2, scaled_box,
528 color2[COLOR_BLUE]));
529 pixSetInRectArbitrary(rms_map, scaled_box, color1[L_ALPHA_CHANNEL]);
532 boxDestroy(&scaled_box);
static uint8_t ClipToByte(double pixel)
static uint32_t ComposeRGB(uint32_t r, uint32_t g, uint32_t b)
void add(double x, double y)
const double kRMSFitScaling
double rms(double m, double c) const
const int kMinColorDifference