Skip to content

Commit f30c22f

Browse files
authored
Merge pull request #176 from danoli3/feat/unicode-multipage
Add Unicode Windows paths for multipage images
2 parents a5d6f7f + 72d2226 commit f30c22f

10 files changed

Lines changed: 196 additions & 36 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Release notes for this fork, newest first. The 3.19 line starts from upstream Fr
55
## Unreleased
66

77
- Fix optional HEIF/AVIF and JPEG XL configuration on native Windows SDKs without pkg-config. Prefer exported CMake packages, retain pkg-config fallback, support SDK headers/import libraries, and use the same discovery for installed CMake consumers. Add MSVC/vcpkg codec-enabled static/shared CI and setup instructions. (#174)
8+
- Add `FreeImage_OpenMultiBitmapU` for Windows Unicode multipage paths, including disk cache and spool files, without requiring C++17.
89

910
## 3.19.23 — Unreleased
1011

‎Makefile.srcs‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,11 @@ FreeImage_<codec>_LIBRARY_RELEASE / _LIBRARY_DEBUG cache entries. For static
209209
codec SDKs, use their exported CMake targets or provide any missing transitive
210210
libraries through FreeImage_<codec>_EXTRA_LIBRARIES in SDK mode. Installed
211211
FreeImage CMake consumers use the same discovery rules.
212+
213+
### Unicode multipage filenames (Windows)
214+
215+
Use `FreeImage_OpenMultiBitmapU` with a `wchar_t` UTF-16 path to create, read,
216+
edit and close multipage TIFF/GIF/ICO files on Windows. Disk caches and spool
217+
files also use Unicode paths. Arguments and ownership match
218+
`FreeImage_OpenMultiBitmap`. Like `FreeImage_LoadU`/`FreeImage_SaveU`, this
219+
entry point returns NULL on non-Windows platforms; use native `char` paths there.

‎Source/CacheFile.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "FreeImage.h"
3030
#include "Utilities.h"
31+
#include "MultiBitmapPath.h"
3132

3233
// ----------------------------------------------------------
3334

@@ -66,7 +67,7 @@ public :
6667
CacheFile();
6768
~CacheFile();
6869

69-
BOOL open(const std::string& filename = "", BOOL keep_in_memory = TRUE);
70+
BOOL open(const MultiBitmapPath& filename = MultiBitmapPath(), BOOL keep_in_memory = TRUE);
7071
void close();
7172

7273
BOOL readFile(BYTE *data, int nr, int size);
@@ -82,7 +83,7 @@ private :
8283

8384
private :
8485
FILE *m_file;
85-
std::string m_filename;
86+
MultiBitmapPath m_filename;
8687
std::list<int> m_free_pages;
8788
PageCache m_page_cache_mem;
8889
PageCache m_page_cache_disk;

‎Source/FreeImage.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,8 @@ DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsNoPixels(FREE_IMAGE_FORMAT fif);
912912
// Multipaging interface ----------------------------------------------------
913913

914914
DLL_API FIMULTIBITMAP * DLL_CALLCONV FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory FI_DEFAULT(FALSE), int flags FI_DEFAULT(0));
915+
// Windows Unicode path variant; returns NULL on other platforms.
916+
DLL_API FIMULTIBITMAP * DLL_CALLCONV FreeImage_OpenMultiBitmapU(FREE_IMAGE_FORMAT fif, const wchar_t *filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory FI_DEFAULT(FALSE), int flags FI_DEFAULT(0));
915917
DLL_API FIMULTIBITMAP * DLL_CALLCONV FreeImage_OpenMultiBitmapFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));
916918
DLL_API BOOL DLL_CALLCONV FreeImage_SaveMultiBitmapToHandle(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP *bitmap, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));
917919
DLL_API BOOL DLL_CALLCONV FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags FI_DEFAULT(0));

‎Source/FreeImage/CacheFile.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ CacheFile::~CacheFile() {
4747
}
4848

