|
@@ -7,15 +7,18 @@
|
|
|
|
|
|
|
|
#include "stbImageWrapper.h"
|
|
#include "stbImageWrapper.h"
|
|
|
|
|
|
|
|
-using namespace dsr;
|
|
|
|
|
|
|
+namespace dsr {
|
|
|
|
|
|
|
|
-OrderedImageRgbaU8 dsr::image_stb_load_RgbaU8(const String& filename, bool mustExist) {
|
|
|
|
|
|
|
+OrderedImageRgbaU8 image_stb_decode_RgbaU8(const SafePointer<uint8_t> data, int size, bool mustParse) {
|
|
|
|
|
+ #ifdef SAFE_POINTER_CHECKS
|
|
|
|
|
+ // If the safe pointer has debug information, use it to assert that size is within bound.
|
|
|
|
|
+ target.assertInside("image_stb_decode_RgbaU8 (data)", data.getUnsafe(), (size_t)size);
|
|
|
|
|
+ #endif
|
|
|
int width, height, bpp;
|
|
int width, height, bpp;
|
|
|
- uint8_t *data = stbi_load(filename.toStdString().c_str(), &width, &height, &bpp, 4);
|
|
|
|
|
- if (data == 0) {
|
|
|
|
|
- if (mustExist) {
|
|
|
|
|
- // TODO: Throw an optional runtime exception
|
|
|
|
|
- printText("The image ", filename, " could not be loaded!\n");
|
|
|
|
|
|
|
+ uint8_t *rawPixelData = stbi_load_from_memory(data.getUnsafe(), size, &width, &height, &bpp, 4);
|
|
|
|
|
+ if (rawPixelData == nullptr) {
|
|
|
|
|
+ if (mustParse) {
|
|
|
|
|
+ throwError("An image could not be parsed!\n");
|
|
|
}
|
|
}
|
|
|
return OrderedImageRgbaU8(); // Return null
|
|
return OrderedImageRgbaU8(); // Return null
|
|
|
}
|
|
}
|
|
@@ -24,7 +27,7 @@ OrderedImageRgbaU8 dsr::image_stb_load_RgbaU8(const String& filename, bool mustE
|
|
|
// Copy the data
|
|
// Copy the data
|
|
|
int rowSize = width * 4;
|
|
int rowSize = width * 4;
|
|
|
int32_t targetStride = image_getStride(result);
|
|
int32_t targetStride = image_getStride(result);
|
|
|
- const uint8_t *sourceRow = data;
|
|
|
|
|
|
|
+ const uint8_t *sourceRow = rawPixelData;
|
|
|
uint8_t* targetRow = image_dangerous_getData(result);
|
|
uint8_t* targetRow = image_dangerous_getData(result);
|
|
|
for (int32_t y = 0; y < height; y++) {
|
|
for (int32_t y = 0; y < height; y++) {
|
|
|
// Copy a row without touching the padding
|
|
// Copy a row without touching the padding
|
|
@@ -34,12 +37,14 @@ OrderedImageRgbaU8 dsr::image_stb_load_RgbaU8(const String& filename, bool mustE
|
|
|
sourceRow += rowSize;
|
|
sourceRow += rowSize;
|
|
|
}
|
|
}
|
|
|
// Free the unpadded image
|
|
// Free the unpadded image
|
|
|
- free(data);
|
|
|
|
|
|
|
+ free((void*)rawPixelData);
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool dsr::image_stb_save(const ImageRgbaU8 &image, const String& filename) {
|
|
|
|
|
|
|
+bool image_stb_save(const ImageRgbaU8 &image, const String& filename) {
|
|
|
// Remove all padding before saving to avoid crashing
|
|
// Remove all padding before saving to avoid crashing
|
|
|
ImageRgbaU8 unpadded = ImageRgbaU8(image_removePadding(image));
|
|
ImageRgbaU8 unpadded = ImageRgbaU8(image_removePadding(image));
|
|
|
return stbi_write_png(filename.toStdString().c_str(), image_getWidth(unpadded), image_getHeight(unpadded), 4, image_dangerous_getData(unpadded), image_getStride(unpadded)) != 0;
|
|
return stbi_write_png(filename.toStdString().c_str(), image_getWidth(unpadded), image_getHeight(unpadded), 4, image_dangerous_getData(unpadded), image_getStride(unpadded)) != 0;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+}
|