Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions Source/Plugins/J2KHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,27 @@ opj_freeimage_stream_create(FreeImageIO *io, fi_handle handle, FIBOOL bRead) {
if (!handle) {
return nullptr;
}
auto *fio = (J2KFIO_t*)malloc(sizeof(J2KFIO_t));
auto fio{ std::make_unique<J2KFIO_t>() };
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

может быть тоже nothrow?

Copy link
Copy Markdown
Contributor Author

@lordnn lordnn May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Согласен. (6fa3c8d)

if (fio) {
fio->io = io;
fio->handle = handle;

opj_stream_t *l_stream = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE, bRead ? OPJ_TRUE : OPJ_FALSE);
if (l_stream) {
opj_stream_set_user_data(l_stream, fio, nullptr);
opj_stream_set_user_data_length(l_stream, _LengthProc(fio));
opj_stream_set_user_data(l_stream, fio.get(), nullptr);
opj_stream_set_user_data_length(l_stream, _LengthProc(fio.get()));
opj_stream_set_read_function(l_stream, (opj_stream_read_fn)_ReadProc);
opj_stream_set_write_function(l_stream, (opj_stream_write_fn)_WriteProc);
opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn)_SkipProc);
opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn)_SeekProc);
fio->stream = l_stream;
}
else {
free(fio);
fio = nullptr;
fio.reset();
}
}

return fio;
return fio.release();
}

void
Expand All @@ -103,7 +102,7 @@ opj_freeimage_stream_destroy(J2KFIO_t* fio) {
if (fio->stream) {
opj_stream_destroy(fio->stream);
}
free(fio);
delete fio;
}
}

Expand Down
7 changes: 4 additions & 3 deletions Source/Plugins/PluginDDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "FreeImage.h"
#include "Utilities.h"
#include "yato/types.h"

