Skip to content

Commit 7b2f76d

Browse files
committed
PSD: Fix thumbnail extraction for PSD files (AcademySoftwareFoundation#3668)
Fixes AcademySoftwareFoundation#3660 In investigating this issue, I also noticed that now that IOProxy support can be relied on, the old code that in the psd reader that directly fumbles with libjpeg is not needed and can be replaced by just a couple of lines to make an ImageBuf read from an IOProxy. And it fixes the bug, too. So I'm not even going to try to figure out what was going wrong with the old code.
1 parent e66ff05 commit 7b2f76d

File tree

5 files changed

+5
-175
lines changed

5 files changed

+5
-175
lines changed

src/jpeg.imageio/jpeg_pvt.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class JpgInput final : public ImageInput {
8686
void jpegerror(my_error_ptr myerr, bool fatal = false);
8787

8888
private:
89-
FILE* m_fd;
9089
std::string m_filename;
9190
int m_next_scanline; // Which scanline is the next to read?
9291
bool m_raw; // Read raw coefficients, not scanlines
@@ -101,7 +100,6 @@ class JpgInput final : public ImageInput {
101100

102101
void init()
103102
{
104-
m_fd = NULL;
105103
m_raw = false;
106104
m_cmyk = false;
107105
m_fatalerr = false;

src/psd.imageio/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33
# https://github.com/OpenImageIO/oiio
44

5-
add_oiio_plugin (psdinput.cpp jpeg_memory_src.cpp
6-
INCLUDE_DIRS ${JPEG_INCLUDE_DIR}
7-
LINK_LIBRARIES JPEG::JPEG)
5+
add_oiio_plugin (psdinput.cpp)
86

src/psd.imageio/jpeg_memory_src.cpp

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/psd.imageio/jpeg_memory_src.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/psd.imageio/psdinput.cpp

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <OpenImageIO/imagebufalgo.h>
2121
#include <OpenImageIO/tiffutils.h>
2222

23-
#include "jpeg_memory_src.h"
23+
// #include "jpeg_memory_src.h"
2424
#include "psd_pvt.h"
2525

2626
OIIO_PLUGIN_NAMESPACE_BEGIN
@@ -265,13 +265,6 @@ class PSDInput final : public ImageInput {
265265

266266
//Load thumbnail resource, used for resources 1033 and 1036
267267
bool load_resource_thumbnail(uint32_t length, bool isBGR);
268-
//For thumbnail loading
269-
struct thumbnail_error_mgr {
270-
jpeg_error_mgr pub;
271-
jmp_buf setjmp_buffer;
272-
};
273-
METHODDEF(void)
274-
thumbnail_error_exit(j_common_ptr cinfo);
275268

276269
//Layers
277270
bool load_layers();
@@ -1270,9 +1263,6 @@ PSDInput::load_resource_thumbnail(uint32_t length, bool isBGR)
12701263
uint32_t compressed_size;
12711264
uint16_t bpp;
12721265
uint16_t planes;
1273-
int stride;
1274-
jpeg_decompress_struct cinfo;
1275-
thumbnail_error_mgr jerr;
12761266
uint32_t jpeg_length = length - 28;
12771267

12781268
bool ok = read_bige<uint32_t>(format) && read_bige<uint32_t>(width)
@@ -1312,40 +1302,13 @@ PSDInput::load_resource_thumbnail(uint32_t length, bool isBGR)
13121302
return false;
13131303
}
13141304

1315-
cinfo.err = jpeg_std_error(&jerr.pub);
1316-
jerr.pub.error_exit = thumbnail_error_exit;
1317-
if (setjmp(jerr.setjmp_buffer)) {
1318-
jpeg_destroy_decompress(&cinfo);
1319-
errorfmt("[Image Resource] [JPEG Thumbnail] libjpeg error");
1320-
return false;
1321-
}
13221305
std::string jpeg_data(jpeg_length, '\0');
13231306
if (!ioread(&jpeg_data[0], jpeg_length))
13241307
return false;
13251308

1326-
jpeg_create_decompress(&cinfo);
1327-
jpeg_memory_src(&cinfo, (unsigned char*)&jpeg_data[0], jpeg_length);
1328-
jpeg_read_header(&cinfo, TRUE);
1329-
jpeg_start_decompress(&cinfo);
1330-
stride = cinfo.output_width * cinfo.output_components;
1331-
ImageSpec thumbspec(cinfo.output_width, cinfo.output_height, 3, TypeUInt8);
1332-
m_thumbnail.reset(thumbspec);
1333-
// jpeg_destroy_decompress will deallocate this
1334-
JSAMPLE** buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo,
1335-
JPOOL_IMAGE, stride, 1);
1336-
while (cinfo.output_scanline < cinfo.output_height) {
1337-
if (jpeg_read_scanlines(&cinfo, buffer, 1) != 1) {
1338-
jpeg_finish_decompress(&cinfo);
1339-
jpeg_destroy_decompress(&cinfo);
1340-
errorfmt("[Image Resource] [JPEG Thumbnail] libjpeg error");
1341-
return false;
1342-
}
1343-
m_thumbnail.get_pixels(ROI(0, width, cinfo.output_scanline,
1344-
cinfo.output_scanline + 1, 0, 1, 0, 3),
1345-
TypeUInt8, buffer[0]);
1346-
}
1347-
jpeg_finish_decompress(&cinfo);
1348-
jpeg_destroy_decompress(&cinfo);
1309+
Filesystem::IOMemReader thumbblob(jpeg_data.data(), jpeg_length);
1310+
m_thumbnail = ImageBuf("thumbnail.jpg", 0, 0, nullptr, nullptr, &thumbblob);
1311+
m_thumbnail.read(0, 0, true);
13491312

13501313
// Set these attributes for the merged composite only (subimage 0)
13511314
composite_attribute("thumbnail_width", (int)width);
@@ -1358,15 +1321,6 @@ PSDInput::load_resource_thumbnail(uint32_t length, bool isBGR)
13581321

13591322

13601323

1361-
void
1362-
PSDInput::thumbnail_error_exit(j_common_ptr cinfo)
1363-
{
1364-
thumbnail_error_mgr* mgr = (thumbnail_error_mgr*)cinfo->err;
1365-
longjmp(mgr->setjmp_buffer, 1);
1366-
}
1367-
1368-
1369-
13701324
bool
13711325
PSDInput::load_layers()
13721326
{

0 commit comments

Comments
 (0)