| 225 | } |
| 226 | |
| 227 | bool IPLFileIO::saveFile(const std::string path, IPLImage* image, int format, int flags, IPLImage* result, bool preview) |
| 228 | { |
| 229 | int width = image->width(); |
| 230 | int height = image->height(); |
| 231 | |
| 232 | FIBITMAP *dib = FreeImage_Allocate(width, height, 24); |
| 233 | |
| 234 | if(image->type() == IPL_IMAGE_COLOR) |
| 235 | { |
| 236 | for(int y = 0; y < height; y++) |
| 237 | { |
| 238 | for(int x = 0; x < width; x++) |
| 239 | { |
| 240 | RGBQUAD rgb; |
| 241 | rgb.rgbRed = static_cast<BYTE>(image->plane(0)->p(x, y) * FACTOR_TO_UCHAR); // R |
| 242 | rgb.rgbGreen = static_cast<BYTE>(image->plane(1)->p(x, y) * FACTOR_TO_UCHAR); // G |
| 243 | rgb.rgbBlue = static_cast<BYTE>(image->plane(2)->p(x, y) * FACTOR_TO_UCHAR); // B |
| 244 | FreeImage_SetPixelColor(dib, x, y, &rgb); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | for(int y = 0; y < height; y++) |
| 251 | { |
| 252 | for(int x = 0; x < width; x++) |
| 253 | { |
| 254 | unsigned char value = image->plane(0)->p(x, y) * FACTOR_TO_UCHAR; |
| 255 | RGBQUAD rgb; |
| 256 | rgb.rgbRed = value; // R |
| 257 | rgb.rgbGreen = value; // G |
| 258 | rgb.rgbBlue = value; // B |
| 259 | FreeImage_SetPixelColor(dib, x, y, &rgb); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // all files need to be flipped |
| 265 | FreeImage_FlipVertical(dib); |
| 266 | |
| 267 | bool success = false; |
| 268 | |
| 269 | if(preview) |
| 270 | { |
| 271 | // only save to memory for preview |
| 272 | FIMEMORY* hmem = NULL; |
| 273 | // open and allocate a memory stream |
| 274 | hmem = FreeImage_OpenMemory(); |
| 275 | |
| 276 | success = FreeImage_SaveToMemory((FREE_IMAGE_FORMAT)format, dib, hmem, flags) != 0; |
| 277 | |
| 278 | // write to result |
| 279 | if(success && result) |
| 280 | { |
| 281 | loadMemory((void*)hmem, result); |
| 282 | } |
| 283 | |
| 284 | //FreeImage_CloseMemory(hmem); |