// ----------------------------------------------------------
// Definitions for the RGB 444 format
Expand Down Expand Up @@ -632,16 +633,16 @@ LoadRGB(const DDSURFACEDESC2 *desc, FreeImageIO *io, fi_handle handle) {
const long delta = (long)filePitch - (long)line;

if (bpp == 16) {
std::unique_ptr<void, decltype(&free)> safePixels(malloc(line * sizeof(uint8_t)), &free);
std::unique_ptr<uint8_t[]> safePixels(new(std::nothrow) uint8_t[line]);
if (safePixels) {
auto *pixels = static_cast<uint8_t*>(safePixels.get());
auto *pixels = safePixels.get();
for (int y = 0; y < height; y++) {
uint8_t *dst_bits = FreeImage_GetScanLine(dib.get(), height - y - 1);
// get the 16-bit RGB pixels
io->read_proc(pixels, 1, line, handle);
io->seek_proc(handle, delta, SEEK_CUR);
// convert to 24-bit
ConvertLine16To24(dst_bits, (const uint16_t*)pixels, format16, width);
ConvertLine16To24(dst_bits, yato::pointer_cast<const uint16_t*>(pixels), format16, width);
}
}
}
Expand Down
18 changes: 8 additions & 10 deletions Source/Plugins/PluginHDR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,15 @@ rgbe_WritePixels(FreeImageIO *io, fi_handle handle, FIRGBF *data, unsigned numpi

static FIBOOL
rgbe_ReadPixels_RLE(FreeImageIO *io, fi_handle handle, FIRGBF *data, int scanline_width, unsigned num_scanlines) {
uint8_t rgbe[4], *scanline_buffer{}, *ptr, *ptr_end;
uint8_t rgbe[4];
int i, count;
uint8_t buf[2];

if ((scanline_width < 8)||(scanline_width > 0x7fff)) {
// run length encoding is not allowed so read flat
return rgbe_ReadPixels(io, handle, data, scanline_width * num_scanlines);
}
std::unique_ptr<void, decltype(&free)> safeScanlineBuffer(nullptr, &free);
std::unique_ptr<uint8_t[]> scanline_buffer;
// read in each successive scanline
while (num_scanlines > 0) {
if (io->read_proc(rgbe, 1, sizeof(rgbe), handle) < 1) {
Expand All @@ -387,17 +387,16 @@ rgbe_ReadPixels_RLE(FreeImageIO *io, fi_handle handle, FIRGBF *data, int scanlin
return rgbe_Error(rgbe_format_error,"wrong scanline width");
}
if (!scanline_buffer) {
safeScanlineBuffer.reset(malloc(sizeof(uint8_t) * 4 * scanline_width));
if (!safeScanlineBuffer) {
scanline_buffer.reset(new(std::nothrow) uint8_t[4 * scanline_width]);
if (!scanline_buffer) {
return rgbe_Error(rgbe_memory_error, "unable to allocate buffer space");
}
scanline_buffer = static_cast<uint8_t*>(safeScanlineBuffer.get());
}

ptr = &scanline_buffer[0];
uint8_t *ptr{ scanline_buffer.get() };
// read each of the four channels for the scanline into the buffer
for (i = 0; i < 4; i++) {
ptr_end = &scanline_buffer[(i+1)*scanline_width];
uint8_t *ptr_end{ &scanline_buffer[(i+1)*scanline_width] };
while (ptr < ptr_end) {
if (io->read_proc(buf, 1, 2 * sizeof(uint8_t), handle) < 1) {
return rgbe_Error(rgbe_read_error, nullptr);
Expand Down Expand Up @@ -516,12 +515,11 @@ rgbe_WritePixels_RLE(FreeImageIO *io, fi_handle handle, FIRGBF *data, unsigned s
// run length encoding is not allowed so write flat
return rgbe_WritePixels(io, handle, data, scanline_width * num_scanlines);
}
std::unique_ptr<void, decltype(&free)> safeBuffer(malloc(sizeof(uint8_t) * 4 * scanline_width), &free);;
if (!safeBuffer) {
std::unique_ptr<uint8_t[]> buffer(new(std::nothrow) uint8_t[4 * scanline_width]);;
if (!buffer) {
// no buffer space so write flat
return rgbe_WritePixels(io, handle, data, scanline_width * num_scanlines);
}
auto *buffer = static_cast<uint8_t*>(safeBuffer.get());

while (num_scanlines-- > 0) {
rgbe[0] = (uint8_t)2;
Expand Down
59 changes: 22 additions & 37 deletions Source/Plugins/PluginICO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,20 @@ SupportsNoPixels() {
static void * DLL_CALLCONV
Open(FreeImageIO *io, fi_handle handle, FIBOOL read) {
// Allocate memory for the header structure
auto *lpIH = (ICONHEADER*)malloc(sizeof(ICONHEADER));
std::unique_ptr<ICONHEADER, decltype(&free)> lpIH(static_cast<ICONHEADER*>(malloc(sizeof(ICONHEADER))), &free);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а почему malloc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Потому что этот указатель возвращается из функции и надо тогда искать править в месте удаления.

Copy link
Copy Markdown
Contributor Author

@lordnn lordnn May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Но, попробовать можно... (e0dd6eb)

if (!lpIH) {
return nullptr;
}

if (read) {
// Read in the header
io->read_proc(lpIH, 1, sizeof(ICONHEADER), handle);
io->read_proc(lpIH.get(), 1, sizeof(ICONHEADER), handle);
#ifdef FREEIMAGE_BIGENDIAN
SwapIconHeader(lpIH);
SwapIconHeader(lpIH.get());
#endif

if (!(lpIH->idReserved == 0) || !(lpIH->idType == 1)) {
// Not an ICO file
free(lpIH);
return nullptr;
}
}
Expand All @@ -260,7 +259,7 @@ Open(FreeImageIO *io, fi_handle handle, FIBOOL read) {
lpIH->idCount = 0;
}

return lpIH;
return lpIH.release();
}

static void DLL_CALLCONV
Expand Down Expand Up @@ -368,16 +367,15 @@ LoadStandardIcon(FreeImageIO *io, fi_handle handle, int flags, FIBOOL header_onl
}

int width_and = WidthBytes(width);
std::unique_ptr<void, decltype(&free)>safeLine(malloc(width_and), &free);
if (!safeLine) {
std::unique_ptr<uint8_t[]>line_and(new(std::nothrow) uint8_t[width_and]);
if (!line_and) {
return nullptr;
}
auto *line_and = static_cast<uint8_t*>(safeLine.get());

//loop through each line of the AND-mask generating the alpha channel, invert XOR-mask
for (int y = 0; y < height; y++) {
FIRGBA8 *quad = (FIRGBA8 *)FreeImage_GetScanLine(dib.get(), y);
io->read_proc(line_and, width_and, 1, handle);
io->read_proc(line_and.get(), width_and, 1, handle);
for (int x = 0; x < width; x++) {
quad->alpha = (line_and[x>>3] & (0x80 >> (x & 0x07))) != 0 ? 0 : 0xFF;
if ( quad->alpha == 0 ) {
Expand Down Expand Up @@ -407,15 +405,14 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {

if (icon_header) {
// load the icon descriptions
std::unique_ptr<void, decltype(&free)> safeList(malloc(icon_header->idCount * sizeof(ICONDIRENTRY)), &free);
if (!safeList) {
std::unique_ptr<ICONDIRENTRY[]> icon_list(new(std::nothrow) ICONDIRENTRY[icon_header->idCount]);
if (!icon_list) {
return nullptr;
}
auto *icon_list = static_cast<ICONDIRENTRY*>(safeList.get());
io->seek_proc(handle, sizeof(ICONHEADER), SEEK_SET);
io->read_proc(icon_list, icon_header->idCount * sizeof(ICONDIRENTRY), 1, handle);
io->read_proc(icon_list.get(), icon_header->idCount * sizeof(ICONDIRENTRY), 1, handle);
#ifdef FREEIMAGE_BIGENDIAN
SwapIconDirEntries(icon_list, icon_header->idCount);
SwapIconDirEntries(icon_list.get(), icon_header->idCount);
#endif

// load the requested icon
Expand Down Expand Up @@ -540,23 +537,19 @@ SaveStandardIcon(FreeImageIO *io, FIBITMAP *dib, fi_handle handle) {
#if defined(FREEIMAGE_BIGENDIAN) || FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_RGB
}
#endif
// AND mask
std::unique_ptr<void, decltype(&free)>safeMask(malloc(size_and), &free);
if (!safeMask) {
// empty AND mask
std::unique_ptr<uint8_t[]>and_mask(new(std::nothrow) uint8_t[size_and]());
if (!and_mask) {
return FALSE;
}
auto *and_mask = static_cast<uint8_t*>(safeMask.get());

if (FreeImage_IsTransparent(dib)) {

if (bit_count == 32) {
// create the AND mask from the alpha channel

int width_and = WidthBytes(width);
uint8_t *and_bits = and_mask;

// clear the mask
memset(and_mask, 0, size_and);
uint8_t *and_bits = and_mask.get();

for (int y = 0; y < height; y++) {
FIRGBA8 *bits = (FIRGBA8*)FreeImage_GetScanLine(dib, y);
Expand All @@ -577,10 +570,7 @@ SaveStandardIcon(FreeImageIO *io, FIBITMAP *dib, fi_handle handle) {
uint8_t *trns = FreeImage_GetTransparencyTable(dib);

int width_and = WidthBytes(width);
uint8_t *and_bits = and_mask;

// clear the mask
memset(and_mask, 0, size_and);
uint8_t *and_bits = and_mask.get();

switch (FreeImage_GetBPP(dib)) {
case 1:
Expand Down Expand Up @@ -638,12 +628,8 @@ SaveStandardIcon(FreeImageIO *io, FIBITMAP *dib, fi_handle handle) {
}
}
}
else {
// empty AND mask
memset(and_mask, 0, size_and);
}

io->write_proc(and_mask, size_and, 1, handle);
io->write_proc(and_mask.get(), size_and, 1, handle);

return TRUE;
}
Expand Down Expand Up @@ -705,11 +691,10 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void

// save the icon descriptions

std::unique_ptr<void, decltype(&free)> safeList(calloc(icon_header->idCount, sizeof(ICONDIRENTRY)), &free);
if (!safeList) {
std::unique_ptr<ICONDIRENTRY[]> icon_list(new(std::nothrow) ICONDIRENTRY[icon_header->idCount]);
if (!icon_list) {
throw FI_MSG_ERROR_MEMORY;
}
auto *icon_list = static_cast<ICONDIRENTRY*>(safeList.get());

for (k = 0; k < icon_header->idCount; k++) {
icon_dib = vPages[k].get();
Expand All @@ -734,7 +719,7 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void

// make a room for icon dir entries, until later update
const long directory_start = io->tell_proc(handle);
io->write_proc(icon_list, sizeof(ICONDIRENTRY) * icon_header->idCount, 1, handle);
io->write_proc(icon_list.get(), sizeof(ICONDIRENTRY) * icon_header->idCount, 1, handle);

// write the image bits for each image

Expand Down Expand Up @@ -764,9 +749,9 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
const long current_pos = io->tell_proc(handle);
io->seek_proc(handle, directory_start, SEEK_SET);
#ifdef FREEIMAGE_BIGENDIAN
SwapIconDirEntries(icon_list, icon_header->idCount);
SwapIconDirEntries(icon_list.get(), icon_header->idCount);
#endif
io->write_proc(icon_list, sizeof(ICONDIRENTRY) * icon_header->idCount, 1, handle);
io->write_proc(icon_list.get(), sizeof(ICONDIRENTRY) * icon_header->idCount, 1, handle);
io->seek_proc(handle, current_pos, SEEK_SET);

return TRUE;
Expand Down
4 changes: 2 additions & 2 deletions Source/Plugins/PluginPCX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,11 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {

if (palette_id == 0x0C) {

if (std::unique_ptr<void, decltype(&free)> cmap(malloc(768 * sizeof(uint8_t)), &free); cmap) {
if (std::unique_ptr<uint8_t[]> cmap(new(std::nothrow) uint8_t[768]); cmap) {
io->read_proc(cmap.get(), 768, 1, handle);

pal = FreeImage_GetPalette(dib.get());
auto *pColormap = static_cast<const uint8_t *>(cmap.get());
const auto *pColormap = cmap.get();

for (int i = 0; i < 256; i++) {
pal[i].red = pColormap[0];
Expand Down
4 changes: 2 additions & 2 deletions Source/Plugins/PluginPICT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ Unpack32Bits( FreeImageIO *io, fi_handle handle, FIBITMAP* dib, MacRect* bounds,
}

// Let's allocate enough for 4 bit planes
if (std::unique_ptr<void, decltype(&free)> pLineBuf(malloc(rowBytes), &free); pLineBuf) {
if (std::unique_ptr<uint8_t[]> pLineBuf(new(std::nothrow) uint8_t[rowBytes]); pLineBuf) {
for ( int i = 0; i < height; i++ ) {
// for each line do...
int linelen; // length of source line in bytes.
Expand All @@ -604,7 +604,7 @@ Unpack32Bits( FreeImageIO *io, fi_handle handle, FIBITMAP* dib, MacRect* bounds,
linelen = Read8( io, handle);
}

uint8_t* pBuf = UnpackPictRow( io, handle, static_cast<uint8_t *>(pLineBuf.get()), width, rowBytes, linelen );
uint8_t* pBuf = UnpackPictRow( io, handle, pLineBuf.get(), width, rowBytes, linelen );

// Convert plane-oriented data into pixel-oriented data &
// copy into destination bitmap.
Expand Down
10 changes: 4 additions & 6 deletions Source/Plugins/PluginPNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,10 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {

// set the individual row_pointers to point at the correct offsets

std::unique_ptr<void, decltype(&free)> safeRowPointers(malloc(height * sizeof(png_bytep)), &free);
if (!safeRowPointers) {
std::unique_ptr<png_bytep[]> row_pointers(new(std::nothrow) png_bytep[height]);
if (!row_pointers) {
return nullptr;
}
auto **row_pointers = static_cast<png_bytepp>(safeRowPointers.get());

// read in the bitmap bits via the pointer table
// allow loading of PNG with minor errors (such as images with several IDAT chunks)
Expand All @@ -730,7 +729,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
}

png_set_benign_errors(png_ptr.get(), 1);
png_read_image(png_ptr.get(), row_pointers);
png_read_image(png_ptr.get(), row_pointers.get());

// check if the bitmap contains transparency, if so enable it in the header

Expand All @@ -740,8 +739,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {

// cleanup

safeRowPointers.reset();
row_pointers = nullptr;
row_pointers.reset();

// read the rest of the file, getting any additional chunks in info_ptr

Expand Down
8 changes: 4 additions & 4 deletions Source/Plugins/PluginTARGA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static const char *FI_MSG_ERROR_CORRUPTED = "Image data corrupted";
class TargaThumbnail
{
public:
TargaThumbnail() : _data(nullptr, &free), _w(0), _h(0), _depth(0) {
TargaThumbnail() : _w(0), _h(0), _depth(0) {
}

FIBOOL isNull() const {
Expand All @@ -119,7 +119,7 @@ class TargaThumbnail
io->read_proc(&_h, 1, 1, handle);

const size_t sizeofData = size - 2;
_data.reset(malloc(sizeofData));
_data.reset(new(std::nothrow) uint8_t[sizeofData]);
if (_data) {
return (io->read_proc(_data.get(), 1, (unsigned)sizeofData, handle) == sizeofData);
}
Expand All @@ -133,7 +133,7 @@ class TargaThumbnail
FIBITMAP* toFIBITMAP();

private:
std::unique_ptr<void, decltype(&free)> _data;
std::unique_ptr<uint8_t[]> _data;
uint8_t _w;
uint8_t _h;
uint8_t _depth;
Expand Down Expand Up @@ -175,7 +175,7 @@ FIBITMAP* TargaThumbnail::toFIBITMAP() {
return nullptr;
}

auto *line = static_cast<const uint8_t *>(_data.get());
auto *line = _data.get();
const uint8_t height = _h;
for (uint8_t h = 0; h < height; ++h, line += line_size) {
uint8_t* dst_line = FreeImage_GetScanLine(dib, height - 1 - h);
Expand Down
Loading