FIBITMAP* DLL_CALLCONV FIA_ShiftImageEdgeToCenter(FIBITMAP *src) { int width = FreeImage_GetWidth(src); int height = FreeImage_GetHeight(src); int xhalf = (width / 2); int yhalf = (height / 2); int bytes_pp = FreeImage_GetBPP(src) / 8; int xhalf_bytes = xhalf * bytes_pp; //std::cout << "width " << width << std::endl; //std::cout << "xhalf " << xhalf << std::endl; FIBITMAP *dst = FIA_CloneImageType(src, width, height); for(int y=0; y < yhalf; y++) { BYTE* srcbits = (BYTE *) FIA_GetScanLineFromTop(src, y); BYTE* outbits = (BYTE *) FIA_GetScanLineFromTop(dst, y + yhalf); memcpy(outbits + xhalf_bytes, srcbits, xhalf_bytes); memcpy(outbits, srcbits + xhalf_bytes, xhalf_bytes); } for(int y=yhalf; y < height; y++) { BYTE* srcbits = (BYTE *) FIA_GetScanLineFromTop(src, y); BYTE* outbits = (BYTE *) FIA_GetScanLineFromTop(dst, y - yhalf); memcpy(outbits + xhalf_bytes, srcbits, xhalf_bytes); memcpy(outbits, srcbits + xhalf_bytes, xhalf_bytes); } return dst; }
FIBITMAP *DLL_CALLCONV FIA_BinaryAnd (FIBITMAP *src1, FIBITMAP *src2, int Not) { if (src1 == NULL || src2 == NULL) return NULL; int width = FreeImage_GetWidth (src1); int height = FreeImage_GetHeight (src1); if (width != FreeImage_GetWidth (src2) || height != FreeImage_GetHeight (src2)) return NULL; // src has to be 8 bit if (FreeImage_GetBPP (src1) != 8 || FreeImage_GetImageType (src1) != FIT_BITMAP) return NULL; if (FreeImage_GetBPP (src2) != 8 || FreeImage_GetImageType (src2) != FIT_BITMAP) return NULL; FIBITMAP *dst = FIA_CloneImageType (src1, width, height); if (Not) { for(register int y = 0; y < height; y++) { unsigned char *src1_ptr = (unsigned char *) FreeImage_GetScanLine (src1, y); unsigned char *src2_ptr = (unsigned char *) FreeImage_GetScanLine (src2, y); unsigned char *dst_ptr = (unsigned char *) FreeImage_GetScanLine (dst, y); for(register int x = 0; x < width; x++) { dst_ptr[x] = !(src1_ptr[x] && src2_ptr[x]); } } } else { for(register int y = 0; y < height; y++) { unsigned char *src1_ptr = (unsigned char *) FreeImage_GetScanLine (src1, y); unsigned char *src2_ptr = (unsigned char *) FreeImage_GetScanLine (src2, y); unsigned char *dst_ptr = (unsigned char *) FreeImage_GetScanLine (dst, y); for(register int x = 0; x < width; x++) { dst_ptr[x] = src1_ptr[x] && src2_ptr[x]; } } } return dst; }
FIBITMAP *DLL_CALLCONV FIA_BinaryErosion (FIABITMAP * src, FilterKernel kernel) { const int dst_width = FreeImage_GetWidth (src->fib) - (2 * kernel.x_radius); const int dst_height = FreeImage_GetHeight (src->fib) - (2 * kernel.y_radius); FIBITMAP *dst = FIA_CloneImageType (src->fib, dst_width, dst_height); const int dst_pitch_in_pixels = FreeImage_GetPitch (dst) / sizeof (unsigned char); register unsigned char *dst_ptr; unsigned char *dst_first_pixel_address_ptr = (unsigned char *) FreeImage_GetBits (dst); int kernel_size = (kernel.x_radius * 2 + kernel.y_radius * 2) + 1; unsigned char *vals = new unsigned char[kernel_size]; for(int i = 0; i < kernel_size; i++) vals[i] = (unsigned char) kernel.values[i]; Kernel < unsigned char >*kern = new Kernel < unsigned char >(src, kernel.x_radius, kernel.y_radius, vals, 1.0); for(register int y = 0; y < dst_height; y++) { kern->Move (0, y); dst_ptr = (dst_first_pixel_address_ptr + y * dst_pitch_in_pixels); for(register int x = 0; x < dst_width; x++) { *dst_ptr = kern->KernelCenterValue (); // If White pixel check neigbours if (*dst_ptr > 0) BinaryErodeKernel (kern, dst_ptr); dst_ptr++; kern->Increment (); } } delete kern; return dst; };
FIABITMAP * BORDER < Tsrc >::SetBorder (FIBITMAP * src, int xborder, int yborder, BorderType type, Tsrc constant) { int width = FreeImage_GetWidth (src); int height = FreeImage_GetHeight (src); int dst_width = width + (2 * xborder); int dst_height = height + (2 * yborder); FIABITMAP *dst = (FIABITMAP *) malloc (sizeof (FIABITMAP)); CheckMemory (dst); dst->fib = FIA_CloneImageType (src, dst_width, dst_height); dst->xborder = xborder; dst->yborder = yborder; FIA_Paste (dst->fib, src, xborder, yborder); if (type == BorderType_Constant && constant != 0.0) { // Set the bottom line of the image to constant value this->SetImageRowToConstant (dst->fib, 0, yborder, constant); // Set the top line of the image to constant value this->SetImageRowToConstant (dst->fib, dst_height - yborder, yborder, constant); // Set the left pixels of the image this->SetImageColToConstant (dst->fib, 0, xborder, constant); // Set the right pixels of the image this->SetImageColToConstant (dst->fib, dst_width - xborder, xborder, constant); } else if (type == BorderType_Copy) { // Get the bottom line of the original image and copy into bottom border CopyImageRow (dst->fib, yborder, 0, yborder); // Get the top line of the original image and copy into top border CopyImageRow (dst->fib, dst_height - yborder - 1, dst_height - yborder, yborder); // Get the left pixels of the image and copy into left border CopyImageCol (dst->fib, xborder, 0, xborder); // Get the right pixels of the image and copy into right border CopyImageCol (dst->fib, dst_width - xborder - 1, dst_width - xborder, xborder); } else if (type == BorderType_Mirror) { int pitch = FreeImage_GetPitch (dst->fib); Tsrc *src_bits, *dst_bits; // Get the bottom line of the original image and copy into bottom border int border_row_start = yborder - 1; // First row on border for bottom int image_row_start = yborder; for(int i = 0; i < yborder; i++) { dst_bits = (Tsrc *) FreeImage_GetScanLine (dst->fib, border_row_start); src_bits = (Tsrc *) FreeImage_GetScanLine (dst->fib, image_row_start); memcpy (dst_bits, src_bits, pitch); border_row_start--; image_row_start++; } // Top border_row_start = dst_height - yborder; // First row on border fort bottom image_row_start = border_row_start - 1; for(int i = 0; i < yborder; i++) { dst_bits = (Tsrc *) FreeImage_GetScanLine (dst->fib, border_row_start); src_bits = (Tsrc *) FreeImage_GetScanLine (dst->fib, image_row_start); memcpy (dst_bits, src_bits, pitch); border_row_start++; image_row_start--; } // Left dst_bits = (Tsrc *) FreeImage_GetBits (dst->fib); for(int y = 0; y < dst_height; y++) { dst_bits = (Tsrc *) FreeImage_GetScanLine (dst->fib, y); for(int x = 0; x < xborder; x++) { dst_bits[x] = dst_bits[2 * xborder - 1 - x]; } } // Right dst_bits = (Tsrc *) FreeImage_GetBits (dst->fib); border_row_start = dst_width - xborder; for(int y = 0; y < dst_height; y++) { dst_bits = (Tsrc *) FreeImage_GetScanLine (dst->fib, y); for(int x = 0; x < xborder; x++) { dst_bits[border_row_start + x] = dst_bits[border_row_start - 1 - x]; } } } return dst; }