Skip to content

Commit

Permalink
Png image exportation completed for 24 and 36 image formats
Browse files Browse the repository at this point in the history
  • Loading branch information
caraplana1 committed Aug 3, 2024
1 parent 84d77bd commit 84164e4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
12 changes: 4 additions & 8 deletions ComicConverter/ComicBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@ public static void CreatePdf(string[] imagesPaths, string fileName)

PdfDocument document = new();
document.Info.Title = fileName.Replace('\\', '/').Split('/').Last();
PdfPage page;
XGraphics graphics;
XImage imageFile;

foreach (var image in imagesPaths)
{
imageFile = XImage.FromFile(image);
var imageFile = XImage.FromFile(image);

page = document.AddPage();
var page = document.AddPage();
page.MediaBox = new PdfRectangle(new XPoint(0, 0), (XPoint)imageFile.Size);

graphics = XGraphics.FromPdfPage(page);
var graphics = XGraphics.FromPdfPage(page);
graphics.DrawImage(imageFile, 0, 0);
}

Expand All @@ -94,11 +91,10 @@ private static DirectoryInfo CreateHiddenDir(string[] filesPaths, string dirName
throw new IOException("There is no file to add");

filesPaths = filesPaths.Where(f => File.Exists(f)).ToArray();
FileInfo fileInfo;

foreach (var image in filesPaths)
{
fileInfo = new(image);
FileInfo fileInfo = new(image);
File.Copy(fileInfo.FullName, $"{dirName}/{fileInfo.Name}", true);
}

Expand Down
63 changes: 47 additions & 16 deletions ComicConverter/ImageExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using SharpCompress.Archives.Rar;
using SharpCompress.Archives.Zip;
using SharpCompress.Archives.Tar;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace ComicConverter
{
Expand Down Expand Up @@ -116,7 +116,7 @@ public static void ExportPdfImages(string filePath, string outputDir = ".")
if (!File.Exists(filePath))
throw new FileNotFoundException();

if (!IsValidPDF(filePath) || outputDir?.Length == 0)
if (!IsValidPdf(filePath) || outputDir?.Length == 0 || outputDir == null)
throw new FormatException();

DirectoryInfo dir = Directory.CreateDirectory(outputDir);
Expand Down Expand Up @@ -146,7 +146,7 @@ public static void ExportPdfImages(string filePath, string outputDir = ".")
/// Verify if the comic file PDF encrypted
/// </summary>
/// <returns>True if it is a valid pdf, false otherwise</returns>
private static bool IsValidPDF(string path)
private static bool IsValidPdf(string path)
{
StreamReader file = new(path);
var firstLine = file.ReadLine()?.Substring(0, 8);
Expand Down Expand Up @@ -208,10 +208,9 @@ private static void ExportPngImage(PdfDictionary image, string name)
{
PdfSharpCore.Pdf.Filters.FlateDecode flate = new PdfSharpCore.Pdf.Filters.FlateDecode();
decodedBytes = flate.Decode(image.Stream.Value, image);
}
}

int bitsPerComponent = decodedBytes.Length * 8 / (width * height);


SKColorType colorType = bitsPerComponent switch
{
Expand All @@ -223,24 +222,56 @@ private static void ExportPngImage(PdfDictionary image, string name)
_ => throw new Exception("Unknown Pixel format " + bitsPerComponent),
};

SKBitmap bitmap = new SKBitmap(width, height, colorType, bitsPerComponent <= 24 ? SKAlphaType.Opaque : SKAlphaType.Premul);
var pixels = bitmap.GetPixels();
SKBitmap bitmap = new SKBitmap(width, height, colorType,
bitsPerComponent <= 24 ? SKAlphaType.Opaque : SKAlphaType.Premul);

// ReSharper disable once PossibleLossOfFraction
// the loss of fraction is not possible due to bitsPerComponent is multiple of 8.
int length = (int)Math.Ceiling((decimal)width * (bitsPerComponent / 8));
List<SKColor> colors = new List<SKColor>();

for (int i = 0; i < height; i++)
switch (bitsPerComponent)
{
var offset = i * length;
Int64 scanOffset = i * bitmap.RowBytes;
Marshal.Copy(decodedBytes, offset, new IntPtr(bitmap.GetAddress(0, 0).ToInt64() + scanOffset), length);
case 1:
throw new NotImplementedException("1-bit color png not supported");
case 8:
throw new NotImplementedException("8-bit color png not supported");
case 16:
throw new NotImplementedException("16-bit color png not supported");
case 24:
{
for (int i = 0; i < height * width; i++)
{
SKColor color = new SKColor(
decodedBytes[i * 3],
decodedBytes[i * 3 + 1],
decodedBytes[i * 3 + 2]
);
colors.Add(color);
}

break;
}
default:
{
for (int i = 0; i < height * width; i++)
{
SKColor color = new SKColor(
decodedBytes[i * 4],
decodedBytes[i * 4 + 1],
decodedBytes[i * 4 + 2],
decodedBytes[i * 4 + 3]
);
colors.Add(color);
}

break;
}
}

bitmap.Pixels = colors.ToArray();

using var imagePng = SKImage.FromBitmap(bitmap);
using var data = imagePng.Encode(SKEncodedImageFormat.Png, 100);
using var filestream = File.OpenWrite($"{name}.png");
data.SaveTo(filestream);
}
}
}
}

0 comments on commit 84164e4

Please sign in to comment.