4949
BOOL
50-
CacheFile::open(const std::string& filename, BOOL keep_in_memory) {
50+
CacheFile::open(const MultiBitmapPath& filename, BOOL keep_in_memory) {
5151

5252
assert(!m_file);
5353

5454
m_filename = filename;
5555
m_keep_in_memory = keep_in_memory;
5656

5757
if ((!m_filename.empty()) && (!m_keep_in_memory)) {
58-
m_file = fopen(m_filename.c_str(), "w+b");
58+
m_file = m_filename.open(true);
5959
return (m_file != NULL);
6060
}
6161

@@ -85,7 +85,7 @@ CacheFile::close() {
8585
m_file = NULL;
8686

8787
// delete the file
88-
remove(m_filename.c_str());
88+
m_filename.remove();
8989
}
9090
}
9191

‎Source/FreeImage/MultiPage.cpp‎

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct MULTIBITMAPHEADER {
122122
BOOL changed;
123123
int page_count;
124124
BlockList m_blocks;
125-
std::string m_filename;
125+
MultiBitmapPath m_filename;
126126
BOOL read_only;
127127
FREE_IMAGE_FORMAT cache_fif;
128128
int load_flags;
@@ -138,20 +138,6 @@ struct MULTIBITMAPHEADER {
138138
// Helper functions
139139
// =====================================================================
140140

141-
inline void
142-
ReplaceExtension(std::string& dst_filename, const std::string& src_filename, const std::string& dst_extension) {
143-
size_t lastDot = src_filename.find_last_of('.');
144-
if (lastDot == std::string::npos) {
145-
dst_filename = src_filename;
146-
dst_filename += ".";
147-
dst_filename += dst_extension;
148-
}
149-
else {
150-
dst_filename = src_filename.substr(0, lastDot + 1);
151-
dst_filename += dst_extension;
152-
}
153-
}
154-
155141
} //< ns
156142

157143

@@ -254,12 +240,13 @@ FreeImage_InternalGetPageCount(FIMULTIBITMAP *bitmap) {
254240
// Multipage functions
255241
// =====================================================================
256242

257-
FIMULTIBITMAP * DLL_CALLCONV
258-
FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory, int flags) {
243+
static FIMULTIBITMAP *
244+
OpenMultiBitmapPath(FREE_IMAGE_FORMAT fif, const MultiBitmapPath& filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory, int flags) {
259245

260246
FILE *handle = NULL;
261247
try {
262248
// sanity check on the parameters
249+
if (filename.empty()) return NULL;
263250

264251
if (create_new) {
265252
read_only = FALSE;
@@ -274,7 +261,7 @@ FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL crea
274261

275262
if (node) {
276263
if (!create_new) {
277-
handle = fopen(filename, "rb");
264+
handle = filename.open(false);
278265
if (handle == NULL) {
279266
return NULL;
280267
}
@@ -308,8 +295,7 @@ FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL crea
308295
// set up the cache
309296

310297
if (!read_only) {
311-
std::string cache_name;
312-
ReplaceExtension(cache_name, filename, "ficache");
298+
MultiBitmapPath cache_name = filename.withExtension("ficache");
313299

314300
if (!header->m_cachefile.open(cache_name, keep_cache_in_memory)) {
315301
// an error occured ...
@@ -334,6 +320,28 @@ FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL crea
334320
return NULL;
335321
}
336322

323+
FIMULTIBITMAP * DLL_CALLCONV
324+
FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory, int flags) {
325+
try {
326+
return OpenMultiBitmapPath(fif, MultiBitmapPath(filename), create_new, read_only, keep_cache_in_memory, flags);
327+
} catch (std::bad_alloc &) {
328+
return NULL;
329+
}
330+
}
331+
332+
FIMULTIBITMAP * DLL_CALLCONV
333+
FreeImage_OpenMultiBitmapU(FREE_IMAGE_FORMAT fif, const wchar_t *filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory, int flags) {
334+
#ifdef _WIN32
335+
try {
336+
return OpenMultiBitmapPath(fif, MultiBitmapPath(filename), create_new, read_only, keep_cache_in_memory, flags);
337+
} catch (std::bad_alloc &) {
338+
return NULL;
339+
}
340+
#else
341+
return NULL;
342+
#endif
343+
}
344+
337345
FIMULTIBITMAP * DLL_CALLCONV
338346
FreeImage_OpenMultiBitmapFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags) {
339347
try {
@@ -502,17 +510,15 @@ FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags) {
502510
try {
503511
// open a temp file
504512

505-
std::string spool_name;
506-
507-
ReplaceExtension(spool_name, header->m_filename, "fispool");
513+
MultiBitmapPath spool_name = header->m_filename.withExtension("fispool");
508514

509515
// open the spool file and the source file
510516

511-
FILE *f = fopen(spool_name.c_str(), "w+b");
517+
FILE *f = spool_name.open(true);
512518

513519
// saves changes
514520
if (f == NULL) {
515-
FreeImage_OutputMessageProc(header->fif, "Failed to open %s, %s", spool_name.c_str(), strerror(errno));
521+
FreeImage_OutputMessageProc(header->fif, "Failed to open multipage spool file: %s", strerror(errno));
516522
success = FALSE;
517523
} else {
518524
success = FreeImage_SaveMultiBitmapToHandle(header->fif, bitmap, &header->io, (fi_handle)f, flags);
@@ -521,7 +527,7 @@ FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags) {
521527

522528
if (fclose(f) != 0) {
523529
success = FALSE;
524-
FreeImage_OutputMessageProc(header->fif, "Failed to close %s, %s", spool_name.c_str(), strerror(errno));
530+
FreeImage_OutputMessageProc(header->fif, "Failed to close multipage spool file: %s", strerror(errno));
525531
}
526532
}
527533
if (header->handle) {
@@ -531,13 +537,13 @@ FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags) {
531537
// applies changes to the destination file
532538

533539
if (success) {
534-
remove(header->m_filename.c_str());
535-
success = (rename(spool_name.c_str(), header->m_filename.c_str()) == 0) ? TRUE:FALSE;
540+
header->m_filename.remove();
541+
success = (spool_name.renameTo(header->m_filename) == 0) ? TRUE:FALSE;
536542
if(!success) {
537-
FreeImage_OutputMessageProc(header->fif, "Failed to rename %s to %s", spool_name.c_str(), header->m_filename.c_str());
543+
FreeImage_OutputMessageProc(header->fif, "Failed to rename multipage spool file: %s", strerror(errno));
538544
}
539545
} else {
540-
remove(spool_name.c_str());
546+
spool_name.remove();
541547
}
542548
} catch (std::bad_alloc &) {
543549
success = FALSE;

‎Source/MultiBitmapPath.h‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (C) 2026 The FreeImage Project
2+
// Distributed under the FreeImage Public License or GNU GPL v2; see license-fi.txt and license-gplv2.txt.
3+
// Internal multipage/cache path handling. No locale conversion of Windows paths.
4+
#ifndef FREEIMAGE_MULTIBITMAP_PATH_H
5+
#define FREEIMAGE_MULTIBITMAP_PATH_H
6+
#include <string>
7+
#include <cstdio>
8+
9+
class MultiBitmapPath {
10+
std::string narrow;
11+
#ifdef _WIN32
12+
std::wstring wide;
13+
#endif
14+
public:
15+
MultiBitmapPath() {}
16+
MultiBitmapPath(const char *path) : narrow(path ? path : "") {}
17+
MultiBitmapPath(const std::string& path) : narrow(path) {}
18+
#ifdef _WIN32
19+
MultiBitmapPath(const wchar_t *path) : wide(path ? path : L"") {}
20+
#endif
21+
bool empty() const {
22+
#ifdef _WIN32
23+
if (!wide.empty()) return false;
24+
#endif
25+
return narrow.empty();
26+
}
27+
FILE *open(bool write) const {
28+
#ifdef _WIN32
29+
if (!wide.empty()) return _wfopen(wide.c_str(), write ? L"w+b" : L"rb");
30+
#endif
31+
return fopen(narrow.c_str(), write ? "w+b" : "rb");
32+
}
33+
int remove() const {
34+
#ifdef _WIN32
35+
if (!wide.empty()) return _wremove(wide.c_str());
36+
#endif
37+
return std::remove(narrow.c_str());
38+
}
39+
int renameTo(const MultiBitmapPath& destination) const {
40+
#ifdef _WIN32
41+
if (!wide.empty()) return _wrename(wide.c_str(), destination.wide.c_str());
42+
#endif
43+
return std::rename(narrow.c_str(), destination.narrow.c_str());
44+
}
45+
MultiBitmapPath withExtension(const char *extension) const {
46+
MultiBitmapPath result(*this);
47+
#ifdef _WIN32
48+
if (!wide.empty()) {
49+
const size_t dot = wide.find_last_of(L'.');
50+
result.wide = (dot == std::wstring::npos ? wide + L"." : wide.substr(0, dot + 1));
51+
while (*extension) result.wide += static_cast<wchar_t>(*extension++);
52+
return result;
53+
}
54+
#endif
55+
const size_t dot = narrow.find_last_of('.');
56+
result.narrow = (dot == std::string::npos ? narrow + "." : narrow.substr(0, dot + 1)) + extension;
57+
return result;
58+
}
59+
};
60+
#endif

‎TestAPI/CMakeLists.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,14 @@ if(BUILD_TESTS)
143143
endif()
144144
add_test(NAME ImageAnalysisTest COMMAND ImageAnalysisTest)
145145
endif()
146+
147+
if(BUILD_TESTS AND BUILD_LIBTIFF)
148+
add_executable(UnicodeMultiPageTest testUnicodeMultiPage.cpp)
149+
target_link_libraries(UnicodeMultiPageTest PRIVATE FreeImage)
150+
if(WIN32 AND NOT FREEIMAGE_STATIC)
151+
add_custom_command(TARGET UnicodeMultiPageTest POST_BUILD
152+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
153+
$<TARGET_FILE:FreeImage> $<TARGET_FILE_DIR:UnicodeMultiPageTest>)
154+
endif()
155+
add_test(NAME UnicodeMultiPageTest COMMAND UnicodeMultiPageTest)
156+
endif()

‎TestAPI/testUnicodeMultiPage.cpp‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "FreeImage.h"
2+
#include <cstdio>
3+
#include <cstdlib>
4+
#ifdef _WIN32
5+
#include <direct.h>
6+
#endif
7+
8+
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "Failed line %d: %s\n", __LINE__, #x); std::exit(1); } } while (0)
9+
10+
static void roundTrip(bool memory) {
11+
#ifdef _WIN32
12+
const wchar_t *path = L"multipage-\u65e5\u672c\u8a9e-\u00e9/image-\u4e2d\u6587.tif";
13+
_wmkdir(L"multipage-\u65e5\u672c\u8a9e-\u00e9");
14+
#define OPEN(create, read) FreeImage_OpenMultiBitmapU(FIF_TIFF, path, create, read, memory)
15+
#else
16+
const char *path = "multipage-unicode-api-test.tif";
17+
#define OPEN(create, read) FreeImage_OpenMultiBitmap(FIF_TIFF, path, create, read, memory)
18+
#endif
19+
FIBITMAP *page = FreeImage_Allocate(1024, 1024, 24);
20+
CHECK(page);
21+
// Incompressible content also exercises eviction from the disk page cache.
22+
unsigned state = 17;
23+
for (unsigned y = 0; y < 1024; ++y) {
24+
BYTE *row = FreeImage_GetScanLine(page, y);
25+
for (unsigned x = 0; x < 3072; ++x) {
26+
state = state * 1664525u + 1013904223u;
27+
row[x] = static_cast<BYTE>(state >> 24);
28+
}
29+
}
30+
const BYTE expected = FreeImage_GetScanLine(page, 0)[0];
31+
FIMULTIBITMAP *multi = OPEN(TRUE, FALSE);
32+
CHECK(multi);
33+
FreeImage_AppendPage(multi, page);
34+
FreeImage_AppendPage(multi, page);
35+
FreeImage_Unload(page);
36+
CHECK(FreeImage_CloseMultiBitmap(multi));
37+
multi = OPEN(FALSE, FALSE);
38+
CHECK(multi && FreeImage_GetPageCount(multi) == 2);
39+
page = FreeImage_LockPage(multi, 1);
40+
CHECK(page && FreeImage_GetScanLine(page, 0)[0] == expected);
41+
FreeImage_GetScanLine(page, 0)[0] = expected ^ 255;
42+
FreeImage_UnlockPage(multi, page, TRUE);
43+
CHECK(FreeImage_CloseMultiBitmap(multi));
44+
multi = OPEN(FALSE, TRUE);
45+
CHECK(multi && FreeImage_GetPageCount(multi) == 2);
46+
for (int i = 0; i < 2; ++i) {
47+
page = FreeImage_LockPage(multi, i);
48+
CHECK(page && FreeImage_GetScanLine(page, 0)[0] == (i ? (expected ^ 255) : expected));
49+
FreeImage_UnlockPage(multi, page, FALSE);
50+
}
51+
CHECK(FreeImage_CloseMultiBitmap(multi));
52+
#ifdef _WIN32
53+
CHECK(_wremove(path) == 0);
54+
CHECK(_wrmdir(L"multipage-\u65e5\u672c\u8a9e-\u00e9") == 0); // cache/spool must be gone
55+
#else
56+
CHECK(std::remove(path) == 0);
57+
#endif
58+
#undef OPEN
59+
}
60+
61+
int main() {
62+
FreeImage_Initialise();
63+
CHECK(!FreeImage_OpenMultiBitmapU(FIF_TIFF, NULL, TRUE, FALSE));
64+
#ifndef _WIN32
65+
CHECK(!FreeImage_OpenMultiBitmapU(FIF_TIFF, L"unused.tif", TRUE, FALSE));
66+
#endif
67+
roundTrip(false);
68+
roundTrip(true);
69+
FreeImage_DeInitialise();
70+
return 0;
71+
}

0 commit comments

Comments
 (0)