MCPcopy Create free account
hub / github.com/OpenApoc/OpenApoc / loadImage

Method loadImage

framework/imageloader/lodepng_image.cpp:80–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78 }
79
80 sp<Image> loadImage(IFile &file) override
81 {
82 auto data = file.readAll();
83 auto dataSize = file.size();
84 unsigned int width, height;
85 lodepng::State png_state;
86
87 unsigned int err = lodepng_inspect(&width, &height, &png_state,
88 reinterpret_cast<unsigned char *>(data.get()), dataSize);
89 if (err)
90 {
91 LogInfo("Failed to read PNG headers from \"%s\" (%u) : %s", file.systemPath(), err,
92 lodepng_error_text(err));
93 return nullptr;
94 }
95
96 LogInfo("Loading PNG \"%s\" size {%u,%d} - colour mode %d depth %u", file.systemPath(),
97 width, height, png_state.info_png.color.colortype,
98 png_state.info_png.color.bitdepth);
99
100 // Just convert to RGBA, as PNG palettes are often reordered/trimmed at every turn by any
101 // app modifying them
102
103 std::vector<unsigned char> image;
104 unsigned int error = lodepng::decode(
105 image, width, height, reinterpret_cast<unsigned char *>(data.get()), file.size());
106 if (error)
107 {
108 LogInfo("LodePNG error code: %d: %s", error, lodepng_error_text(error));
109 }
110 if (!image.size())
111 {
112 LogInfo("Failed to load image %s (not a PNG?)", file.systemPath());
113 return nullptr;
114 }
115 OpenApoc::Vec2<int> size(width, height);
116 auto img = mksp<OpenApoc::RGBImage>(size);
117 OpenApoc::RGBImageLock dst(img, OpenApoc::ImageLockUse::Write);
118
119 for (int y = 0; y < size.y; y++)
120 {
121 for (int x = 0; x < size.x; x++)
122 {
123 OpenApoc::Colour c(
124 image[4 * size.x * y + 4 * x + 0], image[4 * size.x * y + 4 * x + 1],
125 image[4 * size.x * y + 4 * x + 2], image[4 * size.x * y + 4 * x + 3]);
126 dst.set(OpenApoc::Vec2<int>(x, y), c);
127 }
128 }
129
130 return img;
131 }
132
133 UString getName() override { return "lodepng"; }
134};

Callers

nothing calls this directly

Calls 4

readAllMethod · 0.80
sizeMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected