///////////////////////////////////////////////// Code to write and read images from memory. /////////////////////////////////////////////////
| 200 | // Code to write and read images from memory. |
| 201 | ////////////////////////////////////////////////////// |
| 202 | size_t writeImageToMemory(u8* output, u32 srcw, u32 srch, u32 dstw, |
| 203 | u32 dsth, const u32* pixelData) |
| 204 | { |
| 205 | size_t written; |
| 206 | int ret; |
| 207 | |
| 208 | SDL_Surface* surf = SDL_CreateRGBSurfaceFrom((void *)pixelData, srcw, srch, 32, srcw * sizeof(u32), |
| 209 | 0xFF, 0xFF00, 0xFF0000, 0xFF000000); |
| 210 | if (!surf) |
| 211 | return 0; |
| 212 | if ((srcw != dstw) || (srch != dsth)) |
| 213 | { |
| 214 | const SDL_Rect rs = { 0, 0, (int)srcw, (int)srch }; |
| 215 | const SDL_Rect rd = { 0, 0, (int)dstw, (int)dsth }; |
| 216 | SDL_Surface* scaled = SDL_CreateRGBSurface(0, dstw, dsth, 32, |
| 217 | 0xFF, 0xFF00, 0xFF0000, 0xFF000000); |
| 218 | if (!scaled) |
| 219 | { |
| 220 | SDL_FreeSurface(surf); |
| 221 | return 0; |
| 222 | } |
| 223 | ret = SDL_SoftStretch(surf, &rs, scaled, &rd); |
| 224 | if (ret != 0) |
| 225 | { |
| 226 | SDL_FreeSurface(surf); |
| 227 | SDL_FreeSurface(scaled); |
| 228 | return 0; |
| 229 | } |
| 230 | SDL_FreeSurface(surf); |
| 231 | surf = scaled; |
| 232 | srcw = dstw; |
| 233 | srch = dsth; |
| 234 | } |
| 235 | |
| 236 | SDL_RWops* memops = SDL_RWFromMem(output, srcw * srch * sizeof(u32)); |
| 237 | if (!memops) |
| 238 | { |
| 239 | SDL_FreeSurface(surf); |
| 240 | return 0; |
| 241 | } |
| 242 | memops->write = _sdl_wop_mem; |
| 243 | |
| 244 | // I'm not a fan of this, but a few people on Linux have run into crashes executing this function. |
| 245 | // Given it is from a third-party library, there is only so much I can do. |
| 246 | bool imageSaveSuccess = true; |
| 247 | try |
| 248 | { |
| 249 | ret = IMG_SavePNG_RW(surf, memops, 0); |
| 250 | } |
| 251 | catch (std::exception& ex) |
| 252 | { |
| 253 | written = 0; |
| 254 | imageSaveSuccess = false; |
| 255 | TFE_System::logWrite(LOG_ERROR, "Serialization", "Failed to generate a PNG for the save file, IMG_SavePNG_RW failed with exception '%s'", ex.what()); |
| 256 | }; |
| 257 | |
| 258 | SDL_FreeSurface(surf); |
| 259 | if (ret == 0 && imageSaveSuccess) |
no test coverage detected