Skip to content

Commit b644d07

Browse files
committed
better wuffs
1 parent 5724a6c commit b644d07

File tree

3 files changed

+40
-71
lines changed

3 files changed

+40
-71
lines changed

ndll/MacArm64/lime-debug.ndll

1.56 KB
Binary file not shown.

project/lib/wuffs-files.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<compilerflag value="-DWUFFS_CONFIG__MODULE__ZLIB"/>
88
<compilerflag value="-DWUFFS_CONFIG__MODULE__PNG"/>
99
<compilerflag value="-DWUFFS_CONFIG__MODULE__DEFLATE"/>
10-
<section if="HXCPP_ARMV7 || HXCPP_ARMV7S || HXCPP_ARM64">
11-
<compilerflag value="-DWUFFS_BASE__CPU_ARCH__ARM_NEON"/>
12-
</section>
10+
<compilerflag value="-DWUFFS_CONFIG__MODULE__AUX__BASE"/>
11+
<compilerflag value="-DWUFFS_CONFIG__MODULE__AUX__IMAGE"/>
12+
<compilerflag value="-DWUFFS_CONFIG__MODULE__BASE"/>
1313

1414
<file name="${NATIVE_TOOLKIT_PATH}/wuffs/release/c/wuffs-v0.3.c" />
1515

project/src/graphics/format/PNG.cpp

Lines changed: 37 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -173,86 +173,55 @@ namespace lime {
173173

174174
return true;
175175
}
176-
177176
bool PNG::Decode(Resource* resource, ImageBuffer* imageBuffer, bool decodeData) {
178-
wuffs_png__decoder decoder;
179-
wuffs_base__image_config ic;
180-
wuffs_base__io_buffer io_buffer = {0};
181-
uint8_t* pixel_buffer = nullptr;
182-
uint8_t* workbuf = nullptr;
183-
size_t workbuf_len = 0;
184-
bool success = false;
185-
186-
187-
// Initialize the decoder
188-
wuffs_base__status status = wuffs_png__decoder__initialize(&decoder, sizeof(decoder), WUFFS_VERSION, 0);
189-
if (status.repr != NULL) {
190-
return false;
191-
}
192-
193-
// Read input data
194-
if (!ReadInput(resource, &io_buffer)) {
177+
class MyCallbacks : public wuffs_aux::DecodeImageCallbacks {
178+
public:
179+
wuffs_base__pixel_format SelectPixfmt(const wuffs_base__image_config& image_config) override {
180+
return wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL);
181+
}
182+
};
183+
184+
MyCallbacks callbacks;
185+
wuffs_aux::sync_io::MemoryInput input(resource->data->b, resource->data->length);
186+
187+
wuffs_aux::DecodeImageResult result = wuffs_aux::DecodeImage(
188+
callbacks,
189+
input,
190+
wuffs_aux::DecodeImageArgQuirks::DefaultValue(),
191+
wuffs_aux::DecodeImageArgFlags::DefaultValue(),
192+
wuffs_aux::DecodeImageArgPixelBlend::DefaultValue(),
193+
wuffs_aux::DecodeImageArgBackgroundColor::DefaultValue(),
194+
wuffs_aux::DecodeImageArgMaxInclDimension::DefaultValue(),
195+
wuffs_aux::DecodeImageArgMaxInclMetadataLength::DefaultValue()
196+
);
197+
198+
if (!result.error_message.empty()) {
199+
printf("Failed to decode image: %s\n", result.error_message.c_str());
195200
return false;
196201
}
197202

198-
// Decode the image configuration
199-
status = wuffs_png__decoder__decode_image_config(&decoder, &ic, &io_buffer);
200-
if (status.repr != NULL) {
201-
goto cleanup;
202-
}
203-
204-
// Set pixel format explicitly
205-
wuffs_base__pixel_config__set(
206-
&ic.pixcfg, WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL,
207-
WUFFS_BASE__PIXEL_SUBSAMPLING__NONE,
208-
wuffs_base__pixel_config__width(&ic.pixcfg),
209-
wuffs_base__pixel_config__height(&ic.pixcfg));
210-
211-
// Allocate buffers
212-
if (!AllocateBuffers(&decoder, &ic, &pixel_buffer, &workbuf, &workbuf_len)) {
213-
goto cleanup;
214-
}
215-
216203
// Update ImageBuffer dimensions
217-
imageBuffer->width = wuffs_base__pixel_config__width(&ic.pixcfg);
218-
imageBuffer->height = wuffs_base__pixel_config__height(&ic.pixcfg);
204+
imageBuffer->width = result.pixbuf.pixcfg.width();
205+
imageBuffer->height = result.pixbuf.pixcfg.height();
219206

220207
if (decodeData) {
221-
wuffs_base__pixel_buffer pb = {0};
222-
status = wuffs_base__pixel_buffer__set_from_slice(
223-
&pb, &ic.pixcfg,
224-
wuffs_base__make_slice_u8(pixel_buffer, imageBuffer->width * imageBuffer->height * 4));
225-
226-
if (status.repr != NULL) {
227-
printf("Failed to set up pixel buffer: %s\n", status.repr);
228-
goto cleanup;
229-
}
230-
231-
status = wuffs_png__decoder__decode_frame(
232-
&decoder, &pb, &io_buffer,
233-
WUFFS_BASE__PIXEL_BLEND__SRC,
234-
wuffs_base__make_slice_u8(workbuf, workbuf_len),
235-
NULL);
208+
// Resize ImageBuffer
209+
imageBuffer->Resize(imageBuffer->width, imageBuffer->height, 32);
236210

237-
if (status.repr != NULL) {
238-
printf("Failed to decode frame: %s\n", status.repr);
239-
goto cleanup;
211+
// Copy decoded data to ImageBuffer
212+
wuffs_base__table_u8 pixels = result.pixbuf.plane(0);
213+
size_t bytes_per_row = imageBuffer->width * 4; // 4 bytes per pixel for RGBA
214+
for (uint32_t y = 0; y < imageBuffer->height; ++y) {
215+
memcpy(imageBuffer->data->buffer->b + (y * bytes_per_row),
216+
pixels.ptr + (y * pixels.stride),
217+
bytes_per_row);
240218
}
241219

242-
// Resize and copy decoded data to imageBuffer
243-
imageBuffer->Resize(imageBuffer->width, imageBuffer->height, 32);
244-
memcpy(imageBuffer->data->buffer->b, pixel_buffer, imageBuffer->width * imageBuffer->height * 4);
220+
// No need for color correction if the format already matches your needs
245221
}
246222

247-
success = true;
248-
249-
cleanup:
250-
free(workbuf);
251-
free(pixel_buffer);
252-
free(io_buffer.data.ptr);
253-
254-
return success;
255-
}
223+
return true;
224+
}
256225

257226
bool PNG::Encode (ImageBuffer *imageBuffer, Bytes* bytes) {
258227

0 commit comments

Comments
 (0)