/*! * pixProjectiveSampled() * * Input: pixs (all depths) * vc (vector of 8 coefficients for projective 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 pixProjective(). See that function * for relative timings between sampled and interpolated. */ PIX * pixProjectiveSampled(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("pixProjectiveSampled"); 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++) { projectiveXformSampledPt(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; }
/*! * pixRotate() * * Input: pixs (1, 2, 4, 8, 32 bpp rgb) * angle (radians; clockwise is positive) * type (L_ROTATE_AREA_MAP, L_ROTATE_SHEAR, L_ROTATE_SAMPLING) * incolor (L_BRING_IN_WHITE, L_BRING_IN_BLACK) * width (original width; use 0 to avoid embedding) * height (original height; use 0 to avoid embedding) * Return: pixd, or null on error * * Notes: * (1) This is a high-level, simple interface for rotating images * about their center. * (2) For very small rotations, just return a clone. * (3) Rotation brings either white or black pixels in * from outside the image. * (4) The rotation type is adjusted if necessary for the image * depth and size of rotation angle. For 1 bpp images, we * rotate either by shear or sampling. * (5) Colormaps are removed for rotation by area mapping. * (6) The dest can be expanded so that no image pixels * are lost. To invoke expansion, input the original * width and height. For repeated rotation, use of the * original width and height allows the expansion to * stop at the maximum required size, which is a square * with side = sqrt(w*w + h*h). * * *** Warning: implicit assumption about RGB component ordering *** */ PIX * pixRotate(PIX *pixs, l_float32 angle, l_int32 type, l_int32 incolor, l_int32 width, l_int32 height) { l_int32 w, h, d; l_uint32 fillval; PIX *pixt1, *pixt2, *pixt3, *pixd; PIXCMAP *cmap; PROCNAME("pixRotate"); if (!pixs) return (PIX *) ERROR_PTR("pixs not defined", procName, NULL); if (type != L_ROTATE_SHEAR && type != L_ROTATE_AREA_MAP && type != L_ROTATE_SAMPLING) return (PIX *) ERROR_PTR("invalid type", procName, NULL); if (incolor != L_BRING_IN_WHITE && incolor != L_BRING_IN_BLACK) return (PIX *) ERROR_PTR("invalid incolor", procName, NULL); if (L_ABS(angle) < MIN_ANGLE_TO_ROTATE) return pixClone(pixs); /* Adjust rotation type if necessary: * - If d == 1 bpp and the angle is more than about 6 degrees, * rotate by sampling; otherwise rotate by shear. * - If d > 1, only allow shear rotation up to about 20 degrees; * beyond that, default a shear request to sampling. */ if (pixGetDepth(pixs) == 1) { if (L_ABS(angle) > MAX_1BPP_SHEAR_ANGLE) { if (type != L_ROTATE_SAMPLING) L_INFO("1 bpp, large angle; rotate by sampling\n", procName); type = L_ROTATE_SAMPLING; } else if (type != L_ROTATE_SHEAR) { L_INFO("1 bpp; rotate by shear\n", procName); type = L_ROTATE_SHEAR; } } else if (L_ABS(angle) > LIMIT_SHEAR_ANGLE && type == L_ROTATE_SHEAR) { L_INFO("large angle; rotate by sampling\n", procName); type = L_ROTATE_SAMPLING; } /* Remove colormap if we rotate by area mapping. */ cmap = pixGetColormap(pixs); if (cmap && type == L_ROTATE_AREA_MAP) pixt1 = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC); else pixt1 = pixClone(pixs); cmap = pixGetColormap(pixt1); /* Otherwise, if there is a colormap and we're not embedding, * add white color if it doesn't exist. */ if (cmap && width == 0) { /* no embedding; generate @incolor */ if (incolor == L_BRING_IN_BLACK) pixcmapAddBlackOrWhite(cmap, 0, NULL); else /* L_BRING_IN_WHITE */ pixcmapAddBlackOrWhite(cmap, 1, NULL); } /* Request to embed in a larger image; do if necessary */ pixt2 = pixEmbedForRotation(pixt1, angle, incolor, width, height); /* Area mapping requires 8 or 32 bpp. If less than 8 bpp and * area map rotation is requested, convert to 8 bpp. */ d = pixGetDepth(pixt2); if (type == L_ROTATE_AREA_MAP && d < 8) pixt3 = pixConvertTo8(pixt2, FALSE); else pixt3 = pixClone(pixt2); /* Do the rotation: shear, sampling or area mapping */ pixGetDimensions(pixt3, &w, &h, &d); if (type == L_ROTATE_SHEAR) { pixd = pixRotateShearCenter(pixt3, angle, incolor); } else if (type == L_ROTATE_SAMPLING) { pixd = pixRotateBySampling(pixt3, w / 2, h / 2, angle, incolor); } else { /* rotate by area mapping */ fillval = 0; if (incolor == L_BRING_IN_WHITE) { if (d == 8) fillval = 255; else /* d == 32 */ fillval = 0xffffff00; } if (d == 8) pixd = pixRotateAMGray(pixt3, angle, fillval); else /* d == 32 */ pixd = pixRotateAMColor(pixt3, angle, fillval); } pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); return pixd; }