Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transcoding Support + More Image Format Support (JPEGXL) #3250

Open
wants to merge 37 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e2a86c6
Add JXL Support.
maxpiva Aug 15, 2024
cb647ad
Make sure if djxl do not exists, continue as usual.
maxpiva Aug 15, 2024
7f6db24
Changed the way conversion happens.
maxpiva Aug 23, 2024
dd0021a
Missing file
maxpiva Aug 23, 2024
efc14ff
Merge branch 'Kareadita:develop' into develop
maxpiva Aug 23, 2024
9d48664
Revert "Changed the way conversion happens."
maxpiva Aug 26, 2024
cf05e5f
Reapply "Changed the way conversion happens."
maxpiva Aug 26, 2024
4902a74
Changed the way conversion happens.
maxpiva Aug 23, 2024
21b4c1b
Merge with Karedita
maxpiva Aug 26, 2024
7d48f6e
Add Documentation
maxpiva Aug 27, 2024
a4a12a1
FIX BOMS
maxpiva Aug 28, 2024
0f98d75
Recode SupportedImageTypesFromRequest removing possible errors.
maxpiva Aug 28, 2024
937f89c
Remove own docker build
maxpiva Aug 28, 2024
a08a795
Shell Call no longer used, since swapped image conversion to Image Ma…
maxpiva Aug 28, 2024
bc8481c
Merge branch 'Kareadita:develop' into JPEG-XL_JPG2000_HEIF
maxpiva Aug 29, 2024
e7f021a
Edit CodeStyle
maxpiva Aug 29, 2024
c624287
Remove custom docker builder
maxpiva Aug 29, 2024
6dbeaf1
Fixed discovered issues.
maxpiva Aug 30, 2024
fead3f6
Merge branch 'Kareadita:develop' into JPEG-XL_JPG2000_HEIF
maxpiva Sep 19, 2024
bf30989
Merge branch 'Kareadita:develop' into JPEG-XL_JPG2000_HEIF
maxpiva Sep 27, 2024
0cc4803
Removed NetVips and ImageSharp (Use only ImageMagick)
maxpiva Sep 28, 2024
10da91c
Remove CropFromDimensions (Geometry will take care)
maxpiva Sep 28, 2024
1541d6a
BOM Changes
maxpiva Sep 28, 2024
23a9aa8
Delete .bak
maxpiva Sep 28, 2024
b6a986a
Bug fixes.
maxpiva Sep 29, 2024
3008ae0
Merge branch 'Kareadita:develop' into RemoveSixLaborsAndNetVips
maxpiva Sep 29, 2024
9bf1fa8
Added SmartCrop (port from smartcrop.js)
maxpiva Oct 1, 2024
170c3b6
Refactor and cleanup.
maxpiva Oct 4, 2024
361c464
BOM Fixes
maxpiva Oct 4, 2024
d6056b8
Update docker-build.sh
maxpiva Oct 4, 2024
92f131a
Fixed Benchmarks
maxpiva Oct 4, 2024
b21ee66
Merge branch 'RemoveSixLaborsAndNetVips' of https://github.com/maxpiv…
maxpiva Oct 4, 2024
4f026e6
Fixed BOM
maxpiva Oct 4, 2024
00ee60b
Requested changes.
maxpiva Oct 10, 2024
fbbbbb8
Removed bak files, fixed EncodeFormat BOM
maxpiva Oct 10, 2024
a0f804b
Removed quality parameter everywhere.
maxpiva Oct 11, 2024
11749bd
Remove quality parameters after refactor.
maxpiva Oct 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions API.Benchmark/ArchiveServiceBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ArchiveServiceBenchmark
private readonly IImageService _imageService;
private readonly IImageFactory _imageFactory;
private const string SourceImage = "Data/comic-normal.jpg";


