MCPcopy Create free account
hub / github.com/WolfireGames/overgrowth / SavePNGTransparent

Method SavePNGTransparent

Source/Images/image_export.cpp:131–162  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

129}
130
131void ImageExport::SavePNGTransparent(const char *file_path, unsigned char *data, unsigned long width, unsigned long height, unsigned short levels) {
132 std::vector<unsigned char> scaled_data;
133 ImageExport::ScaleImageUp(data, levels, &width, &height, &scaled_data);
134
135 int bytespp = 4;
136 for (unsigned y = 0; y < height; y++) {
137 unsigned char *bits = scaled_data.data() + y * width * 4;
138 for (unsigned x = 0; x < width; x++) {
139 // Set pixel color to green with a transparency of 128
140 bits[0] = min(255, (int)((bits[0] / 255.0f / (scaled_data[y * width * 4 + x * 4 + 3] / 255.0f)) * 255));
141 bits[1] = min(255, (int)((bits[1] / 255.0f / (scaled_data[y * width * 4 + x * 4 + 3] / 255.0f)) * 255));
142 bits[2] = min(255, (int)((bits[2] / 255.0f / (scaled_data[y * width * 4 + x * 4 + 3] / 255.0f)) * 255));
143 bits[3] = scaled_data[y * width * 4 + x * 4 + 3];
144 // jump to next pixel
145 bits += bytespp;
146 }
147 }
148
149 CreateParentDirs(file_path);
150#ifdef _WIN32
151 createfile(file_path);
152 std::string short_path(file_path);
153 ShortenWindowsPath(short_path);
154 if (!stbi_write_png(short_path.c_str(), width, height, 4, scaled_data.data(), width * 4)) {
155 DisplayError("Error", "Problem exporting .png file");
156 }
157#else
158 if (!stbi_write_png(file_path, width, height, 4, scaled_data.data(), width * 4)) {
159 DisplayError("Error", "Problem exporting .png file");
160 }
161#endif
162}
163
164void ImageExport::SaveJPEG(const char *abs_path, unsigned char *data, unsigned long width, unsigned long height) {
165 CreateParentDirs(abs_path);

Callers

nothing calls this directly

Calls 8

CreateParentDirsFunction · 0.85
createfileFunction · 0.85
ShortenWindowsPathFunction · 0.85
minFunction · 0.50
stbi_write_pngFunction · 0.50
DisplayErrorFunction · 0.50
dataMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected