/*! * pixBilinearSampled() * * Input: pixs (all depths) * vc (vector of 8 coefficients for bilinear transformation) * incolor (L_BRING_IN_WHITE, L_BRING_IN_BLACK) * Return: pixd, or null on error * * Notes: * (1) Brings in either black or white pixels from the boundary. * (2) Retains colormap, which you can do for a sampled transform.. * (3) For 8 or 32 bpp, much better quality is obtained by the * somewhat slower pixBilinear(). See that function * for relative timings between sampled and interpolated. */ PIX * pixBilinearSampled(PIX *pixs, l_float32 *vc, l_int32 incolor) { l_int32 i, j, w, h, d, x, y, wpls, wpld, color, cmapindex; l_uint32 val; l_uint32 *datas, *datad, *lines, *lined; PIX *pixd; PIXCMAP *cmap; PROCNAME("pixBilinearSampled"); if (!pixs) return (PIX *) ERROR_PTR("pixs not defined", procName, NULL); if (!vc) return (PIX *) ERROR_PTR("vc not defined", procName, NULL); if (incolor != L_BRING_IN_WHITE && incolor != L_BRING_IN_BLACK) return (PIX *) ERROR_PTR("invalid incolor", procName, NULL); pixGetDimensions(pixs, &w, &h, &d); if (d != 1 && d != 2 && d != 4 && d != 8 && d != 32) return (PIX *) ERROR_PTR("depth not 1, 2, 4, 8 or 16", procName, NULL); /* Init all dest pixels to color to be brought in from outside */ pixd = pixCreateTemplate(pixs); if ((cmap = pixGetColormap(pixs)) != NULL) { if (incolor == L_BRING_IN_WHITE) color = 1; else color = 0; pixcmapAddBlackOrWhite(cmap, color, &cmapindex); pixSetAllArbitrary(pixd, cmapindex); } else { if ((d == 1 && incolor == L_BRING_IN_WHITE) || (d > 1 && incolor == L_BRING_IN_BLACK)) { pixClearAll(pixd); } else { pixSetAll(pixd); } } /* Scan over the dest pixels */ datas = pixGetData(pixs); wpls = pixGetWpl(pixs); datad = pixGetData(pixd); wpld = pixGetWpl(pixd); for (i = 0; i < h; i++) { lined = datad + i * wpld; for (j = 0; j < w; j++) { bilinearXformSampledPt(vc, j, i, &x, &y); if (x < 0 || y < 0 || x >= w || y >= h) continue; lines = datas + y * wpls; if (d == 1) { val = GET_DATA_BIT(lines, x); SET_DATA_BIT_VAL(lined, j, val); } else if (d == 8) { val = GET_DATA_BYTE(lines, x); SET_DATA_BYTE(lined, j, val); } else if (d == 32) { lined[j] = lines[x]; } else if (d == 2) { val = GET_DATA_DIBIT(lines, x); SET_DATA_DIBIT(lined, j, val); } else if (d == 4) { val = GET_DATA_QBIT(lines, x); SET_DATA_QBIT(lined, j, val); } } } return pixd; }
/*! * pixSubtractGray() * * Input: pixd (<optional>; this can be null, equal to pixs1, or * different from pixs1) * pixs1 (can be == to pixd) * pixs2 * Return: pixd always * * Notes: * (1) Arithmetic subtraction of two 8, 16 or 32 bpp images. * (2) Source pixs2 is always subtracted from source pixs1. * (3) Do explicit clipping to 0. * (4) Alignment is to UL corner. * (5) There are 3 cases. The result can go to a new dest, * in-place to pixs1, or to an existing input dest: * (a) pixd == null (src1 - src2) --> new pixd * (b) pixd == pixs1 (src1 - src2) --> src1 (in-place) * (d) pixd != pixs1 (src1 - src2) --> input pixd * (6) pixs2 must be different from both pixd and pixs1. */ PIX * pixSubtractGray(PIX *pixd, PIX *pixs1, PIX *pixs2) { l_int32 i, j, w, h, ws, hs, d, wpls, wpld, val, diff; l_uint32 *datas, *datad, *lines, *lined; PROCNAME("pixSubtractGray"); if (!pixs1) return (PIX *)ERROR_PTR("pixs1 not defined", procName, pixd); if (!pixs2) return (PIX *)ERROR_PTR("pixs2 not defined", procName, pixd); if (pixs2 == pixs1) return (PIX *)ERROR_PTR("pixs2 and pixs1 must differ", procName, pixd); if (pixs2 == pixd) return (PIX *)ERROR_PTR("pixs2 and pixd must differ", procName, pixd); d = pixGetDepth(pixs1); if (d != 8 && d != 16 && d != 32) return (PIX *)ERROR_PTR("pix are not 8, 16 or 32 bpp", procName, pixd); if (pixGetDepth(pixs2) != d) return (PIX *)ERROR_PTR("depths differ (pixs1, pixs2)", procName, pixd); if (pixd && (pixGetDepth(pixd) != d)) return (PIX *)ERROR_PTR("depths differ (pixs1, pixd)", procName, pixd); if (!pixSizesEqual(pixs1, pixs2)) L_WARNING("pixs1 and pixs2 not equal in size\n", procName); if (pixd && !pixSizesEqual(pixs1, pixd)) L_WARNING("pixs1 and pixd not equal in size\n", procName); if (pixs1 != pixd) pixd = pixCopy(pixd, pixs1); /* pixd - pixs2 ==> pixd */ datas = pixGetData(pixs2); datad = pixGetData(pixd); wpls = pixGetWpl(pixs2); wpld = pixGetWpl(pixd); pixGetDimensions(pixs2, &ws, &hs, NULL); pixGetDimensions(pixd, &w, &h, NULL); w = L_MIN(ws, w); h = L_MIN(hs, h); for (i = 0; i < h; i++) { lined = datad + i * wpld; lines = datas + i * wpls; if (d == 8) { for (j = 0; j < w; j++) { diff = GET_DATA_BYTE(lined, j) - GET_DATA_BYTE(lines, j); val = L_MAX(diff, 0); SET_DATA_BYTE(lined, j, val); } } else if (d == 16) { for (j = 0; j < w; j++) { diff = GET_DATA_TWO_BYTES(lined, j) - GET_DATA_TWO_BYTES(lines, j); val = L_MAX(diff, 0); SET_DATA_TWO_BYTES(lined, j, val); } } else { /* d == 32; no clipping */ for (j = 0; j < w; j++) *(lined + j) -= *(lines + j); } } return pixd; }
/*! * pixFinalAccumulate() * * Input: pixs (32 bpp) * offset (same as used for initialization) * depth (8, 16 or 32 bpp, of destination) * Return: pixd (8, 16 or 32 bpp), or null on error * * Notes: * (1) The offset must be >= 0 and should not exceed 0x40000000. * (2) The offset is subtracted from the src 32 bpp image * (3) For 8 bpp dest, the result is clipped to [0, 0xff] * (4) For 16 bpp dest, the result is clipped to [0, 0xffff] */ PIX * pixFinalAccumulate(PIX *pixs, l_uint32 offset, l_int32 depth) { l_int32 i, j, w, h, wpls, wpld, val; l_uint32 *datas, *datad, *lines, *lined; PIX *pixd; PROCNAME("pixFinalAccumulate"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 32) return (PIX *)ERROR_PTR("pixs not 32 bpp", procName, NULL); if (depth != 8 && depth != 16 && depth != 32) return (PIX *)ERROR_PTR("dest depth not 8, 16, 32 bpp", procName, NULL); if (offset > 0x40000000) offset = 0x40000000; pixGetDimensions(pixs, &w, &h, NULL); if ((pixd = pixCreate(w, h, depth)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); pixCopyResolution(pixd, pixs); /* but how did pixs get it initially? */ datas = pixGetData(pixs); datad = pixGetData(pixd); wpls = pixGetWpl(pixs); wpld = pixGetWpl(pixd); if (depth == 8) { for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) { val = lines[j] - offset; val = L_MAX(0, val); val = L_MIN(255, val); SET_DATA_BYTE(lined, j, (l_uint8)val); } } } else if (depth == 16) { for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) { val = lines[j] - offset; val = L_MAX(0, val); val = L_MIN(0xffff, val); SET_DATA_TWO_BYTES(lined, j, (l_uint16)val); } } } else { /* depth == 32 */ for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) lined[j] = lines[j] - offset; } } return pixd; }
/*! * \brief pixReadStreamJpeg() * * \param[in] fp file stream * \param[in] cmapflag 0 for no colormap in returned pix; * 1 to return an 8 bpp cmapped pix if spp = 3 or 4 * \param[in] reduction scaling factor: 1, 2, 4 or 8 * \param[out] pnwarn [optional] number of warnings * \param[in] hint a bitwise OR of L_JPEG_* values; 0 for default * \return pix, or NULL on error * * Usage: see pixReadJpeg * <pre> * Notes: * (1) The jpeg comment, if it exists, is not stored in the pix. * </pre> */ PIX * pixReadStreamJpeg(FILE *fp, l_int32 cmapflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint) { l_int32 cyan, yellow, magenta, black, nwarn; l_int32 i, j, k, rval, gval, bval; l_int32 w, h, wpl, spp, ncolors, cindex, ycck, cmyk; l_uint32 *data; l_uint32 *line, *ppixel; JSAMPROW rowbuffer; PIX *pix; PIXCMAP *cmap; struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; jmp_buf jmpbuf; /* must be local to the function */ PROCNAME("pixReadStreamJpeg"); if (pnwarn) *pnwarn = 0; if (!fp) return (PIX *)ERROR_PTR("fp not defined", procName, NULL); if (cmapflag != 0 && cmapflag != 1) cmapflag = 0; /* default */ if (reduction != 1 && reduction != 2 && reduction != 4 && reduction != 8) return (PIX *)ERROR_PTR("reduction not in {1,2,4,8}", procName, NULL); if (BITS_IN_JSAMPLE != 8) /* set in jmorecfg.h */ return (PIX *)ERROR_PTR("BITS_IN_JSAMPLE != 8", procName, NULL); rewind(fp); pix = NULL; rowbuffer = NULL; /* Modify the jpeg error handling to catch fatal errors */ cinfo.err = jpeg_std_error(&jerr); jerr.error_exit = jpeg_error_catch_all_1; cinfo.client_data = (void *)&jmpbuf; if (setjmp(jmpbuf)) { pixDestroy(&pix); LEPT_FREE(rowbuffer); return (PIX *)ERROR_PTR("internal jpeg error", procName, NULL); } /* Initialize jpeg structs for decompression */ jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, fp); jpeg_read_header(&cinfo, TRUE); cinfo.scale_denom = reduction; cinfo.scale_num = 1; jpeg_calc_output_dimensions(&cinfo); if (hint & L_JPEG_READ_LUMINANCE) { cinfo.out_color_space = JCS_GRAYSCALE; spp = 1; L_INFO("reading luminance channel only\n", procName); } else { spp = cinfo.out_color_components; } /* Allocate the image and a row buffer */ w = cinfo.output_width; h = cinfo.output_height; ycck = (cinfo.jpeg_color_space == JCS_YCCK && spp == 4 && cmapflag == 0); cmyk = (cinfo.jpeg_color_space == JCS_CMYK && spp == 4 && cmapflag == 0); if (spp != 1 && spp != 3 && !ycck && !cmyk) { return (PIX *)ERROR_PTR("spp must be 1 or 3, or YCCK or CMYK", procName, NULL); } if ((spp == 3 && cmapflag == 0) || ycck || cmyk) { /* rgb or 4 bpp color */ rowbuffer = (JSAMPROW)LEPT_CALLOC(sizeof(JSAMPLE), spp * w); pix = pixCreate(w, h, 32); } else { /* 8 bpp gray or colormapped */ rowbuffer = (JSAMPROW)LEPT_CALLOC(sizeof(JSAMPLE), w); pix = pixCreate(w, h, 8); } pixSetInputFormat(pix, IFF_JFIF_JPEG); if (!rowbuffer || !pix) { LEPT_FREE(rowbuffer); pixDestroy(&pix); return (PIX *)ERROR_PTR("rowbuffer or pix not made", procName, NULL); } /* Initialize decompression. Set up a colormap for color * quantization if requested. */ if (spp == 1) { /* Grayscale or colormapped */ jpeg_start_decompress(&cinfo); } else { /* Color; spp == 3 or YCCK or CMYK */ if (cmapflag == 0) { /* 24 bit color in 32 bit pix or YCCK/CMYK */ cinfo.quantize_colors = FALSE; jpeg_start_decompress(&cinfo); } else { /* Color quantize to 8 bits */ cinfo.quantize_colors = TRUE; cinfo.desired_number_of_colors = 256; jpeg_start_decompress(&cinfo); /* Construct a pix cmap */ cmap = pixcmapCreate(8); ncolors = cinfo.actual_number_of_colors; for (cindex = 0; cindex < ncolors; cindex++) { rval = cinfo.colormap[0][cindex]; gval = cinfo.colormap[1][cindex]; bval = cinfo.colormap[2][cindex]; pixcmapAddColor(cmap, rval, gval, bval); } pixSetColormap(pix, cmap); } } wpl = pixGetWpl(pix); data = pixGetData(pix); /* Decompress. Unfortunately, we cannot use the return value * from jpeg_read_scanlines() to determine if there was a problem * with the data; it always appears to return 1. We can only * tell from the warnings during decoding, such as "premature * end of data segment". The default behavior is to return an * image even if there are warnings. However, by setting the * hint to have the same bit flag as L_JPEG_FAIL_ON_BAD_DATA, * no image will be returned if there are any warnings. */ for (i = 0; i < h; i++) { if (jpeg_read_scanlines(&cinfo, &rowbuffer, (JDIMENSION)1) == 0) { L_ERROR("read error at scanline %d\n", procName, i); pixDestroy(&pix); jpeg_destroy_decompress(&cinfo); LEPT_FREE(rowbuffer); return (PIX *)ERROR_PTR("bad data", procName, NULL); } /* -- 24 bit color -- */ if ((spp == 3 && cmapflag == 0) || ycck || cmyk) { ppixel = data + i * wpl; if (spp == 3) { for (j = k = 0; j < w; j++) { SET_DATA_BYTE(ppixel, COLOR_RED, rowbuffer[k++]); SET_DATA_BYTE(ppixel, COLOR_GREEN, rowbuffer[k++]); SET_DATA_BYTE(ppixel, COLOR_BLUE, rowbuffer[k++]); ppixel++; } } else { /* This is a conversion from CMYK -> RGB that ignores color profiles, and is invoked when the image header claims to be in CMYK or YCCK colorspace. If in YCCK, libjpeg may be doing YCCK -> CMYK under the hood. To understand why the colors need to be inverted on read-in for the Adobe marker, see the "Special color spaces" section of "Using the IJG JPEG Library" by Thomas G. Lane: http://www.jpegcameras.com/libjpeg/libjpeg-3.html#ss3.1 The non-Adobe conversion is equivalent to: rval = black - black * cyan / 255 ... The Adobe conversion is equivalent to: rval = black - black * (255 - cyan) / 255 ... Note that cyan is the complement to red, and we are subtracting the complement color (weighted by black) from black. For Adobe conversions, where they've already inverted the CMY but not the K, we have to invert again. The results must be clipped to [0 ... 255]. */ for (j = k = 0; j < w; j++) { cyan = rowbuffer[k++]; magenta = rowbuffer[k++]; yellow = rowbuffer[k++]; black = rowbuffer[k++]; if (cinfo.saw_Adobe_marker) { rval = (black * cyan) / 255; gval = (black * magenta) / 255; bval = (black * yellow) / 255; } else { rval = black * (255 - cyan) / 255; gval = black * (255 - magenta) / 255; bval = black * (255 - yellow) / 255; } rval = L_MIN(L_MAX(rval, 0), 255); gval = L_MIN(L_MAX(gval, 0), 255); bval = L_MIN(L_MAX(bval, 0), 255); composeRGBPixel(rval, gval, bval, ppixel); ppixel++; } } } else { /* 8 bpp grayscale or colormapped pix */ line = data + i * wpl; for (j = 0; j < w; j++) SET_DATA_BYTE(line, j, rowbuffer[j]); } } nwarn = cinfo.err->num_warnings; if (pnwarn) *pnwarn = nwarn; /* If the pixel density is neither 1 nor 2, it may not be defined. * In that case, don't set the resolution. */ if (cinfo.density_unit == 1) { /* pixels per inch */ pixSetXRes(pix, cinfo.X_density); pixSetYRes(pix, cinfo.Y_density); } else if (cinfo.density_unit == 2) { /* pixels per centimeter */ pixSetXRes(pix, (l_int32)((l_float32)cinfo.X_density * 2.54 + 0.5)); pixSetYRes(pix, (l_int32)((l_float32)cinfo.Y_density * 2.54 + 0.5)); } if (cinfo.output_components != spp) fprintf(stderr, "output spp = %d, spp = %d\n", cinfo.output_components, spp); jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); LEPT_FREE(rowbuffer); if (nwarn > 0) { if (hint & L_JPEG_FAIL_ON_BAD_DATA) { L_ERROR("fail with %d warning(s) of bad data\n", procName, nwarn); pixDestroy(&pix); } else { L_WARNING("%d warning(s) of bad data\n", procName, nwarn); } } return pix; }
/*! * pixBilateralGrayExact() * * Input: pixs (8 bpp gray) * spatial_kel (gaussian kernel) * range_kel (<optional> 256 x 1, monotonically decreasing) * Return: pixd (8 bpp bilateral filtered image) * * Notes: * (1) See pixBilateralExact(). */ PIX * pixBilateralGrayExact(PIX *pixs, L_KERNEL *spatial_kel, L_KERNEL *range_kel) { l_int32 i, j, id, jd, k, m, w, h, d, sx, sy, cx, cy, wplt, wpld; l_int32 val, center_val; l_uint32 *datat, *datad, *linet, *lined; l_float32 sum, weight_sum, weight; L_KERNEL *keli; PIX *pixt, *pixd; PROCNAME("pixBilateralGrayExact"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 8) return (PIX *)ERROR_PTR("pixs must be gray", procName, NULL); pixGetDimensions(pixs, &w, &h, &d); if (!spatial_kel) return (PIX *)ERROR_PTR("spatial kel not defined", procName, NULL); if (!range_kel) return pixConvolve(pixs, spatial_kel, 8, 1); if (range_kel->sx != 256 || range_kel->sy != 1) return (PIX *)ERROR_PTR("range kel not {256 x 1", procName, NULL); keli = kernelInvert(spatial_kel); kernelGetParameters(keli, &sy, &sx, &cy, &cx); if ((pixt = pixAddMirroredBorder(pixs, cx, sx - cx, cy, sy - cy)) == NULL) return (PIX *)ERROR_PTR("pixt not made", procName, NULL); pixd = pixCreate(w, h, 8); datat = pixGetData(pixt); datad = pixGetData(pixd); wplt = pixGetWpl(pixt); wpld = pixGetWpl(pixd); for (i = 0, id = 0; id < h; i++, id++) { lined = datad + id * wpld; for (j = 0, jd = 0; jd < w; j++, jd++) { center_val = GET_DATA_BYTE(datat + (i + cy) * wplt, j + cx); weight_sum = 0.0; sum = 0.0; for (k = 0; k < sy; k++) { linet = datat + (i + k) * wplt; for (m = 0; m < sx; m++) { val = GET_DATA_BYTE(linet, j + m); weight = keli->data[k][m] * range_kel->data[0][L_ABS(center_val - val)]; weight_sum += weight; sum += val * weight; } } SET_DATA_BYTE(lined, jd, (l_int32)(sum / weight_sum + 0.5)); } } kernelDestroy(&keli); pixDestroy(&pixt); return pixd; }
/*! * \brief gifToPix() * * \param[in] gif opened gif stream * \return pix, or NULL on error * * <pre> * Notes: * (1) This decodes the pix from the compressed gif stream and * closes the stream. * (2) It is static so that the stream is not exposed to clients. * </pre> */ static PIX * gifToPix(GifFileType *gif) { l_int32 wpl, i, j, w, h, d, cindex, ncolors; l_int32 rval, gval, bval; l_uint32 *data, *line; PIX *pixd, *pixdi; PIXCMAP *cmap; ColorMapObject *gif_cmap; SavedImage si; #if (GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1) || GIFLIB_MAJOR > 5 int giferr; #endif /* 5.1 and beyond */ PROCNAME("gifToPix"); /* Read all the data, but use only the first image found */ if (DGifSlurp(gif) != GIF_OK) { DGifCloseFile(gif, &giferr); return (PIX *)ERROR_PTR("failed to read GIF data", procName, NULL); } if (gif->SavedImages == NULL) { DGifCloseFile(gif, &giferr); return (PIX *)ERROR_PTR("no images found in GIF", procName, NULL); } si = gif->SavedImages[0]; w = si.ImageDesc.Width; h = si.ImageDesc.Height; if (w <= 0 || h <= 0) { DGifCloseFile(gif, &giferr); return (PIX *)ERROR_PTR("invalid image dimensions", procName, NULL); } if (si.RasterBits == NULL) { DGifCloseFile(gif, &giferr); return (PIX *)ERROR_PTR("no raster data in GIF", procName, NULL); } if (si.ImageDesc.ColorMap) { /* private cmap for this image */ gif_cmap = si.ImageDesc.ColorMap; } else if (gif->SColorMap) { /* global cmap for whole picture */ gif_cmap = gif->SColorMap; } else { /* don't know where to take cmap from */ DGifCloseFile(gif, &giferr); return (PIX *)ERROR_PTR("color map is missing", procName, NULL); } ncolors = gif_cmap->ColorCount; if (ncolors <= 2) d = 1; else if (ncolors <= 4) d = 2; else if (ncolors <= 16) d = 4; else d = 8; if ((cmap = pixcmapCreate(d)) == NULL) { DGifCloseFile(gif, &giferr); return (PIX *)ERROR_PTR("cmap creation failed", procName, NULL); } for (cindex = 0; cindex < ncolors; cindex++) { rval = gif_cmap->Colors[cindex].Red; gval = gif_cmap->Colors[cindex].Green; bval = gif_cmap->Colors[cindex].Blue; pixcmapAddColor(cmap, rval, gval, bval); } if ((pixd = pixCreate(w, h, d)) == NULL) { DGifCloseFile(gif, &giferr); pixcmapDestroy(&cmap); return (PIX *)ERROR_PTR("failed to allocate pixd", procName, NULL); } pixSetInputFormat(pixd, IFF_GIF); pixSetColormap(pixd, cmap); wpl = pixGetWpl(pixd); data = pixGetData(pixd); for (i = 0; i < h; i++) { line = data + i * wpl; if (d == 1) { for (j = 0; j < w; j++) { if (si.RasterBits[i * w + j]) SET_DATA_BIT(line, j); } } else if (d == 2) { for (j = 0; j < w; j++) SET_DATA_DIBIT(line, j, si.RasterBits[i * w + j]); } else if (d == 4) { for (j = 0; j < w; j++) SET_DATA_QBIT(line, j, si.RasterBits[i * w + j]); } else { /* d == 8 */ for (j = 0; j < w; j++) SET_DATA_BYTE(line, j, si.RasterBits[i * w + j]); } } /* If the image has been interlaced (for viewing in a browser), * this restores the raster lines to normal order. */ if (gif->Image.Interlace) { pixdi = pixUninterlaceGIF(pixd); pixTransferAllData(pixd, &pixdi, 0, 0); } DGifCloseFile(gif, &giferr); return pixd; }
/*! * \brief pixColorSegmentTryCluster() * * \param[in] pixd * \param[in] pixs * \param[in] maxdist * \param[in] maxcolors * \param[in] debugflag 1 for debug output; 0 otherwise * \return 0 if OK, 1 on error * * <pre> * Notes: * This function should only be called from pixColorSegCluster() * </pre> */ static l_int32 pixColorSegmentTryCluster(PIX *pixd, PIX *pixs, l_int32 maxdist, l_int32 maxcolors, l_int32 debugflag) { l_int32 rmap[256], gmap[256], bmap[256]; l_int32 w, h, wpls, wpld, i, j, k, found, ret, index, ncolors; l_int32 rval, gval, bval, dist2, maxdist2; l_int32 countarray[256]; l_int32 rsum[256], gsum[256], bsum[256]; l_uint32 *ppixel; l_uint32 *datas, *datad, *lines, *lined; PIXCMAP *cmap; PROCNAME("pixColorSegmentTryCluster"); if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (!pixd) return ERROR_INT("pixd not defined", procName, 1); w = pixGetWidth(pixs); h = pixGetHeight(pixs); maxdist2 = maxdist * maxdist; cmap = pixGetColormap(pixd); pixcmapClear(cmap); for (k = 0; k < 256; k++) { rsum[k] = gsum[k] = bsum[k] = 0; rmap[k] = gmap[k] = bmap[k] = 0; } datas = pixGetData(pixs); datad = pixGetData(pixd); wpls = pixGetWpl(pixs); wpld = pixGetWpl(pixd); ncolors = 0; for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) { ppixel = lines + j; rval = GET_DATA_BYTE(ppixel, COLOR_RED); gval = GET_DATA_BYTE(ppixel, COLOR_GREEN); bval = GET_DATA_BYTE(ppixel, COLOR_BLUE); ncolors = pixcmapGetCount(cmap); found = FALSE; for (k = 0; k < ncolors; k++) { dist2 = (rval - rmap[k]) * (rval - rmap[k]) + (gval - gmap[k]) * (gval - gmap[k]) + (bval - bmap[k]) * (bval - bmap[k]); if (dist2 <= maxdist2) { /* take it; greedy */ found = TRUE; SET_DATA_BYTE(lined, j, k); countarray[k]++; rsum[k] += rval; gsum[k] += gval; bsum[k] += bval; break; } } if (!found) { /* Add a new color */ ret = pixcmapAddNewColor(cmap, rval, gval, bval, &index); /* fprintf(stderr, "index = %d, (i,j) = (%d,%d), rgb = (%d, %d, %d)\n", index, i, j, rval, gval, bval); */ if (ret == 0 && index < maxcolors) { countarray[index] = 1; SET_DATA_BYTE(lined, j, index); rmap[index] = rval; gmap[index] = gval; bmap[index] = bval; rsum[index] = rval; gsum[index] = gval; bsum[index] = bval; } else { if (debugflag) { L_INFO("maxcolors exceeded for maxdist = %d\n", procName, maxdist); } return 1; } } } } /* Replace the colors in the colormap by the averages */ for (k = 0; k < ncolors; k++) { rval = rsum[k] / countarray[k]; gval = gsum[k] / countarray[k]; bval = bsum[k] / countarray[k]; pixcmapResetColor(cmap, k, rval, gval, bval); } return 0; }
/*! * pixApplyVerticalDisparity() * * Input: pixs (1, 8 or 32 bpp) * fpix (vertical disparity array) * Return: pixd (modified by fpix), or null on error * * Notes: * (1) This applies the vertical disparity array to the specified * image. For src pixels above the image, we use the pixels * in the first raster line. */ PIX * pixApplyVerticalDisparity(PIX *pixs, FPIX *fpix) { l_int32 i, j, w, h, d, fw, fh, wpld, wplf, isrc, val8; l_uint32 *datad, *lined; l_float32 *dataf, *linef; void **lineptrs; PIX *pixd; PROCNAME("pixApplyVerticalDisparity"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (!fpix) return (PIX *)ERROR_PTR("fpix not defined", procName, NULL); pixGetDimensions(pixs, &w, &h, &d); if (d != 1 && d != 8 && d != 32) return (PIX *)ERROR_PTR("pix not 1, 8 or 32 bpp", procName, NULL); fpixGetDimensions(fpix, &fw, &fh); if (fw < w || fh < h) { fprintf(stderr, "fw = %d, w = %d, fh = %d, h = %d\n", fw, w, fh, h); return (PIX *)ERROR_PTR("invalid fpix size", procName, NULL); } pixd = pixCreateTemplate(pixs); datad = pixGetData(pixd); dataf = fpixGetData(fpix); wpld = pixGetWpl(pixd); wplf = fpixGetWpl(fpix); if (d == 1) { lineptrs = pixGetLinePtrs(pixs, NULL); for (i = 0; i < h; i++) { lined = datad + i * wpld; linef = dataf + i * wplf; for (j = 0; j < w; j++) { isrc = (l_int32)(i - linef[j] + 0.5); if (isrc < 0) isrc = 0; if (isrc > h - 1) isrc = h - 1; if (GET_DATA_BIT(lineptrs[isrc], j)) SET_DATA_BIT(lined, j); } } } else if (d == 8) { lineptrs = pixGetLinePtrs(pixs, NULL); for (i = 0; i < h; i++) { lined = datad + i * wpld; linef = dataf + i * wplf; for (j = 0; j < w; j++) { isrc = (l_int32)(i - linef[j] + 0.5); if (isrc < 0) isrc = 0; if (isrc > h - 1) isrc = h - 1; val8 = GET_DATA_BYTE(lineptrs[isrc], j); SET_DATA_BYTE(lined, j, val8); } } } else { /* d == 32 */ lineptrs = pixGetLinePtrs(pixs, NULL); for (i = 0; i < h; i++) { lined = datad + i * wpld; linef = dataf + i * wplf; for (j = 0; j < w; j++) { isrc = (l_int32)(i - linef[j] + 0.5); if (isrc < 0) isrc = 0; if (isrc > h - 1) isrc = h - 1; lined[j] = GET_DATA_FOUR_BYTES(lineptrs[isrc], j); } } } FREE(lineptrs); return pixd; }
/*! * pixApplyHorizontalDisparity() * * Input: pixs (1, 8 or 32 bpp) * fpix (horizontal disparity array) * extraw (extra width added to pixd) * Return: pixd (modified by fpix), or null on error * * Notes: * (1) This applies the horizontal disparity array to the specified * image. */ PIX * pixApplyHorizontalDisparity(PIX *pixs, FPIX *fpix, l_int32 extraw) { l_int32 i, j, w, h, d, wd, fw, fh, wpls, wpld, wplf, jsrc, val8; l_uint32 *datas, *lines, *datad, *lined; l_float32 *dataf, *linef; PIX *pixd; PROCNAME("pixApplyHorizontalDisparity"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (!fpix) return (PIX *)ERROR_PTR("fpix not defined", procName, NULL); pixGetDimensions(pixs, &w, &h, &d); if (d != 1 && d != 8 && d != 32) return (PIX *)ERROR_PTR("pix not 1, 8 or 32 bpp", procName, NULL); fpixGetDimensions(fpix, &fw, &fh); if (fw < w + extraw || fh < h) { fprintf(stderr, "fw = %d, w = %d, fh = %d, h = %d\n", fw, w, fh, h); return (PIX *)ERROR_PTR("invalid fpix size", procName, NULL); } wd = w + extraw; pixd = pixCreate(wd, h, d); datas = pixGetData(pixs); datad = pixGetData(pixd); dataf = fpixGetData(fpix); wpls = pixGetWpl(pixs); wpld = pixGetWpl(pixd); wplf = fpixGetWpl(fpix); if (d == 1) { for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; linef = dataf + i * wplf; for (j = 0; j < wd; j++) { jsrc = (l_int32)(j - linef[j] + 0.5); if (jsrc < 0) jsrc = 0; if (jsrc > w - 1) jsrc = w - 1; if (GET_DATA_BIT(lines, jsrc)) SET_DATA_BIT(lined, j); } } } else if (d == 8) { for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; linef = dataf + i * wplf; for (j = 0; j < wd; j++) { jsrc = (l_int32)(j - linef[j] + 0.5); if (jsrc < 0) jsrc = 0; if (jsrc > w - 1) jsrc = w - 1; val8 = GET_DATA_BYTE(lines, jsrc); SET_DATA_BYTE(lined, j, val8); } } } else { /* d == 32 */ for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; linef = dataf + i * wplf; for (j = 0; j < wd; j++) { jsrc = (l_int32)(j - linef[j] + 0.5); if (jsrc < 0) jsrc = 0; if (jsrc > w - 1) jsrc = w - 1; lined[j] = lines[jsrc]; } } } return pixd; }
/*! * pixAssignToNearestColor() * * Input: pixd (8 bpp, colormapped) * pixs (32 bpp; 24-bit color) * pixm (<optional> 1 bpp) * level (of octcube used for finding nearest color in cmap) * countarray (<optional> ptr to array, in which we can store * the number of pixels found in each color in * the colormap in pixd) * Return: 0 if OK, 1 on error * * Notes: * (1) This is used in phase 2 of color segmentation, where pixs * is the original input image to pixColorSegment(), and * pixd is the colormapped image returned from * pixColorSegmentCluster(). It is also used, with a mask, * in phase 4. * (2) This is an in-place operation. * (3) The colormap in pixd is unchanged. * (4) pixs and pixd must be the same size (w, h). * (5) The selection mask pixm can be null. If it exists, it must * be the same size as pixs and pixd, and only pixels * corresponding to fg in pixm are assigned. Set to * NULL if all pixels in pixd are to be assigned. * (6) The countarray can be null. If it exists, it is pre-allocated * and of a size at least equal to the size of the colormap in pixd. * (7) This does a best-fit (non-greedy) assignment of pixels to * existing clusters. Specifically, it assigns each pixel * in pixd to the color index in the pixd colormap that has a * color closest to the corresponding rgb pixel in pixs. * (8) 'level' is the octcube level used to quickly find the nearest * color in the colormap for each pixel. For color segmentation, * this parameter is set to LEVEL_IN_OCTCUBE. * (9) We build a mapping table from octcube to colormap index so * that this function can run in a time (otherwise) independent * of the number of colors in the colormap. This avoids a * brute-force search for the closest colormap color to each * pixel in the image. */ l_int32 pixAssignToNearestColor(PIX *pixd, PIX *pixs, PIX *pixm, l_int32 level, l_int32 *countarray) { l_int32 w, h, wpls, wpld, wplm, i, j; l_int32 rval, gval, bval, index; l_int32 *cmaptab; l_uint32 octindex; l_uint32 *rtab, *gtab, *btab; l_uint32 *ppixel; l_uint32 *datas, *datad, *datam, *lines, *lined, *linem; PIXCMAP *cmap; PROCNAME("pixAssignToNearestColor"); if (!pixd) return ERROR_INT("pixd not defined", procName, 1); if ((cmap = pixGetColormap(pixd)) == NULL) return ERROR_INT("cmap not found", procName, 1); if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (pixGetDepth(pixs) != 32) return ERROR_INT("pixs not 32 bpp", procName, 1); /* Set up the tables to map rgb to the nearest colormap index */ if (makeRGBToIndexTables(&rtab, >ab, &btab, level)) return ERROR_INT("index tables not made", procName, 1); if ((cmaptab = pixcmapToOctcubeLUT(cmap, level, L_MANHATTAN_DISTANCE)) == NULL) return ERROR_INT("cmaptab not made", procName, 1); w = pixGetWidth(pixs); h = pixGetHeight(pixs); datas = pixGetData(pixs); datad = pixGetData(pixd); wpls = pixGetWpl(pixs); wpld = pixGetWpl(pixd); if (pixm) { datam = pixGetData(pixm); wplm = pixGetWpl(pixm); } for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; if (pixm) linem = datam + i * wplm; for (j = 0; j < w; j++) { if (pixm) { if (!GET_DATA_BIT(linem, j)) continue; } ppixel = lines + j; rval = GET_DATA_BYTE(ppixel, COLOR_RED); gval = GET_DATA_BYTE(ppixel, COLOR_GREEN); bval = GET_DATA_BYTE(ppixel, COLOR_BLUE); /* Map from rgb to octcube index */ getOctcubeIndexFromRGB(rval, gval, bval, rtab, gtab, btab, &octindex); /* Map from octcube index to nearest colormap index */ index = cmaptab[octindex]; if (countarray) countarray[index]++; SET_DATA_BYTE(lined, j, index); } } FREE(cmaptab); FREE(rtab); FREE(gtab); FREE(btab); return 0; }
/*! * pixApplyHorizDisparity() * * Input: dew * pixs (1, 8 or 32 bpp) * grayin (gray value, from 0 to 255, for pixels brought in; * use -1 to use pixels on the boundary of pixs) * Return: pixd (modified to remove horizontal disparity if possible), * or null on error. * * Notes: * (1) This applies the horizontal disparity array to the specified * image. * (2) Specify gray color for pixels brought in from the outside: * 0 is black, 255 is white. Use -1 to select pixels from the * boundary of the source image. * (3) The input pixs has already been corrected for vertical disparity. * If the horizontal disparity array doesn't exist, this returns * a clone of @pixs. */ static PIX * pixApplyHorizDisparity(L_DEWARP *dew, PIX *pixs, l_int32 grayin) { l_int32 i, j, w, h, d, fw, fh, wpls, wpld, wplf, jsrc, val8; l_uint32 *datas, *lines, *datad, *lined; l_float32 *dataf, *linef; FPIX *fpix; PIX *pixd; PROCNAME("pixApplyHorizDisparity"); if (!dew) return (PIX *)ERROR_PTR("dew not defined", procName, pixs); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); pixGetDimensions(pixs, &w, &h, &d); if (d != 1 && d != 8 && d != 32) return (PIX *)ERROR_PTR("pix not 1, 8 or 32 bpp", procName, NULL); if ((fpix = dew->fullhdispar) == NULL) return (PIX *)ERROR_PTR("fullhdispar not defined", procName, NULL); fpixGetDimensions(fpix, &fw, &fh); if (fw < w || fh < h) { fprintf(stderr, "fw = %d, w = %d, fh = %d, h = %d\n", fw, w, fh, h); return (PIX *)ERROR_PTR("invalid fpix size", procName, NULL); } /* Two choices for requested pixels outside pixs: (1) use pixels' * from the boundary of pixs; use white or light gray pixels. */ pixd = pixCreateTemplate(pixs); if (grayin >= 0) pixSetAllGray(pixd, grayin); datas = pixGetData(pixs); datad = pixGetData(pixd); dataf = fpixGetData(fpix); wpls = pixGetWpl(pixs); wpld = pixGetWpl(pixd); wplf = fpixGetWpl(fpix); if (d == 1) { for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; linef = dataf + i * wplf; for (j = 0; j < w; j++) { jsrc = (l_int32)(j - linef[j] + 0.5); if (grayin < 0) /* use value at boundary if outside */ jsrc = L_MIN(L_MAX(jsrc, 0), w - 1); if (jsrc >= 0 && jsrc < w) { /* remains gray if outside */ if (GET_DATA_BIT(lines, jsrc)) SET_DATA_BIT(lined, j); } } } } else if (d == 8) { for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; linef = dataf + i * wplf; for (j = 0; j < w; j++) { jsrc = (l_int32)(j - linef[j] + 0.5); if (grayin < 0) jsrc = L_MIN(L_MAX(jsrc, 0), w - 1); if (jsrc >= 0 && jsrc < w) { val8 = GET_DATA_BYTE(lines, jsrc); SET_DATA_BYTE(lined, j, val8); } } } } else { /* d == 32 */ for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; linef = dataf + i * wplf; for (j = 0; j < w; j++) { jsrc = (l_int32)(j - linef[j] + 0.5); if (grayin < 0) jsrc = L_MIN(L_MAX(jsrc, 0), w - 1); if (jsrc >= 0 && jsrc < w) lined[j] = lines[jsrc]; } } } return pixd; }
main(int argc, char **argv) { l_int32 x, y, i, j, k, w, h, w2, w4, w8, w16, w32, wpl, nerrors; l_int32 count1, count2, count3, ret, val1, val2; l_uint32 val32; l_uint32 *data, *line, *line1, *line2, *data1, *data2; void **lines1, **linet1, **linet2; PIX *pixs, *pixt1, *pixt2; static char mainName[] = "lowaccess_reg"; pixs = pixRead("feyn.tif"); /* width divisible by 16 */ pixGetDimensions(pixs, &w, &h, NULL); data = pixGetData(pixs); wpl = pixGetWpl(pixs); lines1 = pixGetLinePtrs(pixs, NULL); /* Get timing for the 3 different methods */ startTimer(); for (k = 0; k < 10; k++) { count1 = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (GET_DATA_BIT(lines1[i], j)) count1++; } } } fprintf(stderr, "Time with line ptrs = %5.3f sec, count1 = %d\n", stopTimer(), count1); startTimer(); for (k = 0; k < 10; k++) { count2 = 0; for (i = 0; i < h; i++) { line = data + i * wpl; for (j = 0; j < w; j++) { if (l_getDataBit(line, j)) count2++; } } } fprintf(stderr, "Time with l_get* = %5.3f sec, count2 = %d\n", stopTimer(), count2); startTimer(); for (k = 0; k < 10; k++) { count3 = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { pixGetPixel(pixs, j, i, &val32); count3 += val32; } } } fprintf(stderr, "Time with pixGetPixel() = %5.3f sec, count3 = %d\n", stopTimer(), count3); pixt1 = pixCreateTemplate(pixs); data1 = pixGetData(pixt1); linet1 = pixGetLinePtrs(pixt1, NULL); pixt2 = pixCreateTemplate(pixs); data2 = pixGetData(pixt2); linet2 = pixGetLinePtrs(pixt2, NULL); nerrors = 0; /* Test different methods for 1 bpp */ count1 = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { val1 = GET_DATA_BIT(lines1[i], j); count1 += val1; if (val1) SET_DATA_BIT(linet1[i], j); } } count2 = 0; for (i = 0; i < h; i++) { line = data + i * wpl; line2 = data2 + i * wpl; for (j = 0; j < w; j++) { val2 = l_getDataBit(line, j); count2 += val2; if (val2) l_setDataBit(line2, j); } } ret = compareResults(pixs, pixt1, pixt2, count1, count2, "1 bpp"); nerrors += ret; /* Test different methods for 2 bpp */ count1 = 0; w2 = w / 2; for (i = 0; i < h; i++) { for (j = 0; j < w2; j++) { val1 = GET_DATA_DIBIT(lines1[i], j); count1 += val1; val1 += 0xbbbbbbbc; SET_DATA_DIBIT(linet1[i], j, val1); } } count2 = 0; for (i = 0; i < h; i++) { line = data + i * wpl; line2 = data2 + i * wpl; for (j = 0; j < w2; j++) { val2 = l_getDataDibit(line, j); count2 += val2; val2 += 0xbbbbbbbc; l_setDataDibit(line2, j, val2); } } ret = compareResults(pixs, pixt1, pixt2, count1, count2, "2 bpp"); nerrors += ret; /* Test different methods for 4 bpp */ count1 = 0; w4 = w / 4; for (i = 0; i < h; i++) { for (j = 0; j < w4; j++) { val1 = GET_DATA_QBIT(lines1[i], j); count1 += val1; val1 += 0xbbbbbbb0; SET_DATA_QBIT(linet1[i], j, val1); } } count2 = 0; for (i = 0; i < h; i++) { line = data + i * wpl; line2 = data2 + i * wpl; for (j = 0; j < w4; j++) { val2 = l_getDataQbit(line, j); count2 += val2; val2 += 0xbbbbbbb0; l_setDataQbit(line2, j, val2); } } ret = compareResults(pixs, pixt1, pixt2, count1, count2, "4 bpp"); nerrors += ret; /* Test different methods for 8 bpp */ count1 = 0; w8 = w / 8; for (i = 0; i < h; i++) { for (j = 0; j < w8; j++) { val1 = GET_DATA_BYTE(lines1[i], j); count1 += val1; val1 += 0xbbbbbb00; SET_DATA_BYTE(linet1[i], j, val1); } } count2 = 0; for (i = 0; i < h; i++) { line = data + i * wpl; line2 = data2 + i * wpl; for (j = 0; j < w8; j++) { val2 = l_getDataByte(line, j); count2 += val2; val2 += 0xbbbbbb00; l_setDataByte(line2, j, val2); } } ret = compareResults(pixs, pixt1, pixt2, count1, count2, "8 bpp"); nerrors += ret; /* Test different methods for 16 bpp */ count1 = 0; w16 = w / 16; for (i = 0; i < h; i++) { for (j = 0; j < w16; j++) { val1 = GET_DATA_TWO_BYTES(lines1[i], j); count1 += val1; val1 += 0xbbbb0000; SET_DATA_TWO_BYTES(linet1[i], j, val1); } } count2 = 0; for (i = 0; i < h; i++) { line = data + i * wpl; line2 = data2 + i * wpl; for (j = 0; j < w16; j++) { val2 = l_getDataTwoBytes(line, j); count2 += val2; val2 += 0xbbbb0000; l_setDataTwoBytes(line2, j, val2); } } ret = compareResults(pixs, pixt1, pixt2, count1, count2, "16 bpp"); nerrors += ret; /* Test different methods for 32 bpp */ count1 = 0; w32 = w / 32; for (i = 0; i < h; i++) { for (j = 0; j < w32; j++) { val1 = GET_DATA_FOUR_BYTES(lines1[i], j); count1 += val1 & 0xfff; SET_DATA_FOUR_BYTES(linet1[i], j, val1); } } count2 = 0; for (i = 0; i < h; i++) { line = data + i * wpl; line2 = data2 + i * wpl; for (j = 0; j < w32; j++) { val2 = l_getDataFourBytes(line, j); count2 += val2 & 0xfff; l_setDataFourBytes(line2, j, val2); } } ret = compareResults(pixs, pixt1, pixt2, count1, count2, "32 bpp"); nerrors += ret; if (!nerrors) fprintf(stderr, "**** No errors ****\n"); else fprintf(stderr, "**** %d errors found! ****\n", nerrors); pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); lept_free(lines1); lept_free(linet1); lept_free(linet2); return 0; }
// Degrade the pix as if by a print/copy/scan cycle with exposure > 0 // corresponding to darkening on the copier and <0 lighter and 0 not copied. // Exposures in [-2,2] are most useful, with -3 and 3 being extreme. // If rotation is NULL, rotation is skipped. If *rotation is non-zero, the pix // is rotated by *rotation else it is randomly rotated and *rotation is // modified. // // HOW IT WORKS: // Most of the process is really dictated by the fact that the minimum // available convolution is 3X3, which is too big really to simulate a // good quality print/scan process. (2X2 would be better.) // 1 pixel wide inputs are heavily smeared by the 3X3 convolution, making the // images generally biased to being too light, so most of the work is to make // them darker. 3 levels of thickening/darkening are achieved with 2 dilations, // (using a greyscale erosion) one heavy (by being before convolution) and one // light (after convolution). // With no dilation, after covolution, the images are so light that a heavy // constant offset is required to make the 0 image look reasonable. A simple // constant offset multiple of exposure to undo this value is enough to achieve // all the required lightening. This gives the advantage that exposure level 1 // with a single dilation gives a good impression of the broken-yet-too-dark // problem that is often seen in scans. // A small random rotation gives some varying greyscale values on the edges, // and some random salt and pepper noise on top helps to realistically jaggy-up // the edges. // Finally a greyscale ramp provides a continuum of effects between exposure // levels. Pix* DegradeImage(Pix* input, int exposure, TRand* randomizer, float* rotation) { Pix* pix = pixConvertTo8(input, false); pixDestroy(&input); input = pix; int width = pixGetWidth(input); int height = pixGetHeight(input); if (exposure >= 2) { // An erosion simulates the spreading darkening of a dark copy. // This is backwards to binary morphology, // see http://www.leptonica.com/grayscale-morphology.html pix = input; input = pixErodeGray(pix, 3, 3); pixDestroy(&pix); } // A convolution is essential to any mode as no scanner produces an // image as sharp as the electronic image. pix = pixBlockconv(input, 1, 1); pixDestroy(&input); // A small random rotation helps to make the edges jaggy in a realistic way. if (rotation != NULL) { float radians_clockwise = 0.0f; if (*rotation) { radians_clockwise = *rotation; } else if (randomizer != NULL) { radians_clockwise = randomizer->SignedRand(kRotationRange); } input = pixRotate(pix, radians_clockwise, L_ROTATE_AREA_MAP, L_BRING_IN_WHITE, 0, 0); // Rotate the boxes to match. *rotation = radians_clockwise; pixDestroy(&pix); } else { input = pix; } if (exposure >= 3 || exposure == 1) { // Erosion after the convolution is not as heavy as before, so it is // good for level 1 and in addition as a level 3. // This is backwards to binary morphology, // see http://www.leptonica.com/grayscale-morphology.html pix = input; input = pixErodeGray(pix, 3, 3); pixDestroy(&pix); } // The convolution really needed to be 2x2 to be realistic enough, but // we only have 3x3, so we have to bias the image darker or lose thin // strokes. int erosion_offset = 0; // For light and 0 exposure, there is no dilation, so compensate for the // convolution with a big darkening bias which is undone for lighter // exposures. if (exposure <= 0) erosion_offset = -3 * kExposureFactor; // Add in a general offset of the greyscales for the exposure level so // a threshold of 128 gives a reasonable binary result. erosion_offset -= exposure * kExposureFactor; // Add a gradual fade over the page and a small amount of salt and pepper // noise to simulate noise in the sensor/paper fibres and varying // illumination. l_uint32* data = pixGetData(input); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { int pixel = GET_DATA_BYTE(data, x); if (randomizer != NULL) pixel += randomizer->IntRand() % (kSaltnPepper*2 + 1) - kSaltnPepper; if (height + width > kMinRampSize) pixel -= (2*x + y) * 32 / (height + width); pixel += erosion_offset; if (pixel < 0) pixel = 0; if (pixel > 255) pixel = 255; SET_DATA_BYTE(data, x, pixel); } data += input->wpl; } return input; }
/*! * pixRotateBySampling() * * Input: pixs (1, 2, 4, 8, 16, 32 bpp rgb; can be cmapped) * xcen (x value of center of rotation) * ycen (y value of center of rotation) * angle (radians; clockwise is positive) * incolor (L_BRING_IN_WHITE, L_BRING_IN_BLACK) * Return: pixd, or null on error * * Notes: * (1) For very small rotations, just return a clone. * (2) Rotation brings either white or black pixels in * from outside the image. * (3) Colormaps are retained. */ PIX * pixRotateBySampling(PIX *pixs, l_int32 xcen, l_int32 ycen, l_float32 angle, l_int32 incolor) { l_int32 w, h, d, i, j, x, y, xdif, ydif, wm1, hm1, wpld; l_uint32 val; l_float32 sina, cosa; l_uint32 *datad, *lined; void **lines; PIX *pixd; PROCNAME("pixRotateBySampling"); if (!pixs) return (PIX *) ERROR_PTR("pixs not defined", procName, NULL); if (incolor != L_BRING_IN_WHITE && incolor != L_BRING_IN_BLACK) return (PIX *) ERROR_PTR("invalid incolor", procName, NULL); pixGetDimensions(pixs, &w, &h, &d); if (d != 1 && d != 2 && d != 4 && d != 8 && d != 16 && d != 32) return (PIX *) ERROR_PTR("invalid depth", procName, NULL); if (L_ABS(angle) < MIN_ANGLE_TO_ROTATE) return pixClone(pixs); if ((pixd = pixCreateTemplateNoInit(pixs)) == NULL) return (PIX *) ERROR_PTR("pixd not made", procName, NULL); pixSetBlackOrWhite(pixd, incolor); sina = sin(angle); cosa = cos(angle); datad = pixGetData(pixd); wpld = pixGetWpl(pixd); wm1 = w - 1; hm1 = h - 1; lines = pixGetLinePtrs(pixs, NULL); /* Treat 1 bpp case specially */ if (d == 1) { for (i = 0; i < h; i++) { /* scan over pixd */ lined = datad + i * wpld; ydif = ycen - i; for (j = 0; j < w; j++) { xdif = xcen - j; x = xcen + (l_int32)(-xdif * cosa - ydif * sina); if (x < 0 || x > wm1) continue; y = ycen + (l_int32)(-ydif * cosa + xdif * sina); if (y < 0 || y > hm1) continue; if (incolor == L_BRING_IN_WHITE) { if (GET_DATA_BIT(lines[y], x)) SET_DATA_BIT(lined, j); } else { if (!GET_DATA_BIT(lines[y], x)) CLEAR_DATA_BIT(lined, j); } } } FREE(lines); return pixd; } for (i = 0; i < h; i++) { /* scan over pixd */ lined = datad + i * wpld; ydif = ycen - i; for (j = 0; j < w; j++) { xdif = xcen - j; x = xcen + (l_int32)(-xdif * cosa - ydif * sina); if (x < 0 || x > wm1) continue; y = ycen + (l_int32)(-ydif * cosa + xdif * sina); if (y < 0 || y > hm1) continue; switch (d) { case 8: val = GET_DATA_BYTE(lines[y], x); SET_DATA_BYTE(lined, j, val); break; case 32: val = GET_DATA_FOUR_BYTES(lines[y], x); SET_DATA_FOUR_BYTES(lined, j, val); break; case 2: val = GET_DATA_DIBIT(lines[y], x); SET_DATA_DIBIT(lined, j, val); break; case 4: val = GET_DATA_QBIT(lines[y], x); SET_DATA_QBIT(lined, j, val); break; case 16: val = GET_DATA_TWO_BYTES(lines[y], x); SET_DATA_TWO_BYTES(lined, j, val); break; default: return (PIX *) ERROR_PTR("invalid depth", procName, NULL); } } } FREE(lines); return pixd; }
/*! * pixLocToColorTransform() * * Input: pixs (1 bpp) * Return: pixd (32 bpp rgb), or null on error * * Notes: * (1) This generates an RGB image where each component value * is coded depending on the (x.y) location and the size * of the fg connected component that the pixel in pixs belongs to. * It is independent of the 4-fold orthogonal orientation, and * only weakly depends on translations and small angle rotations. * Background pixels are black. * (2) Such encodings can be compared between two 1 bpp images * by performing this transform and calculating the * "earth-mover" distance on the resulting R,G,B histograms. */ PIX * pixLocToColorTransform(PIX *pixs) { l_int32 w, h, w2, h2, wpls, wplr, wplg, wplb, wplcc, i, j, rval, gval, bval; l_float32 invw2, invh2; l_uint32 *datas, *datar, *datag, *datab, *datacc; l_uint32 *lines, *liner, *lineg, *lineb, *linecc; PIX *pix1, *pixcc, *pixr, *pixg, *pixb, *pixd; PROCNAME("pixLocToColorTransform"); if (!pixs || pixGetDepth(pixs) != 1) return (PIX *)ERROR_PTR("pixs undefined or not 1 bpp", procName, NULL); /* Label each pixel with the area of the c.c. to which it belongs. * Clip the result to 255 in an 8 bpp pix. This is used for * the blue component of pixd. */ pixGetDimensions(pixs, &w, &h, NULL); w2 = w / 2; h2 = h / 2; invw2 = 255.0 / (l_float32)w2; invh2 = 255.0 / (l_float32)h2; pix1 = pixConnCompAreaTransform(pixs, 8); pixcc = pixConvert16To8(pix1, L_CLIP_TO_255); pixDestroy(&pix1); /* Label the red and green components depending on the location * of the fg pixels, in a way that is 4-fold rotationally invariant. */ pixr = pixCreate(w, h, 8); pixg = pixCreate(w, h, 8); pixb = pixCreate(w, h, 8); wpls = pixGetWpl(pixs); wplr = pixGetWpl(pixr); wplg = pixGetWpl(pixg); wplb = pixGetWpl(pixb); wplcc = pixGetWpl(pixcc); datas = pixGetData(pixs); datar = pixGetData(pixr); datag = pixGetData(pixg); datab = pixGetData(pixb); datacc = pixGetData(pixcc); for (i = 0; i < h; i++) { lines = datas + i * wpls; liner = datar + i * wplr; lineg = datag + i * wplg; lineb = datab + i * wplb; linecc = datacc+ i * wplcc; for (j = 0; j < w; j++) { if (GET_DATA_BIT(lines, j) == 0) continue; if (w < h) { rval = invh2 * L_ABS((l_float32)(i - h2)); gval = invw2 * L_ABS((l_float32)(j - w2)); } else { rval = invw2 * L_ABS((l_float32)(j - w2)); gval = invh2 * L_ABS((l_float32)(i - h2)); } bval = GET_DATA_BYTE(linecc, j); SET_DATA_BYTE(liner, j, rval); SET_DATA_BYTE(lineg, j, gval); SET_DATA_BYTE(lineb, j, bval); } } pixd = pixCreateRGBImage(pixr, pixg, pixb); pixDestroy(&pixcc); pixDestroy(&pixr); pixDestroy(&pixg); pixDestroy(&pixb); return pixd; }
/*! * pixMinOrMax() * * Input: pixd (<optional> destination: this can be null, * equal to pixs1, or different from pixs1) * pixs1 (can be == to pixd) * pixs2 * type (L_CHOOSE_MIN, L_CHOOSE_MAX) * Return: pixd always * * Notes: * (1) This gives the min or max of two images. * (2) The depth can be 8 or 16 bpp. * (3) There are 3 cases: * - if pixd == null, Min(src1, src2) --> new pixd * - if pixd == pixs1, Min(src1, src2) --> src1 (in-place) * - if pixd != pixs1, Min(src1, src2) --> input pixd */ PIX * pixMinOrMax(PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 type) { l_int32 d, ws, hs, w, h, wpls, wpld, i, j; l_int32 vals, vald, val; l_uint32 *datas, *datad, *lines, *lined; PROCNAME("pixMinOrMax"); if (!pixs1) return (PIX *)ERROR_PTR("pixs1 not defined", procName, pixd); if (!pixs2) return (PIX *)ERROR_PTR("pixs2 not defined", procName, pixd); if (pixs1 == pixs2) return (PIX *)ERROR_PTR("pixs1 and pixs2 must differ", procName, pixd); if (type != L_CHOOSE_MIN && type != L_CHOOSE_MAX) return (PIX *)ERROR_PTR("invalid type", procName, pixd); d = pixGetDepth(pixs1); if (pixGetDepth(pixs2) != d) return (PIX *)ERROR_PTR("depths unequal", procName, pixd); if (d != 8 && d != 16) return (PIX *)ERROR_PTR("depth not 8 or 16 bpp", procName, pixd); if (pixs1 != pixd) pixd = pixCopy(pixd, pixs1); pixGetDimensions(pixs2, &ws, &hs, NULL); pixGetDimensions(pixd, &w, &h, NULL); w = L_MIN(w, ws); h = L_MIN(h, hs); datas = pixGetData(pixs2); datad = pixGetData(pixd); wpls = pixGetWpl(pixs2); wpld = pixGetWpl(pixd); for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; if (d == 8) { if (type == L_CHOOSE_MIN) { for (j = 0; j < w; j++) { vals = GET_DATA_BYTE(lines, j); vald = GET_DATA_BYTE(lined, j); val = L_MIN(vals, vald); SET_DATA_BYTE(lined, j, val); } } else { /* type == L_CHOOSE_MAX */ for (j = 0; j < w; j++) { vals = GET_DATA_BYTE(lines, j); vald = GET_DATA_BYTE(lined, j); val = L_MAX(vals, vald); SET_DATA_BYTE(lined, j, val); } } } else { /* d == 16 */ if (type == L_CHOOSE_MIN) { for (j = 0; j < w; j++) { vals = GET_DATA_TWO_BYTES(lines, j); vald = GET_DATA_TWO_BYTES(lined, j); val = L_MIN(vals, vald); SET_DATA_TWO_BYTES(lined, j, val); } } else { /* type == L_CHOOSE_MAX */ for (j = 0; j < w; j++) { vals = GET_DATA_TWO_BYTES(lines, j); vald = GET_DATA_TWO_BYTES(lined, j); val = L_MAX(vals, vald); SET_DATA_TWO_BYTES(lined, j, val); } } } } return pixd; }
/*! * \brief pixReadStreamPnm() * * \param[in] fp file stream opened for read * \return pix, or NULL on error */ PIX * pixReadStreamPnm(FILE *fp) { l_uint8 val8, rval8, gval8, bval8; l_uint16 val16; l_int32 w, h, d, bpl, wpl, i, j, type; l_int32 val, rval, gval, bval; l_uint32 rgbval; l_uint32 *line, *data; PIX *pix; PROCNAME("pixReadStreamPnm"); if (!fp) return (PIX *)ERROR_PTR("fp not defined", procName, NULL); if (freadHeaderPnm(fp, &w, &h, &d, &type, NULL, NULL)) return (PIX *)ERROR_PTR( "header read failed", procName, NULL); if ((pix = pixCreate(w, h, d)) == NULL) return (PIX *)ERROR_PTR( "pix not made", procName, NULL); pixSetInputFormat(pix, IFF_PNM); data = pixGetData(pix); wpl = pixGetWpl(pix); /* Old "ascii" format */ if (type <= 3) { for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (type == 1 || type == 2) { if (pnmReadNextAsciiValue(fp, &val)) return (PIX *)ERROR_PTR( "read abend", procName, pix); pixSetPixel(pix, j, i, val); } else { /* type == 3 */ if (pnmReadNextAsciiValue(fp, &rval)) return (PIX *)ERROR_PTR( "read abend", procName, pix); if (pnmReadNextAsciiValue(fp, &gval)) return (PIX *)ERROR_PTR( "read abend", procName, pix); if (pnmReadNextAsciiValue(fp, &bval)) return (PIX *)ERROR_PTR( "read abend", procName, pix); composeRGBPixel(rval, gval, bval, &rgbval); pixSetPixel(pix, j, i, rgbval); } } } return pix; } /* "raw" format for 1 bpp */ if (type == 4) { bpl = (d * w + 7) / 8; for (i = 0; i < h; i++) { line = data + i * wpl; for (j = 0; j < bpl; j++) { if (fread(&val8, 1, 1, fp) != 1) return (PIX *)ERROR_PTR( "read error in 4", procName, pix); SET_DATA_BYTE(line, j, val8); } } return pix; } /* "raw" format for grayscale */ if (type == 5) { bpl = (d * w + 7) / 8; for (i = 0; i < h; i++) { line = data + i * wpl; if (d != 16) { for (j = 0; j < w; j++) { if (fread(&val8, 1, 1, fp) != 1) return (PIX *)ERROR_PTR( "error in 5", procName, pix); if (d == 2) SET_DATA_DIBIT(line, j, val8); else if (d == 4) SET_DATA_QBIT(line, j, val8); else /* d == 8 */ SET_DATA_BYTE(line, j, val8); } } else { /* d == 16 */ for (j = 0; j < w; j++) { if (fread(&val16, 2, 1, fp) != 1) return (PIX *)ERROR_PTR( "16 bpp error", procName, pix); SET_DATA_TWO_BYTES(line, j, val16); } } } return pix; } /* "raw" format, type == 6; rgb */ for (i = 0; i < h; i++) { line = data + i * wpl; for (j = 0; j < wpl; j++) { if (fread(&rval8, 1, 1, fp) != 1) return (PIX *)ERROR_PTR( "read error type 6", procName, pix); if (fread(&gval8, 1, 1, fp) != 1) return (PIX *)ERROR_PTR( "read error type 6", procName, pix); if (fread(&bval8, 1, 1, fp) != 1) return (PIX *)ERROR_PTR( "read error type 6", procName, pix); composeRGBPixel(rval8, gval8, bval8, &rgbval); line[j] = rgbval; } } return pix; }
/*! * erodeGrayLow() * * Input: datad, w, h, wpld (8 bpp image) * datas, wpls (8 bpp image, of same dimensions) * size (full length of SEL; restricted to odd numbers) * direction (L_HORIZ or L_VERT) * buffer (holds full line or column of src image pixels) * minarray (array of dimension 2*size+1) * Return: void * * Notes: * (1) See notes in dilateGrayLow() */ static void erodeGrayLow(l_uint32 *datad, l_int32 w, l_int32 h, l_int32 wpld, l_uint32 *datas, l_int32 wpls, l_int32 size, l_int32 direction, l_uint8 *buffer, l_uint8 *minarray) { l_int32 i, j, k; l_int32 hsize, nsteps, startmin, startx, starty; l_uint8 minval; l_uint32 *lines, *lined; if (direction == L_HORIZ) { hsize = size / 2; nsteps = (w - 2 * hsize) / size; for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; /* fill buffer with pixels in byte order */ for (j = 0; j < w; j++) buffer[j] = GET_DATA_BYTE(lines, j); for (j = 0; j < nsteps; j++) { /* refill the minarray */ startmin = (j + 1) * size - 1; minarray[size - 1] = buffer[startmin]; for (k = 1; k < size; k++) { minarray[size - 1 - k] = L_MIN(minarray[size - k], buffer[startmin - k]); minarray[size - 1 + k] = L_MIN(minarray[size + k - 2], buffer[startmin + k]); } /* compute erosion values */ startx = hsize + j * size; SET_DATA_BYTE(lined, startx, minarray[0]); SET_DATA_BYTE(lined, startx + size - 1, minarray[2 * size - 2]); for (k = 1; k < size - 1; k++) { minval = L_MIN(minarray[k], minarray[k + size - 1]); SET_DATA_BYTE(lined, startx + k, minval); } } } } else { /* direction == L_VERT */ hsize = size / 2; nsteps = (h - 2 * hsize) / size; for (j = 0; j < w; j++) { /* fill buffer with pixels in byte order */ for (i = 0; i < h; i++) { lines = datas + i * wpls; buffer[i] = GET_DATA_BYTE(lines, j); } for (i = 0; i < nsteps; i++) { /* refill the minarray */ startmin = (i + 1) * size - 1; minarray[size - 1] = buffer[startmin]; for (k = 1; k < size; k++) { minarray[size - 1 - k] = L_MIN(minarray[size - k], buffer[startmin - k]); minarray[size - 1 + k] = L_MIN(minarray[size + k - 2], buffer[startmin + k]); } /* compute erosion values */ starty = hsize + i * size; lined = datad + starty * wpld; SET_DATA_BYTE(lined, j, minarray[0]); SET_DATA_BYTE(lined + (size - 1) * wpld, j, minarray[2 * size - 2]); for (k = 1; k < size - 1; k++) { minval = L_MIN(minarray[k], minarray[k + size - 1]); SET_DATA_BYTE(lined + wpld * k, j, minval); } } } } return; }
/*! * pixReadStreamPng() * * Input: stream * Return: pix, or null on error * * Notes: * (1) If called from pixReadStream(), the stream is positioned * at the beginning of the file. * (2) To do sequential reads of png format images from a stream, * use pixReadStreamPng() */ PIX * pixReadStreamPng(FILE *fp) { l_uint8 rval, gval, bval; l_int32 i, j, k; l_int32 wpl, d, spp, cindex; l_uint32 png_transforms; l_uint32 *data, *line, *ppixel; int num_palette, num_text; png_byte bit_depth, color_type, channels; png_uint_32 w, h, rowbytes; png_uint_32 xres, yres; png_bytep rowptr; png_bytep *row_pointers; png_structp png_ptr; png_infop info_ptr, end_info; png_colorp palette; png_textp text_ptr; /* ptr to text_chunk */ PIX *pix; PIXCMAP *cmap; PROCNAME("pixReadStreamPng"); if (!fp) return (PIX *)ERROR_PTR("fp not defined", procName, NULL); pix = NULL; /* Allocate the 3 data structures */ if ((png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL)) == NULL) return (PIX *)ERROR_PTR("png_ptr not made", procName, NULL); if ((info_ptr = png_create_info_struct(png_ptr)) == NULL) { png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); return (PIX *)ERROR_PTR("info_ptr not made", procName, NULL); } if ((end_info = png_create_info_struct(png_ptr)) == NULL) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); return (PIX *)ERROR_PTR("end_info not made", procName, NULL); } /* Set up png setjmp error handling */ if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); return (PIX *)ERROR_PTR("internal png error", procName, NULL); } png_init_io(png_ptr, fp); /* ---------------------------------------------------------- * * Set the transforms flags. Whatever happens here, * NEVER invert 1 bpp using PNG_TRANSFORM_INVERT_MONO. * ---------------------------------------------------------- */ /* To strip 16 --> 8 bit depth, use PNG_TRANSFORM_STRIP_16 */ if (var_PNG_STRIP_16_TO_8 == 1) /* our default */ png_transforms = PNG_TRANSFORM_STRIP_16; else png_transforms = PNG_TRANSFORM_IDENTITY; /* To remove alpha channel, use PNG_TRANSFORM_STRIP_ALPHA */ if (var_PNG_STRIP_ALPHA == 1) /* our default */ png_transforms |= PNG_TRANSFORM_STRIP_ALPHA; /* Read it */ png_read_png(png_ptr, info_ptr, png_transforms, NULL); row_pointers = png_get_rows(png_ptr, info_ptr); w = png_get_image_width(png_ptr, info_ptr); h = png_get_image_height(png_ptr, info_ptr); bit_depth = png_get_bit_depth(png_ptr, info_ptr); rowbytes = png_get_rowbytes(png_ptr, info_ptr); color_type = png_get_color_type(png_ptr, info_ptr); channels = png_get_channels(png_ptr, info_ptr); spp = channels; if (spp == 1) d = bit_depth; else if (spp == 2) { d = 2 * bit_depth; L_WARNING("there shouldn't be 2 spp!", procName); } else /* spp == 3 (rgb), spp == 4 (rgba) */ d = 4 * bit_depth; /* Remove if/when this is implemented for all bit_depths */ if (spp == 3 && bit_depth != 8) { fprintf(stderr, "Help: spp = 3 and depth = %d != 8\n!!", bit_depth); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); return (PIX *)ERROR_PTR("not implemented for this depth", procName, NULL); } if (color_type == PNG_COLOR_TYPE_PALETTE || color_type == PNG_COLOR_MASK_PALETTE) { /* generate a colormap */ png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette); cmap = pixcmapCreate(d); /* spp == 1 */ for (cindex = 0; cindex < num_palette; cindex++) { rval = palette[cindex].red; gval = palette[cindex].green; bval = palette[cindex].blue; pixcmapAddColor(cmap, rval, gval, bval); } } else cmap = NULL; if ((pix = pixCreate(w, h, d)) == NULL) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); return (PIX *)ERROR_PTR("pix not made", procName, NULL); } wpl = pixGetWpl(pix); data = pixGetData(pix); pixSetColormap(pix, cmap); if (spp == 1) { /* copy straight from buffer to pix */ for (i = 0; i < h; i++) { line = data + i * wpl; rowptr = row_pointers[i]; for (j = 0; j < rowbytes; j++) { SET_DATA_BYTE(line, j, rowptr[j]); } } } else { /* spp == 3 or spp == 4 */ for (i = 0; i < h; i++) { ppixel = data + i * wpl; rowptr = row_pointers[i]; for (j = k = 0; j < w; j++) { SET_DATA_BYTE(ppixel, COLOR_RED, rowptr[k++]); SET_DATA_BYTE(ppixel, COLOR_GREEN, rowptr[k++]); SET_DATA_BYTE(ppixel, COLOR_BLUE, rowptr[k++]); if (spp == 4) SET_DATA_BYTE(ppixel, L_ALPHA_CHANNEL, rowptr[k++]); ppixel++; } } } #if DEBUG if (cmap) { for (i = 0; i < 16; i++) { fprintf(stderr, "[%d] = %d\n", i, ((l_uint8 *)(cmap->array))[i]); } } #endif /* DEBUG */ /* If there is no colormap, PNG defines black = 0 and * white = 1 by default for binary monochrome. Therefore, * since we use the opposite definition, we must invert * the image colors in either of these cases: * (i) there is no colormap (default) * (ii) there is a colormap which defines black to * be 0 and white to be 1. * We cannot use the PNG_TRANSFORM_INVERT_MONO flag * because that flag (since version 1.0.9) inverts 8 bpp * grayscale as well, which we don't want to do. * (It also doesn't work if there is a colormap.) * If there is a colormap that defines black = 1 and * white = 0, we don't need to do anything. * * How do we check the polarity of the colormap? * The colormap determines the values of black and * white pixels in the following way: * if black = 1 (255), white = 0 * 255, 255, 255, 0, 0, 0, 0, 0, 0 * if black = 0, white = 1 (255) * 0, 0, 0, 0, 255, 255, 255, 0 * So we test the first byte to see if it is 0; * if so, invert the colors. * * If there is a colormap, after inverting the pixels it is * necessary to destroy the colormap. Otherwise, if someone were * to call pixRemoveColormap(), this would cause the pixel * values to be inverted again! */ if (d == 1 && (!cmap || (cmap && ((l_uint8 *)(cmap->array))[0] == 0x0))) { /* fprintf(stderr, "Inverting binary data on png read\n"); */ pixInvert(pix, pix); pixDestroyColormap(pix); } xres = png_get_x_pixels_per_meter(png_ptr, info_ptr); yres = png_get_y_pixels_per_meter(png_ptr, info_ptr); pixSetXRes(pix, (l_int32)((l_float32)xres / 39.37 + 0.5)); /* to ppi */ pixSetYRes(pix, (l_int32)((l_float32)yres / 39.37 + 0.5)); /* to ppi */ /* Get the text if there is any */ png_get_text(png_ptr, info_ptr, &text_ptr, &num_text); if (num_text && text_ptr) pixSetText(pix, text_ptr->text); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); return pix; }
/*! * thresholdToValueLow() */ void thresholdToValueLow(l_uint32 *datad, l_int32 w, l_int32 h, l_int32 d, l_int32 wpld, l_int32 threshval, l_int32 setval) { l_int32 i, j, setabove; l_uint32 *lined; if (setval > threshval) setabove = TRUE; else setabove = FALSE; for (i = 0; i < h; i++) { lined = datad + i * wpld; if (setabove == TRUE) { if (d == 8) { for (j = 0; j < w; j++) { if (GET_DATA_BYTE(lined, j) - threshval >= 0) SET_DATA_BYTE(lined, j, setval); } } else if (d == 16) { for (j = 0; j < w; j++) { if (GET_DATA_TWO_BYTES(lined, j) - threshval >= 0) SET_DATA_TWO_BYTES(lined, j, setval); } } else { /* d == 32 */ for (j = 0; j < w; j++) { if (*(lined + j) >= threshval) *(lined + j) = setval; } } } else { /* set if below or at threshold */ if (d == 8) { for (j = 0; j < w; j++) { if (GET_DATA_BYTE(lined, j) - threshval <= 0) SET_DATA_BYTE(lined, j, setval); } } else if (d == 16) { for (j = 0; j < w; j++) { if (GET_DATA_TWO_BYTES(lined, j) - threshval <= 0) SET_DATA_TWO_BYTES(lined, j, setval); } } else { /* d == 32 */ for (j = 0; j < w; j++) { if (*(lined + j) <= threshval) *(lined + j) = setval; } } } } return; }
/*! * \brief pixAssignToNearestColor() * * \param[in] pixd 8 bpp, colormapped * \param[in] pixs 32 bpp; 24-bit color * \param[in] pixm [optional] 1 bpp * \param[in] level of octcube used for finding nearest color in cmap * \param[in] countarray [optional] ptr to array, in which we can store * the number of pixels found in each color in * the colormap in pixd * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) This is used in phase 2 of color segmentation, where pixs * is the original input image to pixColorSegment(), and * pixd is the colormapped image returned from * pixColorSegmentCluster(). It is also used, with a mask, * in phase 4. * (2) This is an in-place operation. * (3) The colormap in pixd is unchanged. * (4) pixs and pixd must be the same size (w, h). * (5) The selection mask pixm can be null. If it exists, it must * be the same size as pixs and pixd, and only pixels * corresponding to fg in pixm are assigned. Set to * NULL if all pixels in pixd are to be assigned. * (6) The countarray can be null. If it exists, it is pre-allocated * and of a size at least equal to the size of the colormap in pixd. * (7) This does a best-fit (non-greedy) assignment of pixels to * existing clusters. Specifically, it assigns each pixel * in pixd to the color index in the pixd colormap that has a * color closest to the corresponding rgb pixel in pixs. * (8) 'level' is the octcube level used to quickly find the nearest * color in the colormap for each pixel. For color segmentation, * this parameter is set to LEVEL_IN_OCTCUBE. * (9) We build a mapping table from octcube to colormap index so * that this function can run in a time (otherwise) independent * of the number of colors in the colormap. This avoids a * brute-force search for the closest colormap color to each * pixel in the image. * </pre> */ l_int32 pixAssignToNearestColor(PIX *pixd, PIX *pixs, PIX *pixm, l_int32 level, l_int32 *countarray) { l_int32 w, h, wpls, wpld, wplm, i, j, success; l_int32 rval, gval, bval, index; l_int32 *cmaptab; l_uint32 octindex; l_uint32 *rtab, *gtab, *btab; l_uint32 *ppixel; l_uint32 *datas, *datad, *datam, *lines, *lined, *linem; PIXCMAP *cmap; PROCNAME("pixAssignToNearestColor"); if (!pixd) return ERROR_INT("pixd not defined", procName, 1); if ((cmap = pixGetColormap(pixd)) == NULL) return ERROR_INT("cmap not found", procName, 1); if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (pixGetDepth(pixs) != 32) return ERROR_INT("pixs not 32 bpp", procName, 1); if (level < 1 || level > 6) return ERROR_INT("level not in [1 ... 6]", procName, 1); /* Set up the tables to map rgb to the nearest colormap index */ success = TRUE; makeRGBToIndexTables(&rtab, >ab, &btab, level); cmaptab = pixcmapToOctcubeLUT(cmap, level, L_MANHATTAN_DISTANCE); if (!rtab || !gtab || !btab || !cmaptab) { L_ERROR("failure to make a table\n", procName); success = FALSE; goto cleanup_arrays; } pixGetDimensions(pixs, &w, &h, NULL); datas = pixGetData(pixs); datad = pixGetData(pixd); wpls = pixGetWpl(pixs); wpld = pixGetWpl(pixd); if (pixm) { datam = pixGetData(pixm); wplm = pixGetWpl(pixm); } for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; if (pixm) linem = datam + i * wplm; for (j = 0; j < w; j++) { if (pixm) { if (!GET_DATA_BIT(linem, j)) continue; } ppixel = lines + j; rval = GET_DATA_BYTE(ppixel, COLOR_RED); gval = GET_DATA_BYTE(ppixel, COLOR_GREEN); bval = GET_DATA_BYTE(ppixel, COLOR_BLUE); /* Map from rgb to octcube index */ getOctcubeIndexFromRGB(rval, gval, bval, rtab, gtab, btab, &octindex); /* Map from octcube index to nearest colormap index */ index = cmaptab[octindex]; if (countarray) countarray[index]++; SET_DATA_BYTE(lined, j, index); } } cleanup_arrays: LEPT_FREE(cmaptab); LEPT_FREE(rtab); LEPT_FREE(gtab); LEPT_FREE(btab); return (success) ? 0 : 1; }
/*! * absDifferenceLow() * * Finds the absolute value of the difference of each pixel, * for 8 and 16 bpp gray and for 32 bpp rgb. For 32 bpp, the * differences are found for each of the RGB components * separately, and the LSB component is ignored. * The results are written into datad. */ void absDifferenceLow(l_uint32 *datad, l_int32 w, l_int32 h, l_int32 wpld, l_uint32 *datas1, l_uint32 *datas2, l_int32 d, l_int32 wpls) { l_int32 i, j, val1, val2, diff; l_uint32 word1, word2; l_uint32 *lines1, *lines2, *lined, *pdword; PROCNAME("absDifferenceLow"); switch (d) { case 8: for (i = 0; i < h; i++) { lines1 = datas1 + i * wpls; lines2 = datas2 + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) { val1 = GET_DATA_BYTE(lines1, j); val2 = GET_DATA_BYTE(lines2, j); diff = L_ABS(val1 - val2); SET_DATA_BYTE(lined, j, diff); } } break; case 16: for (i = 0; i < h; i++) { lines1 = datas1 + i * wpls; lines2 = datas2 + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) { val1 = GET_DATA_TWO_BYTES(lines1, j); val2 = GET_DATA_TWO_BYTES(lines2, j); diff = L_ABS(val1 - val2); SET_DATA_TWO_BYTES(lined, j, diff); } } break; case 32: for (i = 0; i < h; i++) { lines1 = datas1 + i * wpls; lines2 = datas2 + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) { word1 = lines1[j]; word2 = lines2[j]; pdword = lined + j; val1 = GET_DATA_BYTE(&word1, COLOR_RED); val2 = GET_DATA_BYTE(&word2, COLOR_RED); diff = L_ABS(val1 - val2); SET_DATA_BYTE(pdword, COLOR_RED, diff); val1 = GET_DATA_BYTE(&word1, COLOR_GREEN); val2 = GET_DATA_BYTE(&word2, COLOR_GREEN); diff = L_ABS(val1 - val2); SET_DATA_BYTE(pdword, COLOR_GREEN, diff); val1 = GET_DATA_BYTE(&word1, COLOR_BLUE); val2 = GET_DATA_BYTE(&word2, COLOR_BLUE); diff = L_ABS(val1 - val2); SET_DATA_BYTE(pdword, COLOR_BLUE, diff); } } break; default: L_ERROR("source depth must be 8, 16 or 32 bpp", procName); break; } return; }
/*! * bilateralCreate() * * Input: pixs (8 bpp gray, no colormap) * spatial_stdev (of gaussian kernel; in pixels, > 0.5) * range_stdev (of gaussian range kernel; > 5.0; typ. 50.0) * ncomps (number of intermediate sums J(k,x); in [4 ... 30]) * reduction (1, 2 or 4) * Return: bil, or null on error * * Notes: * (1) This initializes a bilateral filtering operation, generating all * the data required. It takes most of the time in the bilateral * filtering operation. * (2) See bilateral.h for details of the algorithm. * (3) See pixBilateral() for constraints on input parameters, which * are not checked here. */ static L_BILATERAL * bilateralCreate(PIX *pixs, l_float32 spatial_stdev, l_float32 range_stdev, l_int32 ncomps, l_int32 reduction) { l_int32 w, ws, wd, h, hs, hd, i, j, k, index; l_int32 border, minval, maxval, spatial_size; l_int32 halfwidth, wpls, wplt, wpld, kval, nval, dval; l_float32 sstdev, fval1, fval2, denom, sum, norm, kern; l_int32 *nc, *kindex; l_float32 *kfract, *range, *spatial; l_uint32 *datas, *datat, *datad, *lines, *linet, *lined; L_BILATERAL *bil; PIX *pixt, *pixt2, *pixsc, *pixd; PIXA *pixac; PROCNAME("bilateralCreate"); sstdev = spatial_stdev / (l_float32)reduction; /* reduced spat. stdev */ if ((bil = (L_BILATERAL *)CALLOC(1, sizeof(L_BILATERAL))) == NULL) return (L_BILATERAL *)ERROR_PTR("bil not made", procName, NULL); bil->spatial_stdev = sstdev; bil->range_stdev = range_stdev; bil->reduction = reduction; bil->ncomps = ncomps; if (reduction == 1) { pixt = pixClone(pixs); } else if (reduction == 2) { pixt = pixScaleAreaMap2(pixs); } else { /* reduction == 4) */ pixt2 = pixScaleAreaMap2(pixs); pixt = pixScaleAreaMap2(pixt2); pixDestroy(&pixt2); } pixGetExtremeValue(pixt, 1, L_SELECT_MIN, NULL, NULL, NULL, &minval); pixGetExtremeValue(pixt, 1, L_SELECT_MAX, NULL, NULL, NULL, &maxval); bil->minval = minval; bil->maxval = maxval; border = (l_int32)(2 * sstdev + 1); pixsc = pixAddMirroredBorder(pixt, border, border, border, border); bil->pixsc = pixsc; pixDestroy(&pixt); bil->pixs = pixClone(pixs); /* -------------------------------------------------------------------- * * Generate arrays for interpolation of J(k,x): * (1.0 - kfract[.]) * J(kindex[.], x) + kfract[.] * J(kindex[.] + 1, x), * where I(x) is the index into kfract[] and kindex[], * and x is an index into the 2D image array. * -------------------------------------------------------------------- */ /* nc is the set of k values to be used in J(k,x) */ nc = (l_int32 *)CALLOC(ncomps, sizeof(l_int32)); for (i = 0; i < ncomps; i++) nc[i] = minval + i * (maxval - minval) / (ncomps - 1); bil->nc = nc; /* kindex maps from intensity I(x) to the lower k index for J(k,x) */ kindex = (l_int32 *)CALLOC(256, sizeof(l_int32)); for (i = minval, k = 0; i <= maxval && k < ncomps - 1; k++) { fval2 = nc[k + 1]; while (i < fval2) { kindex[i] = k; i++; } } kindex[maxval] = ncomps - 2; bil->kindex = kindex; /* kfract maps from intensity I(x) to the fraction of J(k+1,x) used */ kfract = (l_float32 *)CALLOC(256, sizeof(l_float32)); /* from lower */ for (i = minval, k = 0; i <= maxval && k < ncomps - 1; k++) { fval1 = nc[k]; fval2 = nc[k + 1]; while (i < fval2) { kfract[i] = (l_float32)(i - fval1) / (l_float32)(fval2 - fval1); i++; } } kfract[maxval] = 1.0; bil->kfract = kfract; #if DEBUG_BILATERAL for (i = minval; i <= maxval; i++) fprintf(stderr, "kindex[%d] = %d; kfract[%d] = %5.3f\n", i, kindex[i], i, kfract[i]); for (i = 0; i < ncomps; i++) fprintf(stderr, "nc[%d] = %d\n", i, nc[i]); #endif /* DEBUG_BILATERAL */ /* -------------------------------------------------------------------- * * Generate 1-D kernel arrays (spatial and range) * * -------------------------------------------------------------------- */ spatial_size = 2 * sstdev + 1; spatial = (l_float32 *)CALLOC(spatial_size, sizeof(l_float32)); denom = 2. * sstdev * sstdev; for (i = 0; i < spatial_size; i++) spatial[i] = expf(-(l_float32)(i * i) / denom); bil->spatial = spatial; range = (l_float32 *)CALLOC(256, sizeof(l_float32)); denom = 2. * range_stdev * range_stdev; for (i = 0; i < 256; i++) range[i] = expf(-(l_float32)(i * i) / denom); bil->range = range; /* -------------------------------------------------------------------- * * Generate principal bilateral component images * * -------------------------------------------------------------------- */ pixac = pixaCreate(ncomps); pixGetDimensions(pixsc, &ws, &hs, NULL); datas = pixGetData(pixsc); wpls = pixGetWpl(pixsc); pixGetDimensions(pixs, &w, &h, NULL); wd = (w + reduction - 1) / reduction; hd = (h + reduction - 1) / reduction; halfwidth = (l_int32)(2.0 * sstdev); for (index = 0; index < ncomps; index++) { pixt = pixCopy(NULL, pixsc); datat = pixGetData(pixt); wplt = pixGetWpl(pixt); kval = nc[index]; /* Separable convolutions: horizontal first */ for (i = 0; i < hd; i++) { lines = datas + (border + i) * wpls; linet = datat + (border + i) * wplt; for (j = 0; j < wd; j++) { sum = 0.0; norm = 0.0; for (k = -halfwidth; k <= halfwidth; k++) { nval = GET_DATA_BYTE(lines, border + j + k); kern = spatial[L_ABS(k)] * range[L_ABS(kval - nval)]; sum += kern * nval; norm += kern; } dval = (l_int32)((sum / norm) + 0.5); SET_DATA_BYTE(linet, border + j, dval); } } /* Vertical convolution */ pixd = pixCreate(wd, hd, 8); datad = pixGetData(pixd); wpld = pixGetWpl(pixd); for (i = 0; i < hd; i++) { linet = datat + (border + i) * wplt; lined = datad + i * wpld; for (j = 0; j < wd; j++) { sum = 0.0; norm = 0.0; for (k = -halfwidth; k <= halfwidth; k++) { nval = GET_DATA_BYTE(linet + k * wplt, border + j); kern = spatial[L_ABS(k)] * range[L_ABS(kval - nval)]; sum += kern * nval; norm += kern; } dval = (l_int32)((sum / norm) + 0.5); SET_DATA_BYTE(lined, j, dval); } } pixDestroy(&pixt); pixaAddPix(pixac, pixd, L_INSERT); } bil->pixac = pixac; bil->lineset = (l_uint32 ***)pixaGetLinePtrs(pixac, NULL); return bil; }
/*! * pixSauvolaGetThreshold() * * Input: pixm (8 bpp grayscale; not colormapped) * pixms (32 bpp) * factor (factor for reducing threshold due to variance; >= 0) * &pixsd (<optional return> local standard deviation) * Return: pixd (8 bpp, sauvola threshold values), or null on error * * Notes: * (1) The Sauvola threshold is determined from the formula: * t = m * (1 - k * (1 - s / 128)) * where: * t = local threshold * m = local mean * k = @factor (>= 0) [ typ. 0.35 ] * s = local standard deviation, which is maximized at * 127.5 when half the samples are 0 and half are 255. * (2) See pixSauvolaBinarize() for other details. * (3) Important definitions and relations for computing averages: * v == pixel value * E(p) == expected value of p == average of p over some pixel set * S(v) == square of v == v * v * mv == E(v) == expected pixel value == mean value * ms == E(S(v)) == expected square of pixel values * == mean square value * var == variance == expected square of deviation from mean * == E(S(v - mv)) = E(S(v) - 2 * S(v * mv) + S(mv)) * = E(S(v)) - S(mv) * = ms - mv * mv * s == standard deviation = sqrt(var) * So for evaluating the standard deviation in the Sauvola * threshold, we take * s = sqrt(ms - mv * mv) */ PIX * pixSauvolaGetThreshold(PIX *pixm, PIX *pixms, l_float32 factor, PIX **ppixsd) { l_int32 i, j, w, h, tabsize, wplm, wplms, wplsd, wpld, usetab; l_int32 mv, ms, var, thresh; l_uint32 *datam, *datams, *datasd, *datad; l_uint32 *linem, *linems, *linesd, *lined; l_float32 sd; l_float32 *tab; /* of 2^16 square roots */ PIX *pixsd, *pixd; PROCNAME("pixSauvolaGetThreshold"); if (ppixsd) *ppixsd = NULL; if (!pixm || pixGetDepth(pixm) != 8) return (PIX *)ERROR_PTR("pixm undefined or not 8 bpp", procName, NULL); if (pixGetColormap(pixm)) return (PIX *)ERROR_PTR("pixm is colormapped", procName, NULL); if (!pixms || pixGetDepth(pixms) != 32) return (PIX *)ERROR_PTR("pixms undefined or not 32 bpp", procName, NULL); if (factor < 0.0) return (PIX *)ERROR_PTR("factor must be >= 0", procName, NULL); /* Only make a table of 2^16 square roots if there * are enough pixels to justify it. */ pixGetDimensions(pixm, &w, &h, NULL); usetab = (w * h > 100000) ? 1 : 0; if (usetab) { tabsize = 1 << 16; tab = (l_float32 *)CALLOC(tabsize, sizeof(l_float32)); for (i = 0; i < tabsize; i++) tab[i] = (l_float32)sqrt((l_float64)i); } pixd = pixCreate(w, h, 8); if (ppixsd) { pixsd = pixCreate(w, h, 8); *ppixsd = pixsd; } datam = pixGetData(pixm); datams = pixGetData(pixms); if (ppixsd) datasd = pixGetData(pixsd); datad = pixGetData(pixd); wplm = pixGetWpl(pixm); wplms = pixGetWpl(pixms); if (ppixsd) wplsd = pixGetWpl(pixsd); wpld = pixGetWpl(pixd); for (i = 0; i < h; i++) { linem = datam + i * wplm; linems = datams + i * wplms; if (ppixsd) linesd = datasd + i * wplsd; lined = datad + i * wpld; for (j = 0; j < w; j++) { mv = GET_DATA_BYTE(linem, j); ms = linems[j]; var = ms - mv * mv; if (usetab) sd = tab[var]; else sd = (l_float32)sqrt(var); if (ppixsd) SET_DATA_BYTE(linesd, j, (l_int32)sd); thresh = (l_int32)(mv * (1.0 - factor * (1.0 - sd / 128.))); SET_DATA_BYTE(lined, j, thresh); } } if (usetab) FREE(tab); return pixd; }
/*! * pixMinOrMax() * * Input: pixd (<optional> destination: this can be null, * equal to pixs1, or different from pixs1) * pixs1 (can be == to pixd) * pixs2 * type (L_CHOOSE_MIN, L_CHOOSE_MAX) * Return: pixd always * * Notes: * (1) This gives the min or max of two images, component-wise. * (2) The depth can be 8 or 16 bpp for 1 component, and 32 bpp * for a 3 component image. For 32 bpp, ignore the LSB * of each word (the alpha channel) * (3) There are 3 cases: * - if pixd == null, Min(src1, src2) --> new pixd * - if pixd == pixs1, Min(src1, src2) --> src1 (in-place) * - if pixd != pixs1, Min(src1, src2) --> input pixd */ PIX * pixMinOrMax(PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 type) { l_int32 d, ws, hs, w, h, wpls, wpld, i, j, vals, vald, val; l_int32 rval1, gval1, bval1, rval2, gval2, bval2, rval, gval, bval; l_uint32 *datas, *datad, *lines, *lined; PROCNAME("pixMinOrMax"); if (!pixs1) return (PIX *)ERROR_PTR("pixs1 not defined", procName, pixd); if (!pixs2) return (PIX *)ERROR_PTR("pixs2 not defined", procName, pixd); if (pixs1 == pixs2) return (PIX *)ERROR_PTR("pixs1 and pixs2 must differ", procName, pixd); if (type != L_CHOOSE_MIN && type != L_CHOOSE_MAX) return (PIX *)ERROR_PTR("invalid type", procName, pixd); d = pixGetDepth(pixs1); if (pixGetDepth(pixs2) != d) return (PIX *)ERROR_PTR("depths unequal", procName, pixd); if (d != 8 && d != 16 && d != 32) return (PIX *)ERROR_PTR("depth not 8, 16 or 32 bpp", procName, pixd); if (pixs1 != pixd) pixd = pixCopy(pixd, pixs1); pixGetDimensions(pixs2, &ws, &hs, NULL); pixGetDimensions(pixd, &w, &h, NULL); w = L_MIN(w, ws); h = L_MIN(h, hs); datas = pixGetData(pixs2); datad = pixGetData(pixd); wpls = pixGetWpl(pixs2); wpld = pixGetWpl(pixd); for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; if (d == 8) { for (j = 0; j < w; j++) { vals = GET_DATA_BYTE(lines, j); vald = GET_DATA_BYTE(lined, j); if (type == L_CHOOSE_MIN) val = L_MIN(vals, vald); else /* type == L_CHOOSE_MAX */ val = L_MAX(vals, vald); SET_DATA_BYTE(lined, j, val); } } else if (d == 16) { for (j = 0; j < w; j++) { vals = GET_DATA_TWO_BYTES(lines, j); vald = GET_DATA_TWO_BYTES(lined, j); if (type == L_CHOOSE_MIN) val = L_MIN(vals, vald); else /* type == L_CHOOSE_MAX */ val = L_MAX(vals, vald); SET_DATA_TWO_BYTES(lined, j, val); } } else { /* d == 32 */ for (j = 0; j < w; j++) { extractRGBValues(lines[j], &rval1, &gval1, &bval1); extractRGBValues(lined[j], &rval2, &gval2, &bval2); if (type == L_CHOOSE_MIN) { rval = L_MIN(rval1, rval2); gval = L_MIN(gval1, gval2); bval = L_MIN(bval1, bval2); } else { /* type == L_CHOOSE_MAX */ rval = L_MAX(rval1, rval2); gval = L_MAX(gval1, gval2); bval = L_MAX(bval1, bval2); } composeRGBPixel(rval, gval, bval, lined + j); } } } return pixd; }
/*! * fpixConvertToPix() * * Input: fpixs * outdepth (0, 8, 16 or 32 bpp) * negvals (L_CLIP_TO_ZERO, L_TAKE_ABSVAL) * errorflag (1 to output error stats; 0 otherwise) * Return: pixd, or null on error * * Notes: * (1) Use @outdepth = 0 to programmatically determine the * output depth. If no values are greater than 255, * it will set outdepth = 8; otherwise to 16 or 32. * (2) Because we are converting a float to an unsigned int * with a specified dynamic range (8, 16 or 32 bits), errors * can occur. If errorflag == TRUE, output the number * of values out of range, both negative and positive. * (3) If a pixel value is positive and out of range, clip to * the maximum value represented at the outdepth of 8, 16 * or 32 bits. */ PIX * fpixConvertToPix(FPIX *fpixs, l_int32 outdepth, l_int32 negvals, l_int32 errorflag) { l_int32 w, h, i, j, wpls, wpld, maxval; l_uint32 vald; l_float32 val; l_float32 *datas, *lines; l_uint32 *datad, *lined; PIX *pixd; PROCNAME("fpixConvertToPix"); if (!fpixs) return (PIX *)ERROR_PTR("fpixs not defined", procName, NULL); if (negvals != L_CLIP_TO_ZERO && negvals != L_TAKE_ABSVAL) return (PIX *)ERROR_PTR("invalid negvals", procName, NULL); if (outdepth != 0 && outdepth != 8 && outdepth != 16 && outdepth != 32) return (PIX *)ERROR_PTR("outdepth not in {0,8,16,32}", procName, NULL); fpixGetDimensions(fpixs, &w, &h); datas = fpixGetData(fpixs); wpls = fpixGetWpl(fpixs); /* Adaptive determination of output depth */ if (outdepth == 0) { outdepth = 8; for (i = 0; i < h; i++) { lines = datas + i * wpls; for (j = 0; j < w; j++) { if (lines[j] > 65535.5) { outdepth = 32; break; } if (lines[j] > 255.5) outdepth = 16; } if (outdepth == 32) break; } } maxval = (1 << outdepth) - 1; /* Gather statistics if @errorflag = TRUE */ if (errorflag) { l_int32 negs = 0; l_int32 overvals = 0; for (i = 0; i < h; i++) { lines = datas + i * wpls; for (j = 0; j < w; j++) { val = lines[j]; if (val < 0.0) negs++; else if (val > maxval) overvals++; } } if (negs > 0) L_ERROR_INT("Number of negative values: %d", procName, negs); if (overvals > 0) L_ERROR_INT("Number of too-large values: %d", procName, overvals); } /* Make the pix and convert the data */ if ((pixd = pixCreate(w, h, outdepth)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); datad = pixGetData(pixd); wpld = pixGetWpl(pixd); for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) { val = lines[j]; if (val >= 0.0) vald = (l_uint32)(val + 0.5); else { /* val < 0.0 */ if (negvals == L_CLIP_TO_ZERO) vald = 0; else vald = (l_uint32)(-val + 0.5); } if (vald > maxval) vald = maxval; if (outdepth == 8) SET_DATA_BYTE(lined, j, vald); else if (outdepth == 16) SET_DATA_TWO_BYTES(lined, j, vald); else /* outdepth == 32 */ SET_DATA_FOUR_BYTES(lined, j, vald); } } return pixd; }
/*! * pixThresholdToValue() * * Input: pixd (<optional>; if not null, must be equal to pixs) * pixs (8, 16, 32 bpp) * threshval * setval * Return: pixd always * * Notes: * - operation can be in-place (pixs == pixd) or to a new pixd * - if setval > threshval, sets pixels with a value >= threshval to setval * - if setval < threshval, sets pixels with a value <= threshval to setval * - if setval == threshval, no-op */ PIX * pixThresholdToValue(PIX *pixd, PIX *pixs, l_int32 threshval, l_int32 setval) { l_int32 i, j, w, h, d, wpld, setabove; l_uint32 *datad, *lined; PROCNAME("pixThresholdToValue"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, pixd); d = pixGetDepth(pixs); if (d != 8 && d != 16 && d != 32) return (PIX *)ERROR_PTR("pixs not 8, 16 or 32 bpp", procName, pixd); if (pixd && (pixs != pixd)) return (PIX *)ERROR_PTR("pixd exists and is not pixs", procName, pixd); if (threshval < 0 || setval < 0) return (PIX *)ERROR_PTR("threshval & setval not < 0", procName, pixd); if (d == 8 && setval > 255) return (PIX *)ERROR_PTR("setval > 255 for 8 bpp", procName, pixd); if (d == 16 && setval > 0xffff) return (PIX *)ERROR_PTR("setval > 0xffff for 16 bpp", procName, pixd); if (!pixd) pixd = pixCopy(NULL, pixs); if (setval == threshval) { L_WARNING("setval == threshval; no operation\n", procName); return pixd; } datad = pixGetData(pixd); pixGetDimensions(pixd, &w, &h, NULL); wpld = pixGetWpl(pixd); if (setval > threshval) setabove = TRUE; else setabove = FALSE; for (i = 0; i < h; i++) { lined = datad + i * wpld; if (setabove == TRUE) { if (d == 8) { for (j = 0; j < w; j++) { if (GET_DATA_BYTE(lined, j) - threshval >= 0) SET_DATA_BYTE(lined, j, setval); } } else if (d == 16) { for (j = 0; j < w; j++) { if (GET_DATA_TWO_BYTES(lined, j) - threshval >= 0) SET_DATA_TWO_BYTES(lined, j, setval); } } else { /* d == 32 */ for (j = 0; j < w; j++) { if (*(lined + j) >= threshval) *(lined + j) = setval; } } } else { /* set if below or at threshold */ if (d == 8) { for (j = 0; j < w; j++) { if (GET_DATA_BYTE(lined, j) - threshval <= 0) SET_DATA_BYTE(lined, j, setval); } } else if (d == 16) { for (j = 0; j < w; j++) { if (GET_DATA_TWO_BYTES(lined, j) - threshval <= 0) SET_DATA_TWO_BYTES(lined, j, setval); } } else { /* d == 32 */ for (j = 0; j < w; j++) { if (*(lined + j) <= threshval) *(lined + j) = setval; } } } } return pixd; }
/*! * pixRankFilterGray() * * Input: pixs (8 bpp; no colormap) * wf, hf (width and height of filter; each is >= 1) * rank (in [0.0 ... 1.0]) * Return: pixd (of rank values), or null on error * * Notes: * (1) This defines, for each pixel in pixs, a neighborhood of * pixels given by a rectangle "centered" on the pixel. * This set of wf*hf pixels has a distribution of values, * and if they are sorted in increasing order, we choose * the pixel such that rank*(wf*hf-1) pixels have a lower * or equal value and (1-rank)*(wf*hf-1) pixels have an equal * or greater value. * (2) By this definition, the rank = 0.0 pixel has the lowest * value, and the rank = 1.0 pixel has the highest value. * (3) We add mirrored boundary pixels to avoid boundary effects, * and put the filter center at (0, 0). * (4) This dispatches to grayscale erosion or dilation if the * filter dimensions are odd and the rank is 0.0 or 1.0, rsp. * (5) Returns a copy if both wf and hf are 1. * (6) Uses row-major or column-major incremental updates to the * histograms depending on whether hf > wf or hv <= wf, rsp. */ PIX * pixRankFilterGray(PIX *pixs, l_int32 wf, l_int32 hf, l_float32 rank) { l_int32 w, h, d, i, j, k, m, n, rankloc, wplt, wpld, val, sum; l_int32 *histo, *histo16; l_uint32 *datat, *linet, *datad, *lined; PIX *pixt, *pixd; PROCNAME("pixRankFilterGray"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetColormap(pixs) != NULL) return (PIX *)ERROR_PTR("pixs has colormap", procName, NULL); pixGetDimensions(pixs, &w, &h, &d); if (d != 8) return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL); if (wf < 1 || hf < 1) return (PIX *)ERROR_PTR("wf < 1 || hf < 1", procName, NULL); if (rank < 0.0 || rank > 1.0) return (PIX *)ERROR_PTR("rank must be in [0.0, 1.0]", procName, NULL); if (wf == 1 && hf == 1) /* no-op */ return pixCopy(NULL, pixs); /* For rank = 0.0, this is a grayscale erosion, and for rank = 1.0, * a dilation. Grayscale morphology operations are implemented * for filters of odd dimension, so we dispatch to grayscale * morphology if both wf and hf are odd. Otherwise, we * slightly adjust the rank (to get the correct behavior) and * use the slower rank filter here. */ if (wf % 2 && hf % 2) { if (rank == 0.0) return pixErodeGray(pixs, wf, hf); else if (rank == 1.0) return pixDilateGray(pixs, wf, hf); } if (rank == 0.0) rank = 0.0001; if (rank == 1.0) rank = 0.9999; /* Add wf/2 to each side, and hf/2 to top and bottom of the * image, mirroring for accuracy and to avoid special-casing * the boundary. */ if ((pixt = pixAddMirroredBorder(pixs, wf / 2, wf / 2, hf / 2, hf / 2)) == NULL) return (PIX *)ERROR_PTR("pixt not made", procName, NULL); /* Set up the two histogram arrays. */ histo = (l_int32 *)CALLOC(256, sizeof(l_int32)); histo16 = (l_int32 *)CALLOC(16, sizeof(l_int32)); rankloc = (l_int32)(rank * wf * hf); /* Place the filter center at (0, 0). This is just a * convenient location, because it allows us to perform * the rank filter over x:(0 ... w - 1) and y:(0 ... h - 1). */ pixd = pixCreateTemplate(pixs); datat = pixGetData(pixt); wplt = pixGetWpl(pixt); datad = pixGetData(pixd); wpld = pixGetWpl(pixd); /* If hf > wf, it's more efficient to use row-major scanning. * Otherwise, traverse the image in use column-major order. */ if (hf > wf) { for (j = 0; j < w; j++) { /* row-major */ /* Start each column with clean histogram arrays. */ for (n = 0; n < 256; n++) histo[n] = 0; for (n = 0; n < 16; n++) histo16[n] = 0; for (i = 0; i < h; i++) { /* fast scan on columns */ /* Update the histos for the new location */ lined = datad + i * wpld; if (i == 0) { /* do full histo */ for (k = 0; k < hf; k++) { linet = datat + (i + k) * wplt; for (m = 0; m < wf; m++) { val = GET_DATA_BYTE(linet, j + m); histo[val]++; histo16[val >> 4]++; } } } else { /* incremental update */ linet = datat + (i - 1) * wplt; for (m = 0; m < wf; m++) { /* remove top line */ val = GET_DATA_BYTE(linet, j + m); histo[val]--; histo16[val >> 4]--; } linet = datat + (i + hf - 1) * wplt; for (m = 0; m < wf; m++) { /* add bottom line */ val = GET_DATA_BYTE(linet, j + m); histo[val]++; histo16[val >> 4]++; } } /* Find the rank value */ sum = 0; for (n = 0; n < 16; n++) { /* search over coarse histo */ sum += histo16[n]; if (sum > rankloc) { sum -= histo16[n]; break; } } k = 16 * n; /* starting value in fine histo */ for (m = 0; m < 16; m++) { sum += histo[k]; if (sum > rankloc) { SET_DATA_BYTE(lined, j, k); break; } k++; } } } } else { /* wf >= hf */
/*! * pixAbsDifference() * * Input: pixs1, pixs2 (both either 8 or 16 bpp gray, or 32 bpp RGB) * Return: pixd, or null on error * * Notes: * (1) The depth of pixs1 and pixs2 must be equal. * (2) Clips computation to the min size, aligning the UL corners * (3) For 8 and 16 bpp, assumes one gray component. * (4) For 32 bpp, assumes 3 color components, and ignores the * LSB of each word (the alpha channel) * (5) Computes the absolute value of the difference between * each component value. */ PIX * pixAbsDifference(PIX *pixs1, PIX *pixs2) { l_int32 i, j, w, h, w2, h2, d, wpls1, wpls2, wpld, val1, val2, diff; l_int32 rval1, gval1, bval1, rval2, gval2, bval2, rdiff, gdiff, bdiff; l_uint32 *datas1, *datas2, *datad, *lines1, *lines2, *lined; PIX *pixd; PROCNAME("pixAbsDifference"); if (!pixs1) return (PIX *)ERROR_PTR("pixs1 not defined", procName, NULL); if (!pixs2) return (PIX *)ERROR_PTR("pixs2 not defined", procName, NULL); d = pixGetDepth(pixs1); if (d != pixGetDepth(pixs2)) return (PIX *)ERROR_PTR("src1 and src2 depths unequal", procName, NULL); if (d != 8 && d != 16 && d != 32) return (PIX *)ERROR_PTR("depths not in {8, 16, 32}", procName, NULL); pixGetDimensions(pixs1, &w, &h, NULL); pixGetDimensions(pixs2, &w2, &h2, NULL); w = L_MIN(w, w2); h = L_MIN(h, h2); if ((pixd = pixCreate(w, h, d)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); pixCopyResolution(pixd, pixs1); datas1 = pixGetData(pixs1); datas2 = pixGetData(pixs2); datad = pixGetData(pixd); wpls1 = pixGetWpl(pixs1); wpls2 = pixGetWpl(pixs2); wpld = pixGetWpl(pixd); if (d == 8) { for (i = 0; i < h; i++) { lines1 = datas1 + i * wpls1; lines2 = datas2 + i * wpls2; lined = datad + i * wpld; for (j = 0; j < w; j++) { val1 = GET_DATA_BYTE(lines1, j); val2 = GET_DATA_BYTE(lines2, j); diff = L_ABS(val1 - val2); SET_DATA_BYTE(lined, j, diff); } } } else if (d == 16) { for (i = 0; i < h; i++) { lines1 = datas1 + i * wpls1; lines2 = datas2 + i * wpls2; lined = datad + i * wpld; for (j = 0; j < w; j++) { val1 = GET_DATA_TWO_BYTES(lines1, j); val2 = GET_DATA_TWO_BYTES(lines2, j); diff = L_ABS(val1 - val2); SET_DATA_TWO_BYTES(lined, j, diff); } } } else { /* d == 32 */ for (i = 0; i < h; i++) { lines1 = datas1 + i * wpls1; lines2 = datas2 + i * wpls2; lined = datad + i * wpld; for (j = 0; j < w; j++) { extractRGBValues(lines1[j], &rval1, &gval1, &bval1); extractRGBValues(lines2[j], &rval2, &gval2, &bval2); rdiff = L_ABS(rval1 - rval2); gdiff = L_ABS(gval1 - gval2); bdiff = L_ABS(bval1 - bval2); composeRGBPixel(rdiff, gdiff, bdiff, lined + j); } } } return pixd; }
/*! * \brief pixRunlengthTransform() * * \param[in] pixs 1 bpp * \param[in] color 0 for white runs, 1 for black runs * \param[in] direction L_HORIZONTAL_RUNS, L_VERTICAL_RUNS * \param[in] depth 8 or 16 bpp * \return pixd 8 or 16 bpp, or NULL on error * * <pre> * Notes: * (1) The dest Pix is 8 or 16 bpp, with the pixel values * equal to the runlength in which it is a member. * The length is clipped to the max pixel value if necessary. * (2) The color determines if we're labelling white or black runs. * (3) A pixel that is not a member of the chosen color gets * value 0; it belongs to a run of length 0 of the * chosen color. * (4) To convert for maximum dynamic range, either linear or * log, use pixMaxDynamicRange(). * </pre> */ PIX * pixRunlengthTransform(PIX *pixs, l_int32 color, l_int32 direction, l_int32 depth) { l_int32 i, j, w, h, wpld, bufsize, maxsize, n; l_int32 *start, *end, *buffer; l_uint32 *datad, *lined; PIX *pixt, *pixd; PROCNAME("pixRunlengthTransform"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 1) return (PIX *)ERROR_PTR("pixs not 1 bpp", procName, NULL); if (depth != 8 && depth != 16) return (PIX *)ERROR_PTR("depth must be 8 or 16 bpp", procName, NULL); pixGetDimensions(pixs, &w, &h, NULL); if (direction == L_HORIZONTAL_RUNS) maxsize = 1 + w / 2; else if (direction == L_VERTICAL_RUNS) maxsize = 1 + h / 2; else return (PIX *)ERROR_PTR("invalid direction", procName, NULL); bufsize = L_MAX(w, h); if ((pixd = pixCreate(w, h, depth)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); datad = pixGetData(pixd); wpld = pixGetWpl(pixd); if ((start = (l_int32 *)LEPT_CALLOC(maxsize, sizeof(l_int32))) == NULL) return (PIX *)ERROR_PTR("start not made", procName, NULL); if ((end = (l_int32 *)LEPT_CALLOC(maxsize, sizeof(l_int32))) == NULL) return (PIX *)ERROR_PTR("end not made", procName, NULL); if ((buffer = (l_int32 *)LEPT_CALLOC(bufsize, sizeof(l_int32))) == NULL) return (PIX *)ERROR_PTR("buffer not made", procName, NULL); /* Use fg runs for evaluation */ if (color == 0) pixt = pixInvert(NULL, pixs); else pixt = pixClone(pixs); if (direction == L_HORIZONTAL_RUNS) { for (i = 0; i < h; i++) { pixFindHorizontalRuns(pixt, i, start, end, &n); runlengthMembershipOnLine(buffer, w, depth, start, end, n); lined = datad + i * wpld; if (depth == 8) { for (j = 0; j < w; j++) SET_DATA_BYTE(lined, j, buffer[j]); } else { /* depth == 16 */ for (j = 0; j < w; j++) SET_DATA_TWO_BYTES(lined, j, buffer[j]); } } } else { /* L_VERTICAL_RUNS */ for (j = 0; j < w; j++) { pixFindVerticalRuns(pixt, j, start, end, &n); runlengthMembershipOnLine(buffer, h, depth, start, end, n); if (depth == 8) { for (i = 0; i < h; i++) { lined = datad + i * wpld; SET_DATA_BYTE(lined, j, buffer[i]); } } else { /* depth == 16 */ for (i = 0; i < h; i++) { lined = datad + i * wpld; SET_DATA_TWO_BYTES(lined, j, buffer[i]); } } } } pixDestroy(&pixt); LEPT_FREE(start); LEPT_FREE(end); LEPT_FREE(buffer); return pixd; }