Repository navigation
Expand file tree
/
Copy pathCodec.cpp
More file actions
665 lines (535 loc) · 19.9 KB
/
Copy pathCodec.cpp
File metadata and controls
665 lines (535 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include "Codec.h"
#include <wx/wx.h>
#include <wx/filename.h>
#include <wx/mstream.h>
#include <wx/wfstream.h>
#include <wx/image.h>
#include <libheif/heif.h>
#include <openjpeg.h>
wxImage Codec::LoadHEIFImage(const wxString& filepath) {
wxImage emptyImage;
heif_context* ctx = heif_context_alloc();
heif_error err = heif_context_read_from_file(ctx, filepath.utf8_str(), nullptr);
if (err.code != heif_error_Ok) {
wxLogError("libheif failed to read file: %s", err.message);
heif_context_free(ctx);
return emptyImage;
}
heif_image_handle* handle;
err = heif_context_get_primary_image_handle(ctx, &handle);
if (err.code != heif_error_Ok) {
wxLogError("libheif failed to get primary image handle: %s", err.message);
heif_context_free(ctx);
return emptyImage;
}
int width = heif_image_handle_get_width(handle);
int height = heif_image_handle_get_height(handle);
heif_image* img;
err = heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_RGB, nullptr);
if (err.code != heif_error_Ok) {
wxLogError("libheif failed to decode image: %s", err.message);
heif_image_handle_release(handle);
heif_context_free(ctx);
return emptyImage;
}
int stride;
const uint8_t* heif_data = heif_image_get_plane_readonly(img, heif_channel_interleaved, &stride);
unsigned char* wxData = (unsigned char*)malloc(width * height * 3);
if (!wxData) {
wxLogError("Failed to allocate memory for wxImage pixel data.");
heif_image_release(img);
heif_image_handle_release(handle);
heif_context_free(ctx);
return emptyImage;
}
for (int y = 0; y < height; ++y) {
memcpy(wxData + (y * width * 3), heif_data + (y * stride), width * 3);
}
heif_image_release(img);
heif_image_handle_release(handle);
heif_context_free(ctx);
return wxImage(width, height, wxData);
}
bool Codec::SaveHEIFImage(const wxImage& img, const wxString& filePath, int quality) {
if (!img.IsOk()) {
wxLogError("Invalid wxImage passed to HEIF encoder.");
return false;
}
int width = img.GetWidth();
int height = img.GetHeight();
bool hasAlpha = img.HasAlpha();
heif_image* h_image = nullptr;
heif_chroma chroma = hasAlpha ? heif_chroma_interleaved_RGBA : heif_chroma_interleaved_RGB;
heif_error err = heif_image_create(width, height, heif_colorspace_RGB, chroma, &h_image);
if (err.code != heif_error_Ok) {
wxLogError("Failed to create HEIF image: %s", err.message);
return false;
}
err = heif_image_add_plane(h_image, heif_channel_interleaved, width, height, hasAlpha ? 32 : 24);
if (err.code != heif_error_Ok) {
wxLogError("Failed to add plane to HEIF image: %s", err.message);
heif_image_release(h_image);
return false;
}
int stride;
uint8_t* heif_data = heif_image_get_plane(h_image, heif_channel_interleaved, &stride);
unsigned char* wx_rgb = img.GetData();
unsigned char* wx_alpha = hasAlpha ? img.GetAlpha() : nullptr;
for (int y = 0; y < height; ++y) {
uint8_t* row = heif_data + (y * stride);
for (int x = 0; x < width; ++x) {
int wx_idx = (y * width + x);
int rgb_idx = wx_idx * 3;
int h_idx = x * (hasAlpha ? 4 : 3);
row[h_idx] = wx_rgb[rgb_idx]; // R
row[h_idx + 1] = wx_rgb[rgb_idx + 1]; // G
row[h_idx + 2] = wx_rgb[rgb_idx + 2]; // B
if (hasAlpha) {
row[h_idx + 3] = wx_alpha[wx_idx]; // A
}
}
}
heif_context* ctx = heif_context_alloc();
heif_encoder* encoder = nullptr;
wxFileName fileName(filePath);
fileName.Normalize(wxPATH_NORM_ALL & wxPATH_NORM_ENV_VARS);
if (fileName.GetExt() == "avif") {
err = heif_context_get_encoder_for_format(ctx, heif_compression_AV1, &encoder);
}
else {
err = heif_context_get_encoder_for_format(ctx, heif_compression_HEVC, &encoder);
}
bool success = false;
if (err.code == heif_error_Ok) {
heif_encoder_set_lossy_quality(encoder, quality);
err = heif_context_encode_image(ctx, h_image, encoder, nullptr, nullptr);
if (err.code == heif_error_Ok) {
err = heif_context_write_to_file(ctx, filePath.utf8_str().data());
if (err.code == heif_error_Ok) {
success = true;
}
else {
wxLogError("Failed to write HEIF file: %s", err.message);
}
}
else {
wxLogError("Failed to encode HEIF image: %s", err.message);
}
heif_encoder_release(encoder);
}
else {
wxLogError("Failed to get HEVC encoder. Ensure libheif was compiled with an HEVC encoder (like x265).");
}
heif_context_free(ctx);
heif_image_release(h_image);
return success;
}
wxImage Codec::LoadJP2(const wxString& filePath) {
wxImage empty;
wxFileInputStream jp2Stream(filePath);
if (!jp2Stream.IsOk()) {
wxLogError("Failed to open JPEG 2000 file: %s", filePath);
return empty;
}
size_t dataSize = jp2Stream.GetLength();
if (dataSize == 0) {
wxLogError("JPEG 2000 file is empty: %s", filePath);
return empty;
}
wxMemoryBuffer buffer(dataSize);
if (jp2Stream.Read(buffer.GetData(), dataSize).LastRead() != dataSize) {
wxLogError("Failed to read JPEG 2000 file into memory: %s", filePath);
return empty;
}
unsigned char* data = static_cast<unsigned char*>(buffer.GetData());
wxImage img = Codec::DecodeJP2(data, dataSize);
if (!img.IsOk()) {
wxLogError("OpenJPEG failed to decode JPEG 2000 file: %s", filePath);
return empty;
}
return img;
}
static OPJ_CODEC_FORMAT DetectJP2Format(const unsigned char* data, size_t size) {
if (size >= 12 &&
data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x00 && data[3] == 0x0C &&
data[4] == 0x6A && data[5] == 0x50 && data[6] == 0x20 && data[7] == 0x20)
{
return OPJ_CODEC_JP2; // .jp2 / .jpf / .jpx (shared box container)
}
if (size >= 4 &&
data[0] == 0xFF && data[1] == 0x4F &&
data[2] == 0xFF && data[3] == 0x51)
{
return OPJ_CODEC_J2K; // .j2c / .j2k raw codestream
}
return OPJ_CODEC_UNKNOWN;
}
wxImage Codec::DecodeJP2(const unsigned char* data, size_t size) {
wxImage empty;
OPJ_CODEC_FORMAT format = DetectJP2Format(data, size);
if (format == OPJ_CODEC_UNKNOWN) {
wxLogError("Unrecognized JPEG 2000 stream: no JP2 box or J2K codestream signature found.");
return empty;
}
opj_dparameters_t params;
opj_set_default_decoder_parameters(¶ms);
opj_stream_t* stream = opj_stream_create(size, true);
if (!stream) return empty;
unsigned char* readPtr = const_cast<unsigned char*>(data);
const unsigned char* dataEnd = data + size;
opj_stream_set_read_function(stream,
[](void* p_buffer, size_t p_nb_bytes, void* p_user_data) -> size_t {
auto* ctx = static_cast<std::pair<unsigned char*, const unsigned char*>*>(p_user_data);
size_t remaining = static_cast<size_t>(ctx->second - ctx->first);
size_t toRead = std::min(p_nb_bytes, remaining);
if (toRead == 0) return static_cast<size_t>(-1);
memcpy(p_buffer, ctx->first, toRead);
ctx->first += toRead;
return toRead;
});
std::pair<unsigned char*, const unsigned char*> readCtx(readPtr, dataEnd);
opj_stream_set_user_data(stream, &readCtx, nullptr);
opj_stream_set_user_data_length(stream, size);
opj_codec_t* codec = opj_create_decompress(format);
if (!codec) {
opj_stream_destroy(stream);
return empty;
}
if (!opj_setup_decoder(codec, ¶ms)) {
opj_destroy_codec(codec);
opj_stream_destroy(stream);
return empty;
}
opj_image_t* jp2Image = nullptr;
if (!opj_read_header(stream, codec, &jp2Image)) {
opj_destroy_codec(codec);
opj_stream_destroy(stream);
return empty;
}
if (!opj_decode(codec, stream, jp2Image)) {
opj_image_destroy(jp2Image);
opj_destroy_codec(codec);
opj_stream_destroy(stream);
return empty;
}
int w = jp2Image->x1 - jp2Image->x0;
int h = jp2Image->y1 - jp2Image->y0;
bool hasAlpha = (jp2Image->numcomps == 4);
wxImage img(w, h, hasAlpha);
unsigned char* rgb = img.GetData();
unsigned char* alpha = hasAlpha ? img.GetAlpha() : nullptr;
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
int idx = y * w + x;
rgb[idx * 3 + 0] = jp2Image->comps[0].data[idx];
rgb[idx * 3 + 1] = jp2Image->comps[1].data[idx];
rgb[idx * 3 + 2] = jp2Image->comps[2].data[idx];
if (hasAlpha)
alpha[idx] = jp2Image->comps[3].data[idx];
}
}
opj_image_destroy(jp2Image);
opj_destroy_codec(codec);
opj_stream_destroy(stream);
return img;
}
// Builds an opj_image_t (3 or 4 x 8-bit components) from a wxImage.
// Caller owns the returned image and must opj_image_destroy() it.
static opj_image_t* CreateOPJImageFromWx(const wxImage& img) {
int width = img.GetWidth();
int height = img.GetHeight();
bool hasAlpha = img.HasAlpha();
int numComps = hasAlpha ? 4 : 3;
opj_image_cmptparm_t cmptparm[4];
memset(cmptparm, 0, sizeof(cmptparm));
for (int i = 0; i < numComps; ++i) {
cmptparm[i].dx = 1;
cmptparm[i].dy = 1;
cmptparm[i].w = width;
cmptparm[i].h = height;
cmptparm[i].x0 = 0;
cmptparm[i].y0 = 0;
cmptparm[i].prec = 8;
cmptparm[i].bpp = 8;
cmptparm[i].sgnd = 0;
}
opj_image_t* image = opj_image_create(numComps, cmptparm, OPJ_CLRSPC_SRGB);
if (!image) return nullptr;
image->x0 = 0;
image->y0 = 0;
image->x1 = width;
image->y1 = height;
const unsigned char* rgb = img.GetData();
const unsigned char* alpha = hasAlpha ? img.GetAlpha() : nullptr;
int pixelCount = width * height;
for (int i = 0; i < pixelCount; ++i) {
image->comps[0].data[i] = rgb[i * 3 + 0];
image->comps[1].data[i] = rgb[i * 3 + 1];
image->comps[2].data[i] = rgb[i * 3 + 2];
if (hasAlpha) {
image->comps[3].data[i] = alpha[i];
}
}
return image;
}
// Writes a wxImage as JP2 (.jp2/.jpf, boxed format) or raw codestream
// (.j2k/.j2c), chosen by the output file's extension. quality is 1-100;
// 100 requests lossless encoding, anything lower is treated as a
// compression-ratio target (lower quality = smaller/lossier file).
bool Codec::SaveJP2(const wxImage& img, const wxString& filePath, int quality) {
if (!img.IsOk()) {
wxLogError("Invalid wxImage passed to SaveJP2.");
return false;
}
quality = std::clamp(quality, 1, 100);
wxFileName fileName(filePath);
fileName.Normalize(wxPATH_NORM_ALL & wxPATH_NORM_ENV_VARS);
wxString ext = fileName.GetExt().Lower();
OPJ_CODEC_FORMAT format =
(ext == "j2k" || ext == "j2c") ? OPJ_CODEC_J2K : OPJ_CODEC_JP2;
opj_image_t* image = CreateOPJImageFromWx(img);
if (!image) {
wxLogError("Failed to build OpenJPEG image from wxImage.");
return false;
}
opj_cparameters_t params;
opj_set_default_encoder_parameters(¶ms);
// Single quality layer. quality == 100 -> lossless (reversible 5-3
// wavelet, no rate cap). Otherwise -> lossy with a rate target derived
// from quality (roughly: lower quality => higher compression ratio).
params.tcp_numlayers = 1;
params.cp_disto_alloc = 1;
if (quality >= 100) {
params.irreversible = 0;
params.tcp_rates[0] = 0; // 0 = lossless in OpenJPEG's convention
}
else {
params.irreversible = 1; // 9-7 wavelet, needed for lossy
// Map 1-99 -> a compression ratio roughly between 40:1 and ~1.1:1.
double ratio = 1.0 + (100 - quality) * 0.4;
params.tcp_rates[0] = static_cast<float>(ratio);
}
opj_codec_t* codec = opj_create_compress(format);
if (!codec) {
wxLogError("Failed to create OpenJPEG encoder.");
opj_image_destroy(image);
return false;
}
if (!opj_setup_encoder(codec, ¶ms, image)) {
wxLogError("Failed to set up OpenJPEG encoder.");
opj_destroy_codec(codec);
opj_image_destroy(image);
return false;
}
opj_stream_t* stream = opj_stream_create_default_file_stream(
filePath.utf8_str(), OPJ_FALSE /* write stream */);
if (!stream) {
wxLogError("Failed to open output stream for: %s", filePath);
opj_destroy_codec(codec);
opj_image_destroy(image);
return false;
}
bool success = false;
if (!opj_start_compress(codec, image, stream)) {
wxLogError("opj_start_compress failed for: %s", filePath);
}
else if (!opj_encode(codec, stream)) {
wxLogError("opj_encode failed for: %s", filePath);
}
else if (!opj_end_compress(codec, stream)) {
wxLogError("opj_end_compress failed for: %s", filePath);
}
else {
success = true;
}
opj_stream_destroy(stream);
opj_destroy_codec(codec);
opj_image_destroy(image);
if (success) {
wxLogMessage("JPEG 2000 file successfully written: %s", filePath);
}
return success;
}
wxImage Codec::LoadICNS(const wxString& filePath) {
wxImage empty;
wxFileInputStream in(filePath);
if (!in.IsOk()) {
wxLogError("Failed to open ICNS file: %s", filePath);
return empty;
}
size_t fileSize = in.GetLength();
if (fileSize < 8) {
wxLogError("ICNS file too small: %s", filePath);
return empty;
}
wxMemoryBuffer buffer(fileSize);
if (in.Read(buffer.GetData(), fileSize).LastRead() != fileSize) {
wxLogError("Failed to read ICNS file into memory: %s", filePath);
return empty;
}
const unsigned char* data = static_cast<const unsigned char*>(buffer.GetData());
if (fileSize < 8 || memcmp(data, "icns", 4) != 0) {
wxLogError("Invalid ICNS header in file: %s", filePath);
return empty;
}
auto ReadBE32 = [](const unsigned char* p) -> uint32_t {
return (static_cast<uint32_t>(p[0]) << 24) |
(static_cast<uint32_t>(p[1]) << 16) |
(static_cast<uint32_t>(p[2]) << 8) |
static_cast<uint32_t>(p[3]);
};
uint32_t totalSize = ReadBE32(data + 4);
if (totalSize > fileSize) {
totalSize = static_cast<uint32_t>(fileSize);
}
wxImage best;
long bestPixels = 0;
size_t offset = 8;
while (offset + 8 <= totalSize) {
const unsigned char* chunkHeader = data + offset;
char type[5] = { 0 };
memcpy(type, chunkHeader, 4);
uint32_t chunkSize = ReadBE32(chunkHeader + 4);
if (chunkSize < 8 || offset + chunkSize > totalSize) {
wxLogError("Malformed ICNS chunk '%s' at offset %zu, stopping scan.", type, offset);
break;
}
const unsigned char* chunkData = chunkHeader + 8;
size_t chunkDataSize = chunkSize - 8;
if (chunkDataSize > 8 &&
chunkData[0] == 0x89 && chunkData[1] == 0x50 &&
chunkData[2] == 0x4E && chunkData[3] == 0x47)
{
wxMemoryInputStream mem(chunkData, chunkDataSize);
wxImage candidate;
if (candidate.LoadFile(mem, wxBITMAP_TYPE_PNG) && candidate.IsOk())
{
long pixels = static_cast<long>(candidate.GetWidth()) *
static_cast<long>(candidate.GetHeight());
if (pixels > bestPixels)
{
bestPixels = pixels;
best = candidate;
}
}
}
offset += chunkSize;
}
if (!best.IsOk()) {
wxLogError("No usable PNG image found in ICNS file: %s", filePath);
return empty;
}
return best;
}
static void WriteBE32(wxFileOutputStream& out, uint32_t value)
{
unsigned char b[4];
b[0] = static_cast<unsigned char>((value >> 24) & 0xFF);
b[1] = static_cast<unsigned char>((value >> 16) & 0xFF);
b[2] = static_cast<unsigned char>((value >> 8) & 0xFF);
b[3] = static_cast<unsigned char>(value & 0xFF);
out.Write(b, 4);
}
static wxImage ScaleWithTransparentPadding(const wxImage& src, int targetSize) {
wxImage working = src.Copy();
if (!working.HasAlpha()) {
working.InitAlpha();
}
int srcW = working.GetWidth();
int srcH = working.GetHeight();
double scale = std::min(
static_cast<double>(targetSize) / srcW,
static_cast<double>(targetSize) / srcH
);
int scaledW = std::max(1, static_cast<int>(std::round(srcW * scale)));
int scaledH = std::max(1, static_cast<int>(std::round(srcH * scale)));
wxImage scaled = working.Scale(scaledW, scaledH, wxIMAGE_QUALITY_HIGH);
if (!scaled.HasAlpha()) {
scaled.InitAlpha();
}
wxImage padded(targetSize, targetSize);
padded.InitAlpha();
unsigned char* dstRGB = padded.GetData();
unsigned char* dstAlpha = padded.GetAlpha();
memset(dstRGB, 0, targetSize * targetSize * 3);
memset(dstAlpha, 0, targetSize * targetSize);
unsigned char* srcRGB = scaled.GetData();
unsigned char* srcAlpha = scaled.GetAlpha();
int offsetX = (targetSize - scaledW) / 2;
int offsetY = (targetSize - scaledH) / 2;
for (int y = 0; y < scaledH; ++y) {
int dstY = offsetY + y;
if (dstY < 0 || dstY >= targetSize) continue;
unsigned char* dstRowRGB = dstRGB + (dstY * targetSize + offsetX) * 3;
const unsigned char* srcRowRGB = srcRGB + (y * scaledW) * 3;
memcpy(dstRowRGB, srcRowRGB, scaledW * 3);
unsigned char* dstRowAlpha = dstAlpha + (dstY * targetSize + offsetX);
const unsigned char* srcRowAlpha = srcAlpha + (y * scaledW);
memcpy(dstRowAlpha, srcRowAlpha, scaledW);
}
return padded;
}
bool Codec::SaveICNS(const wxImage& img, const wxString& filePath)
{
if (!img.IsOk()) {
wxLogError("Invalid wxImage passed to SaveICNS.");
return false;
}
struct IconSpec {
const char* type;
int size;
};
const IconSpec icons[] = {
{"icp4", 16},
{"icp5", 32},
{"icp6", 64},
{"ic07", 128},
{"ic08", 256},
{"ic09", 512},
{"ic10", 1024},
{"ic11", 32},
{"ic12", 64},
{"ic13", 256},
{"ic14", 512},
};
wxFileOutputStream out(filePath);
if (!out.IsOk()) {
wxLogError("Failed to open ICNS output file: %s", filePath);
return false;
}
char magic[4] = { 'i','c','n','s' };
out.Write(magic, 4);
WriteBE32(out, 0);
uint32_t totalSize = 8;
for (const auto& icon : icons) {
wxImage scaled = ScaleWithTransparentPadding(img, icon.size);
wxMemoryOutputStream pngStream;
if (!scaled.SaveFile(pngStream, wxBITMAP_TYPE_PNG)) {
wxLogError("Failed to encode PNG for ICNS block: %s", icon.type);
return false;
}
size_t pngSize = pngStream.GetSize();
if (pngSize == 0) {
wxLogError("Empty PNG stream for ICNS block: %s", icon.type);
return false;
}
wxStreamBuffer* buf = pngStream.GetOutputStreamBuffer();
buf->Seek(0, wxFromStart);
wxMemoryBuffer pngData(pngSize);
buf->Read(pngData.GetWriteBuf(pngSize), pngSize);
pngData.UngetWriteBuf(pngSize);
char type[4];
memcpy(type, icon.type, 4);
out.Write(type, 4);
uint32_t blockSize = 8 + static_cast<uint32_t>(pngSize);
WriteBE32(out, blockSize);
out.Write(pngData.GetData(), pngSize);
totalSize += blockSize;
}
out.SeekO(4, wxFromStart);
WriteBE32(out, totalSize);
wxLogMessage("ICNS file successfully written: %s", filePath);
return true;
}