public ArchiveServiceBenchmark()
{
Expand Down Expand Up @@ -71,7 +71,7 @@ public void ImageMagick_ExtractImage_PNG()
int width = 320;
int height = (int)(thumbnail2.Height * (width / (double)thumbnail2.Width));
thumbnail2.Thumbnail(width, height);
thumbnail2.Save(_directoryService.FileSystem.Path.Join(outputDirectory, "imagesharp.png"), EncodeFormat.PNG, 100);
thumbnail2.Save(_directoryService.FileSystem.Path.Join(outputDirectory, "imagesharp.png"), EncodeFormat.PNG);
}

[Benchmark]
Expand All @@ -85,7 +85,7 @@ public void ImageMagick_ExtractImage_WebP()
int width = 320;
int height = (int)(thumbnail2.Height * (width / (double)thumbnail2.Width));
thumbnail2.Thumbnail(width, height);
thumbnail2.Save(_directoryService.FileSystem.Path.Join(outputDirectory, "imagesharp.webp"), EncodeFormat.PNG, 100);
thumbnail2.Save(_directoryService.FileSystem.Path.Join(outputDirectory, "imagesharp.webp"), EncodeFormat.PNG);
}


Expand Down
2 changes: 1 addition & 1 deletion API.Tests/Services/ArchiveServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void GetCoverImage_Default_Test(string inputFile, string expectedOutputFi
int height = (int)(thumbnail.Height * (width / (double)thumbnail.Width));
thumbnail.Thumbnail(width, height);
using MemoryStream stream = new MemoryStream();
thumbnail.Save(stream, EncodeFormat.PNG, 100);
thumbnail.Save(stream, EncodeFormat.PNG);
var expectedBytes = stream.ToArray();

archiveService.Configure().CanOpen(Path.Join(testDirectory, inputFile)).Returns(ArchiveLibrary.Default);
Expand Down
4 changes: 2 additions & 2 deletions API.Tests/Services/ImageServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void GenerateFiles(string outputExtension)
var thumbnail = factory.Create(imagePath);
thumbnail = ImageService.Thumbnail(thumbnail, dims.Width, dims.Height);
var outputFileName = fileName + outputExtension + ".png";
thumbnail.Save(Path.Join(_testDirectory, outputFileName), EncodeFormat.PNG,100);
thumbnail.Save(Path.Join(_testDirectory, outputFileName), EncodeFormat.PNG);
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ private static void GenerateColorImage(string hexColor, string outputPath)
ImageMagickImageFactory factory = new ImageMagickImageFactory();
var color = ImageService.HexToRgb(hexColor);
using var colorImage = factory.Create(200,100,color.R, color.G, color.B);
colorImage.Save(outputPath, EncodeFormat.PNG, 100);
colorImage.Save(outputPath, EncodeFormat.PNG);
}

private void GenerateHtmlFileForColorScape()
Expand Down
12 changes: 12 additions & 0 deletions API/Extensions/EncodeFormatExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ public static string GetExtension(this EncodeFormat encodeFormat)
_ => throw new ArgumentOutOfRangeException(nameof(encodeFormat), encodeFormat, null)
};
}

public static int DefaultQuality(this EncodeFormat encodeFormat)
{
return encodeFormat switch
{
EncodeFormat.PNG => 100, // (Image Magick Maximum Deflate Compression) (In case of PNG, png is always lossless, Quality indicate the compression level)
EncodeFormat.WEBP => 100,
EncodeFormat.AVIF => 100,
EncodeFormat.JPEG => 99, // (Best Compression speed, with almost no visual quality loss)
_ => throw new ArgumentOutOfRangeException(nameof(encodeFormat), encodeFormat, null)
};
}
}
2 changes: 1 addition & 1 deletion API/Services/BookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ private void GetPdfPage(IDocReader docReader, int pageNumber, Stream stream)
var height = pageReader.GetPageHeight();
IImage image = _imageService.ImageFactory.CreateFromBGRAByteArray(rawBytes, width, height);
stream.Seek(0, SeekOrigin.Begin);
image.Save(stream, EncodeFormat.PNG, 100);
image.Save(stream, EncodeFormat.PNG);
stream.Seek(0, SeekOrigin.Begin);
}

