MCPcopy Create free account
hub / github.com/PCSX2/pcsx2 / SaveFile

Function SaveFile

pcsx2/GS/GSPng.cpp:30–89  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28{
29
30 bool SaveFile(const std::string& file, const Format fmt, const u8* const image,
31 u8* const row, const int width, const int height, const int pitch,
32 const int compression, const bool rb_swapped = false, const bool first_image = false)
33 {
34 const int channel_bit_depth = pixel[fmt].channel_bit_depth;
35 const int bytes_per_pixel_in = pixel[fmt].bytes_per_pixel_in;
36
37 const int type = first_image ? pixel[fmt].type : PNG_COLOR_TYPE_GRAY;
38 const int offset = first_image ? 0 : pixel[fmt].bytes_per_pixel_out;
39 const int bytes_per_pixel_out = first_image ? pixel[fmt].bytes_per_pixel_out : bytes_per_pixel_in - offset;
40
41 auto fp = FileSystem::OpenManagedCFile(file.c_str(), "wb");
42 if (!fp)
43 return false;
44
45 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
46 png_infop info_ptr = nullptr;
47
48 if (png_ptr == nullptr)
49 return false;
50
51 info_ptr = png_create_info_struct(png_ptr);
52 if (info_ptr == nullptr)
53 return false;
54
55 if (setjmp(png_jmpbuf(png_ptr)))
56 return false;
57
58 png_set_write_fn(
59 png_ptr, fp.get(),
60 [](png_structp png_ptr, png_bytep data_ptr, png_size_t size) {
61 if (std::fwrite(data_ptr, size, 1, static_cast<std::FILE*>(png_get_io_ptr(png_ptr))) != 1)
62 png_error(png_ptr, "file write error");
63 },
64 [](png_structp png_ptr) {});
65
66 png_set_compression_level(png_ptr, compression);
67 png_set_IHDR(png_ptr, info_ptr, width, height, channel_bit_depth, type,
68 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
69 png_write_info(png_ptr, info_ptr);
70
71 if (channel_bit_depth > 8)
72 png_set_swap(png_ptr);
73 if (rb_swapped && type != PNG_COLOR_TYPE_GRAY)
74 png_set_bgr(png_ptr);
75
76 for (int y = 0; y < height; ++y)
77 {
78 for (int x = 0; x < width; ++x)
79 for (int i = 0; i < bytes_per_pixel_out; ++i)
80 row[bytes_per_pixel_out * x + i] = image[y * pitch + bytes_per_pixel_in * x + i + offset];
81 png_write_row(png_ptr, row);
82 }
83 png_write_end(png_ptr, nullptr);
84
85 if (png_ptr)
86 png_destroy_write_struct(&png_ptr, info_ptr ? &info_ptr : nullptr);
87

Callers 2

SaveFileMethod · 0.85
SaveFunction · 0.85

Calls 2

c_strMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected