Skip to content

Commit

Permalink
Use unique_ptr for JPEG decompression buffer.
Browse files Browse the repository at this point in the history
Apply suggestions from code review.

Co-authored-by: Adam Johnson <AdamJohnso@gmail.com>
  • Loading branch information
Deledrius and Hoikas committed Mar 6, 2024
1 parent 39ac1ef commit b735289
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions core/Util/plJPEG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "plJPEG.h"
#include "Debug/plDebug.h"
#include "PRP/Surface/plMipmap.h"

#include <cstring>
#include <memory>

extern "C" {
#include <jerror.h>
Expand Down Expand Up @@ -281,27 +283,24 @@ plMipmap* plJPEG::DecompressJPEG(hsStream* S)
RAII_JSAMPROW<1> jbuffer(row_stride);

// Start with a reasonable size for the buffer
std::vector<uint8_t> buffer(ji.dinfo.output_width * ji.dinfo.output_height * ji.dinfo.out_color_components);
auto buffer_size = ji.dinfo.output_width * ji.dinfo.output_height * ji.dinfo.out_color_components;
auto buffer = std::make_unique<unsigned char[]>(buffer_size);

size_t offs = 0;
while (ji.dinfo.output_scanline < ji.dinfo.output_height) {
while (offs + out_stride > buffer.capacity()) {
buffer.reserve(buffer.capacity() + INPUT_BUF_SIZE);
}
jpeg_read_scanlines(&ji.dinfo, jbuffer.data, 1);
for (size_t x = 0; x < ji.dinfo.output_width; x++) {
memcpy(buffer.data() + offs + (x * 4),
memcpy(buffer.get() + offs + (x * 4),
jbuffer.data[0] + (x * ji.dinfo.out_color_components),
ji.dinfo.out_color_components);
}
offs += out_stride;
}

jpeg_finish_decompress(&ji.dinfo);
buffer.shrink_to_fit();

plMipmap* newMipmap = new plMipmap(ji.dinfo.output_width, ji.dinfo.output_height, 1, plMipmap::kUncompressed, plMipmap::kRGB8888);
newMipmap->setImageData(buffer.data(), buffer.size());
newMipmap->setImageData(buffer.get(), buffer_size);

return newMipmap;
}
Expand Down

0 comments on commit b735289

Please sign in to comment.