| 338 | } |
| 339 | |
| 340 | void window_impl::saveFrameBuffer(const char* pFullPath) |
| 341 | { |
| 342 | #ifdef USE_FREEIMAGE |
| 343 | FI_Init(); |
| 344 | |
| 345 | auto FIErrorHandler = [](FREE_IMAGE_FORMAT pOutputFIFormat, const char* pMessage) { |
| 346 | printf("FreeImage Error Handler: %s\n", pMessage); |
| 347 | }; |
| 348 | |
| 349 | FreeImage_SetOutputMessage(FIErrorHandler); |
| 350 | |
| 351 | FREE_IMAGE_FORMAT format = FreeImage_GetFileType(pFullPath); |
| 352 | if (format == FIF_UNKNOWN) { |
| 353 | format = FreeImage_GetFIFFromFilename(pFullPath); |
| 354 | } |
| 355 | if (format == FIF_UNKNOWN) { |
| 356 | FG_ERROR("Freeimage: unrecognized image format", FG_ERR_FREEIMAGE_UNKNOWN_FORMAT); |
| 357 | } |
| 358 | |
| 359 | if (!(format==FIF_BMP || format==FIF_PNG)) { |
| 360 | FG_ERROR("Supports only bmp and png as of now", FG_ERR_FREEIMAGE_SAVE_FAILED); |
| 361 | } |
| 362 | |
| 363 | uint w = mWindow->mWidth; |
| 364 | uint h = mWindow->mHeight; |
| 365 | uint c = 4; |
| 366 | uint d = c * 8; |
| 367 | |
| 368 | FIBITMAP* bmp = FreeImage_Allocate(w, h, d); |
| 369 | if (!bmp) { |
| 370 | FG_ERROR("Freeimage: allocation failed", FG_ERR_FREEIMAGE_BAD_ALLOC); |
| 371 | } |
| 372 | |
| 373 | FI_BitmapResource bmpUnloader(bmp); |
| 374 | |
| 375 | uint pitch = FreeImage_GetPitch(bmp); |
| 376 | uchar* dst = FreeImage_GetBits(bmp); |
| 377 | |
| 378 | /* as glReadPixels was called using PBO earlier, hopefully |
| 379 | * it was async call(which it should be unless vendor driver |
| 380 | * is doing something fishy) and the transfer is over by now |
| 381 | * */ |
| 382 | glBindBuffer(GL_PIXEL_PACK_BUFFER, mWindow->mFramePBO); |
| 383 | |
| 384 | uchar* src = (uchar*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY); |
| 385 | |
| 386 | if (src) { |
| 387 | // copy data from mapped memory location |
| 388 | uint w = mWindow->mWidth; |
| 389 | uint h = mWindow->mHeight; |
| 390 | uint i = 0; |
| 391 | |
| 392 | for (uint y = 0; y < h; ++y) { |
| 393 | for (uint x = 0; x < w; ++x) { |
| 394 | *(dst + x * c + FI_RGBA_RED ) = (uchar) src[4*i+0]; // r |
| 395 | *(dst + x * c + FI_RGBA_GREEN) = (uchar) src[4*i+1]; // g |
| 396 | *(dst + x * c + FI_RGBA_BLUE ) = (uchar) src[4*i+2]; // b |
| 397 | *(dst + x * c + FI_RGBA_ALPHA) = (uchar) src[4*i+3]; // a |