Expand Down
63 changes: 27 additions & 36 deletions API/Services/ImageService.cs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions API/Services/ImageServices/IImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public interface IImage : IDisposable
/// <param name="filename">The name of the file to save the image to.</param>
/// <param name="format">The format to save the image in.</param>
/// <param name="quality">The quality of the saved image.</param>
majora2007 marked this conversation as resolved.
Show resolved Hide resolved
void Save(string filename, EncodeFormat format, int quality);
void Save(string filename, EncodeFormat format);

/// <summary>
/// Saves the image to the specified stream with the specified format and quality.
/// </summary>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="format">The format to save the image in.</param>
/// <param name="quality">The quality of the saved image.</param>
void Save(Stream stream, EncodeFormat format, int quality);
void Save(Stream stream, EncodeFormat format);

/// <summary>
/// Asynchronously saves the image to the specified file with the specified format and quality.
Expand All @@ -82,7 +82,7 @@ public interface IImage : IDisposable
/// <param name="quality">The quality of the saved image.</param>
/// <param name="token">The cancellation token.</param>
/// <returns>A task representing the asynchronous save operation.</returns>
Task SaveAsync(string filename, EncodeFormat format, int quality, CancellationToken token = default);
Task SaveAsync(string filename, EncodeFormat format, CancellationToken token = default);

/// <summary>
/// Asynchronously saves the image to the specified stream with the specified format and quality.
Expand All @@ -92,7 +92,7 @@ public interface IImage : IDisposable
/// <param name="quality">The quality of the saved image.</param>
/// <param name="token">The cancellation token.</param>
/// <returns>A task representing the asynchronous save operation.</returns>
Task SaveAsync(Stream stream, EncodeFormat format, int quality, CancellationToken token = default);
Task SaveAsync(Stream stream, EncodeFormat format, CancellationToken token = default);

/// <summary>
/// Gets the RGBA image data as an array of floats.
Expand Down
17 changes: 9 additions & 8 deletions API/Services/ImageServices/ImageMagick/ImageMagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using System.Threading.Tasks;
using API.Entities.Enums;
using API.Extensions;
using ImageMagick;

namespace API.Services.ImageServices.ImageMagick;
Expand Down Expand Up @@ -139,30 +140,30 @@ public void Composite(IImage overlay, int x, int y)
}

/// <inheritdoc/>
public void Save(string filename, EncodeFormat format, int quality)
public void Save(string filename, EncodeFormat format)
{
_image.Quality = quality;
_image.Quality = format.DefaultQuality();
_image.Write(filename, MagickFormatFromEncodeFormat(format));
}

/// <inheritdoc/>
public void Save(Stream stream, EncodeFormat format, int quality)
public void Save(Stream stream, EncodeFormat format)
{
_image.Quality = quality;
_image.Quality = format.DefaultQuality();
_image.Write(stream, MagickFormatFromEncodeFormat(format));
}

/// <inheritdoc/>
public Task SaveAsync(string filename, EncodeFormat format, int quality, CancellationToken token = default)
public Task SaveAsync(string filename, EncodeFormat format, CancellationToken token = default)
{
_image.Quality = quality;
_image.Quality = format.DefaultQuality();
return _image.WriteAsync(filename, MagickFormatFromEncodeFormat(format), token);
}

/// <inheritdoc/>
public Task SaveAsync(Stream stream, EncodeFormat format, int quality, CancellationToken token = default)
public Task SaveAsync(Stream stream, EncodeFormat format, CancellationToken token = default)
{
_image.Quality = quality;
_image.Quality = format.DefaultQuality();
return _image.WriteAsync(stream, MagickFormatFromEncodeFormat(format), token);
}

Expand Down