Image thin_image(const Image &box, double THRESHOLD_BOND, const ColorGray &bgColor) { Image image(Geometry(box.columns(), box.rows()), "white"); image.type(GrayscaleType); unsigned int xsize = box.columns(); unsigned int ysize = box.rows(); unsigned char *ptr = (unsigned char*) malloc(xsize * ysize * sizeof(unsigned char)); for (unsigned int i = 0; i < xsize; i++) for (unsigned int j = 0; j < ysize; j++) ptr[i + j * xsize] = get_pixel(box, bgColor, i, j, THRESHOLD_BOND); if (xsize>1 && ysize>1) thin1(ptr, xsize, ysize); for (unsigned int i = 0; i < xsize; i++) for (unsigned int j = 0; j < ysize; j++) if (ptr[i + j * xsize] == 1) image.pixelColor(i, j, "black"); free(ptr); return (image); }
void thin_image(bitmap_type *image, bool bgSpec, pixel bg, at_exception_type * exp) { /* This is nasty as we need to call thin once for each * colour in the image the way I do this is to keep a second * copy of the bitmap and to use this to keep * track of which colours have not yet been processed, * trades time for pathological case memory.....*/ long m, n, num_pixels; bitmap_type bm; unsigned int const spp = image->np; unsigned int const width = image->width; unsigned int const height = image->height; if (bgSpec) background = bg; else PPM_ASSIGN(background, 255, 255, 255); /* Clone the image */ bm.height = image->height; bm.width = image->width; bm.np = image->np; MALLOCARRAY(bm.bitmap, height * width * spp); if (bm.bitmap == NULL) pm_error("Unable to get memory for thin image bitmap clone"); memcpy(bm.bitmap, image->bitmap, height * width * spp); num_pixels = height * width; switch (spp) { case 3: { Pixel *ptr = (Pixel*)bm.bitmap; Pixel bg_color; bg_color[0] = PPM_GETR(background); bg_color[1] = PPM_GETG(background); bg_color[2] = PPM_GETB(background); for (n = num_pixels - 1; n >= 0L; --n) { Pixel p; PIXEL_SET(p, ptr[n]); if (!PIXEL_EQUAL(p, bg_color)) { /* we have a new colour in the image */ LOG3("Thinning colour (%x, %x, %x)\n", p[0], p[1], p[2]); for (m = n - 1; m >= 0L; --m) { if (PIXEL_EQUAL(ptr[m], p)) PIXEL_SET(ptr[m], bg_color); } thin3(image, p); } } break; } case 1: { unsigned char * const ptr = bm.bitmap; unsigned char bg_color; if (PPM_ISGRAY(background)) bg_color = PPM_GETR(background); else bg_color = PPM_LUMIN(background); for (n = num_pixels - 1; n >= 0L; --n) { unsigned char c = ptr[n]; if (c != bg_color) { LOG1 ("Thinning colour %x\n", c); for (m = n - 1; m >= 0L; --m) if (ptr[m] == c) ptr[m] = bg_color; thin1(image, c); } } break; } default: { LOG1 ("thin_image: %u-plane images are not supported", spp); at_exception_fatal(exp, "thin_image: wrong plane images are passed"); goto cleanup; } } cleanup: free (bm.bitmap); }