Skip to content

Commit

Permalink
New: Improved reading load and memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ollm committed Aug 12, 2024
1 parent 2737fbe commit f12cbdb
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 124 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Setting to force app Color Profile [`c7d479a`](https://github.com/ollm/OpenComic/commit/c7d479abeed6638c39ec413abd68ccfcacf6d5d5)
- WebDAV server support [`b6f4439`](https://github.com/ollm/OpenComic/commit/b6f4439d8953b3855fbc3d7ff476bfd6230a51a1)
- Library Navigation using side mouse buttons [`96f4bb8`](https://github.com/ollm/OpenComic/commit/96f4bb8e0033bd58b78e9ec29155ae332c72ec8b)
- Move to trash and Delete permanently options
- Move to trash and Delete permanently options [`2737fbe`](https://github.com/ollm/OpenComic/commit/2737fbe2b9a318c9e5a8b27885e153caa5e9b327)
- Improved reading load and memory usage


##### 🐛 Bug Fixes
Expand Down
2 changes: 0 additions & 2 deletions scripts/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,6 @@ function showHoverText()
let left = rect.left + (rect.width / 2);
let top = rect.top + rect.height;

console.log(left+' + '+hoverRect.width+' / 2 + 8 > '+window.innerWidth);

if(left < (hoverRect.width / 2) + 8)
left = (hoverRect.width / 2) + 8;
else if(left + (hoverRect.width / 2) + 8 > window.innerWidth)
Expand Down
130 changes: 127 additions & 3 deletions scripts/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ async function resize(fromImage, toImage, config = {})

return new Promise(function(resolve, reject) {

const extension = fileExtension(fromImage);

if(/*inArray(extension, imageExtensions.ico)/* || */inArray(extension, imageExtensions.ico)) // Unsupported images format for resize
return reject({});

sharp(fromImage).jpeg({quality: config.quality}).resize(config).toFile(toImage, function(error) {

if(error)
Expand Down Expand Up @@ -125,6 +130,12 @@ async function resizeToBlob(fromImage, config = {})
compressionLevel: 0,
}, ...config};

if(config.width && config.width < 1)
config.width = 1;

if(config.height && config.height < 1)
config.height = 1;

return new Promise(function(resolve, reject) {

// pipelineColourspace('rgb16').toColourspace('rgb16')
Expand Down Expand Up @@ -266,15 +277,19 @@ async function isAnimated(path)
let extension = fileExtension(path);
let _isAnimated = false;

if(inArray(extension, imageExtensions.jpg) || inArray(extension, imageExtensions.bmp) || inArray(extension, imageExtensions.ico)) // Extensions that do not support animations
if(inArray(extension, imageExtensions.bmp) || inArray(extension, imageExtensions.ico)) // Unsupported image format by sharp
{
_isAnimated = true;
}
else if(inArray(extension, imageExtensions.jpg)) // Extensions that do not support animations
{
_isAnimated = false;
}
else if(inArray(extension, imageExtensions.svg) || inArray(extension, imageExtensions.gif)) // Is always animated or is a vector format
else if(inArray(extension, imageExtensions.svg)) // Is a vector format
{
_isAnimated = true;
}
else if(inArray(extension, imageExtensions.png) || inArray(extension, imageExtensions.webp) || inArray(extension, imageExtensions.avif)) // They can have animations
else if(inArray(extension, imageExtensions.png) || inArray(extension, imageExtensions.webp) || inArray(extension, imageExtensions.avif) || inArray(extension, imageExtensions.gif)) // They can have animations
{
_isAnimated = false;

Expand All @@ -286,6 +301,8 @@ async function isAnimated(path)
type = 'image/webp';
else if(inArray(extension, imageExtensions.avif))
type = 'image/avif';
else if(inArray(extension, imageExtensions.gif))
type = 'image/gif';

let decoder = new ImageDecoder({data: await fsp.readFile(path), type: type});
await decoder.tracks.ready;
Expand All @@ -305,6 +322,16 @@ async function isAnimated(path)
return _isAnimated;
}

function sharpSupportedFormat(path, extension = false)
{
extension = extension || fileExtension(path);

if(inArray(extension, imageExtensions.bmp) || inArray(extension, imageExtensions.ico)) // Unsupported image format by sharp
return false;

return true;
}

function loadImage(url, encode = false)
{
return new Promise(function(resolve) {
Expand All @@ -315,12 +342,109 @@ function loadImage(url, encode = false)
});
}

var threads = false, sizesCache = {};

async function getSizes(images)
{
if(sharp === false) sharp = require('sharp');
if(threads === false) threads = os.cpus().length || 1;

const sizes = [];
const len = images.length;

for(let i = 0; i < len; i++)
{
sizes.push(false);
}

let promises = [];
let index = 0;

for(let i = 0; i < threads; i++)
{
promises.push(new Promise(async function(resolve) {

toBreak:
while(true)
{
const p = index++;
const image = images[p];

if(image)
{
if(image.image && !image.folder)
{
const sha = image.sha || sha1(image.path);

if(sizesCache[sha])
{
sizes[p] = sizesCache[sha];
}
else
{
let size = {
width: 1,
height: 1,
};

try
{
if(sharpSupportedFormat(image.image))
{
const _sharp = sharp(shortWindowsPath.generateSync(image.image));
const metadata = await _sharp.metadata();

size = {
width: metadata.width,
height: metadata.height,
};
}
else
{
const img = new Image();
img.src = image.image;
await img.decode();

size = {
width: img.naturalWidth,
height: img.naturalHeight,
};
}
}
catch(error)
{
console.error(error);
}

sizesCache[sha] = size;
sizes[p] = size;
}
}
}
else
{
break toBreak;
}
}

resolve();

}));
}

await Promise.all(promises)

return sizes;
}

module.exports = {
resize: resize,
resizeToCanvas: resizeToCanvas,
resizeToBlob: resizeToBlob,
convertToPng: convertToPng,
convertToWebp: convertToWebp,
isAnimated: isAnimated,
sharpSupportedFormat: sharpSupportedFormat,
loadImage: loadImage,
getSizes: getSizes,
};
Loading

0 comments on commit f12cbdb

Please sign in to comment.