404 ASSERT_HOST(pix != NULL && pixGetDepth(pix) == 32);
407 int width = pixGetWidth(pix);
408 int height = pixGetHeight(pix);
409 int left_pad =
MAX(rect.
left() - 2 * factor, 0) / factor;
410 int top_pad = (rect.
top() + 2 * factor + (factor - 1)) / factor;
411 top_pad =
MIN(height, top_pad);
412 int right_pad = (rect.
right() + 2 * factor + (factor - 1)) / factor;
413 right_pad =
MIN(width, right_pad);
414 int bottom_pad =
MAX(rect.
bottom() - 2 * factor, 0) / factor;
415 int width_pad = right_pad - left_pad;
416 int height_pad = top_pad - bottom_pad;
417 if (width_pad < 1 || height_pad < 1 || width_pad + height_pad < 4)
420 Box* scaled_box = boxCreate(left_pad, height - top_pad,
421 width_pad, height_pad);
422 Pix* scaled = pixClipRectangle(pix, scaled_box, NULL);
425 STATS red_stats(0, 256);
426 STATS green_stats(0, 256);
427 STATS blue_stats(0, 256);
428 uinT32* data = pixGetData(scaled);
430 for (
int y = 0; y < height_pad; ++y) {
431 for (
int x = 0; x < width_pad; ++x, ++data) {
432 int r = GET_DATA_BYTE(data, COLOR_RED);
433 int g = GET_DATA_BYTE(data, COLOR_GREEN);
434 int b = GET_DATA_BYTE(data, COLOR_BLUE);
436 green_stats.add(g, 1);
437 blue_stats.add(b, 1);
444 int best_l8 =
static_cast<int>(red_stats.ile(0.125f));
445 int best_u8 =
static_cast<int>(ceil(red_stats.ile(0.875f)));
446 int best_i8r = best_u8 - best_l8;
447 int x_color = COLOR_RED;
448 int y1_color = COLOR_GREEN;
449 int y2_color = COLOR_BLUE;
450 int l8 =
static_cast<int>(green_stats.ile(0.125f));
451 int u8 =
static_cast<int>(ceil(green_stats.ile(0.875f)));
452 if (u8 - l8 > best_i8r) {
456 x_color = COLOR_GREEN;
457 y1_color = COLOR_RED;
459 l8 =
static_cast<int>(blue_stats.ile(0.125f));
460 u8 =
static_cast<int>(ceil(blue_stats.ile(0.875f)));
461 if (u8 - l8 > best_i8r) {
465 x_color = COLOR_BLUE;
466 y1_color = COLOR_GREEN;
467 y2_color = COLOR_RED;
472 uinT32* data = pixGetData(scaled);
473 for (
int im_y = 0; im_y < height_pad; ++im_y) {
474 for (
int im_x = 0; im_x < width_pad; ++im_x, ++data) {
475 int x = GET_DATA_BYTE(data, x_color);
476 int y1 = GET_DATA_BYTE(data, y1_color);
477 int y2 = GET_DATA_BYTE(data, y2_color);
482 double m1 = line1.
m();
483 double c1 = line1.
c(m1);
484 double m2 = line2.
m();
485 double c2 = line2.
c(m2);
486 double rms = line1.
rms(m1, c1) + line2.
rms(m2, c2);
490 color1[y1_color] =
ClipToByte(m1 * best_l8 + c1 + 0.5);
491 color1[y2_color] =
ClipToByte(m2 * best_l8 + c2 + 0.5);
494 color2[y1_color] =
ClipToByte(m1 * best_u8 + c1 + 0.5);
495 color2[y2_color] =
ClipToByte(m2 * best_u8 + c2 + 0.5);
499 color1[COLOR_RED] =
ClipToByte(red_stats.median());
500 color1[COLOR_GREEN] =
ClipToByte(green_stats.median());
501 color1[COLOR_BLUE] =
ClipToByte(blue_stats.median());
502 color1[L_ALPHA_CHANNEL] = 0;
503 memcpy(color2, color1, 4);
505 if (color_map1 != NULL) {
506 pixSetInRectArbitrary(color_map1, scaled_box,
509 color1[COLOR_BLUE]));
510 pixSetInRectArbitrary(color_map2, scaled_box,
513 color2[COLOR_BLUE]));
514 pixSetInRectArbitrary(rms_map, scaled_box, color1[L_ALPHA_CHANNEL]);
517 boxDestroy(&scaled_box);
double rms(double m, double c) const
static uinT8 ClipToByte(double pixel)
static uinT32 ComposeRGB(uinT32 r, uinT32 g, uinT32 b)
void add(double x, double y)
const double kRMSFitScaling
const int kMinColorDifference