| 2536 | #endif |
| 2537 | |
| 2538 | bool Image::saveImageToPNG(std::string_view filePath, bool isToRGB) |
| 2539 | { |
| 2540 | #if AX_USE_WIC |
| 2541 | return encodeWithWIC(filePath, isToRGB, GUID_ContainerFormatPng); |
| 2542 | #elif AX_USE_PNG |
| 2543 | bool ret = false; |
| 2544 | do |
| 2545 | { |
| 2546 | png_structp png_ptr; |
| 2547 | png_infop info_ptr; |
| 2548 | png_bytep* row_pointers; |
| 2549 | |
| 2550 | auto outStream = FileUtils::getInstance()->openFileStream(filePath, IFileStream::Mode::WRITE); |
| 2551 | |
| 2552 | AX_BREAK_IF(nullptr == outStream); |
| 2553 | |
| 2554 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 2555 | |
| 2556 | if (nullptr == png_ptr) |
| 2557 | { |
| 2558 | outStream.reset(); |
| 2559 | break; |
| 2560 | } |
| 2561 | |
| 2562 | info_ptr = png_create_info_struct(png_ptr); |
| 2563 | if (nullptr == info_ptr) |
| 2564 | { |
| 2565 | outStream.reset(); |
| 2566 | png_destroy_write_struct(&png_ptr, nullptr); |
| 2567 | break; |
| 2568 | } |
| 2569 | if (setjmp(png_jmpbuf(png_ptr))) |
| 2570 | { |
| 2571 | outStream.reset(); |
| 2572 | png_destroy_write_struct(&png_ptr, &info_ptr); |
| 2573 | break; |
| 2574 | } |
| 2575 | |
| 2576 | // png_init_io(png_ptr, outStream); |
| 2577 | png_set_write_fn(png_ptr, outStream.get(), pngWriteCallback, nullptr); |
| 2578 | |
| 2579 | if (!isToRGB && hasAlpha()) |
| 2580 | { |
| 2581 | png_set_IHDR(png_ptr, info_ptr, _width, _height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, |
| 2582 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 2583 | } |
| 2584 | else |
| 2585 | { |
| 2586 | png_set_IHDR(png_ptr, info_ptr, _width, _height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, |
| 2587 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 2588 | } |
| 2589 | |
| 2590 | png_write_info(png_ptr, info_ptr); |
| 2591 | |
| 2592 | png_set_packing(png_ptr); |
| 2593 | |
| 2594 | row_pointers = (png_bytep*)malloc(_height * sizeof(png_bytep)); |
| 2595 | if (row_pointers == nullptr) |
nothing calls this directly
no test coverage detected