Skip to content

Commit 0e24922

Browse files
committed
New: Support for JXR image format
1 parent e11046b commit 0e24922

7 files changed

Lines changed: 122 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
99
##### 🚀 New Features
1010

1111
- Improve chapter detection in tracking [`ad05a23`](https://github.com/ollm/OpenComic/commit/ad05a2364882227688cccfa2fdf58a21f8e9615c)
12-
- Support for `JP2` image format
12+
- Support for `JP2` image format [`e11046b`](https://github.com/ollm/OpenComic/commit/e11046b4eca758f8b15c1caeb831a235a5f3d62c)
13+
- Support for `JXR` image format
1314

1415
## [v1.4.1](https://github.com/ollm/OpenComic/releases/tag/v1.4.1) (08-02-2025)
1516

‎package-lock.json‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"gm": "^1.25.0",
8888
"handlebars": "^4.7.8",
8989
"jimp": "^1.6.0",
90+
"jpegxr": "^0.3.0",
9091
"jquery": "^3.7.1",
9192
"jquery-bez": "^1.0.11",
9293
"lodash.isequal": "^4.5.0",

‎scripts/image.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ async function resizeToBlob(fromImage, config = {})
196196

197197
}
198198

199-
async function rawToPng(fromBuffer, toImage, width, height, channels = 3, config = {})
199+
async function rawToPng(fromBuffer, toImage, raw = {}, config = {})
200200
{
201201
if(sharp === false) sharp = require('sharp');
202202

@@ -205,9 +205,11 @@ async function rawToPng(fromBuffer, toImage, width, height, channels = 3, config
205205
compressionLevel: 2,
206206
}, ...config};
207207

208+
console.log({raw: raw});
209+
208210
return new Promise(function(resolve, reject) {
209211

210-
sharp(fromBuffer, {raw: {width: width, height: height, channels: channels}}).png({force: true, compressionLevel: config.compressionLevel}).toFile(toImage, function(error) {
212+
sharp(fromBuffer, {raw: raw}).keepIccProfile().pipelineColourspace(raw.rgb16 ? 'rgb16' : 'srgb').removeAlpha().toColourspace(raw.rgb16 ? 'rgb16' : 'srgb').png({force: true, compressionLevel: config.compressionLevel}).toFile(toImage, function(error) {
211213

212214
if(error)
213215
reject();

‎scripts/opencomic.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ var compatibleMime = [
141141
'image/jp2',
142142
'image/jpx',
143143
'image/jpm',
144+
'image/jxr',
145+
'image/vnd.ms-photo',
144146
'image/png',
145147
'image/apng',
146148
'image/svg',
@@ -157,9 +159,13 @@ var compatibleMime = [
157159

158160
// This image formats requires conversion to PNG to be displayed
159161
var convertMime = [
162+
// jp2
160163
'image/jp2',
161164
'image/jpx',
162165
'image/jpm',
166+
// jxr
167+
'image/jxr',
168+
'image/vnd.ms-photo',
163169
];
164170

165171
var compressedMime = {
@@ -287,6 +293,7 @@ var imageExtensions = {
287293
'avifs',
288294
],
289295
convert: [ // This image formats requires conversion to PNG to be displayed
296+
// jp2
290297
'jp2',
291298
'j2k',
292299
'jpf',
@@ -295,6 +302,10 @@ var imageExtensions = {
295302
'j2c',
296303
'jpc',
297304
'jpx',
305+
// jxr
306+
'jxr',
307+
'hdp',
308+
'wdp',
298309
],
299310
jpg: [
300311
'jpg',
@@ -315,6 +326,11 @@ var imageExtensions = {
315326
'jpc',
316327
'jpx',
317328
],
329+
jxr: [
330+
'jxr',
331+
'hdp',
332+
'wdp',
333+
],
318334
png: [
319335
'png',
320336
'x-png',

‎scripts/worker/convert-image.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const fs = require('fs');
22

3+
var jpegxr = false;
4+
35
async function jp2(path)
46
{
57
const buffer = fs.readFileSync(path);
@@ -37,6 +39,47 @@ async function jp2(path)
3739
return false;
3840
}
3941

42+
async function jxr(path)
43+
{
44+
const buffer = fs.readFileSync(path);
45+
46+
if(jpegxr === false)
47+
jpegxr = await require('jpegxr')();
48+
49+
try
50+
{
51+
const image = jpegxr.decode(buffer);
52+
const pixelInfo = image.pixelInfo;
53+
54+
const len = image.bytes.length;
55+
56+
const width = image.width;
57+
const height = image.height;
58+
59+
const channels = Math.round(len / (width * height));
60+
61+
return {
62+
buffer: image.bytes,
63+
width: width,
64+
height: height,
65+
length: len,
66+
channels: pixelInfo.channels || channels,
67+
colorFormat: pixelInfo.colorFormat,
68+
bits: +pixelInfo.bitDepth.replace(/[^0-9]+/, ''),
69+
bitsString: pixelInfo.bitDepth,
70+
bitsPerPixel: pixelInfo.bitsPerPixel,
71+
premultiplied: pixelInfo.premultipledAlpha,
72+
pixelInfo: pixelInfo,
73+
};
74+
}
75+
catch(error)
76+
{
77+
return {error: error};
78+
}
79+
80+
return false;
81+
}
82+
4083
async function convert(path, mime)
4184
{
4285
switch (mime)
@@ -47,6 +90,12 @@ async function convert(path, mime)
4790

4891
return jp2(path);
4992

93+
break;
94+
case 'image/jxr':
95+
case 'image/vnd.ms-photo':
96+
97+
return jxr(path);
98+
5099
break;
51100
}
52101

‎scripts/workers.js‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,37 @@ function work(options = {})
132132

133133
}
134134

135+
function float32To16Array(uint8Buffer)
136+
{
137+
const floatData = new Float32Array(
138+
uint8Buffer.buffer,
139+
uint8Buffer.byteOffset,
140+
uint8Buffer.byteLength / Float32Array.BYTES_PER_ELEMENT
141+
);
142+
143+
const uint16Data = new Uint16Array(floatData.length);
144+
145+
for(let i = 0, len = floatData.length; i < len; i++)
146+
{
147+
const clamped = Math.max(0, Math.min(1, floatData[i]));
148+
uint16Data[i] = Math.round(clamped * 65535);
149+
}
150+
151+
return uint16Data;
152+
}
153+
154+
function convertBuffer(buffer, bits, bitsString = false)
155+
{
156+
if(bits >= 32 && bitsString.toLowerCase() === '32float')
157+
buffer = float32To16Array(buffer);
158+
else if(bits >= 32)
159+
buffer = new Uint16Array(new Uint32Array(buffer.buffer));
160+
else if(bits >= 16)
161+
buffer = new Uint16Array(buffer.buffer);
162+
163+
return buffer;
164+
}
165+
135166
async function convertImage(path)
136167
{
137168
const webpPath = fileManager.realPath(path);
@@ -156,7 +187,18 @@ async function convertImage(path)
156187
if(!fs.existsSync(parentPath))
157188
fs.mkdirSync(parentPath);
158189

159-
await image.rawToPng(result.buffer, webpPath, result.width, result.height, result.channels, {
190+
const bits = result.bits;
191+
192+
if(bits > 8 && result.buffer instanceof Uint8Array)
193+
result.buffer = convertBuffer(result.buffer, bits, result.bitsString);
194+
195+
await image.rawToPng(result.buffer, webpPath, {
196+
width: result.width,
197+
height: result.height,
198+
channels: result.channels,
199+
rgb16: bits > 8 ? true : false,
200+
premultiplied: result.premultiplied || false,
201+
}, {
160202
compressionLevel: 2,
161203
});
162204

0 commit comments

Comments
 (0)