Skip to content

Commit

Permalink
Fixed issue with unpremultiplied alpha.
Browse files Browse the repository at this point in the history
  • Loading branch information
arklumpus committed Jan 11, 2021
1 parent ff571df commit aea60b4
Show file tree
Hide file tree
Showing 89 changed files with 1,023 additions and 937 deletions.
2 changes: 1 addition & 1 deletion MuPDFCore/MuPDFCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Description>MuPDFCore is a set of multiplatform .NET Core bindings for MuPDF. It can render PDF, XPS, EPUB and other formats to raster images returned either as raw bytes, or as image files in multiple formats (including PNG and PSD). It also supports multithreading.</Description>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
<PackageIcon>icon.png</PackageIcon>
<Authors>Giorgio Bianchini</Authors>
<Company>University of Bristol</Company>
Expand Down
13 changes: 10 additions & 3 deletions MuPDFCore/MuPDFDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,22 @@ public void Render(int pageNumber, Rectangle region, double zoom, PixelFormats p
throw new MuPDFException("Unknown error", result);
}

RoundedRectangle roundedRegion = region.Round(fzoom);
RoundedSize roundedSize = new RoundedSize(roundedRegion.Width, roundedRegion.Height);

if (pixelFormat == PixelFormats.RGBA || pixelFormat == PixelFormats.BGRA)
{
Utils.UnpremultiplyAlpha(destination, roundedSize);
}

if (this.ClipToPageBounds && !Pages[pageNumber].Bounds.Contains(DisplayLists[pageNumber].Bounds.Intersect(region)))
{
RoundedRectangle roundedRegion = region.Round(fzoom);
Utils.ClipImage(destination, new RoundedSize(roundedRegion.Width, roundedRegion.Height), region, Pages[pageNumber].Bounds, pixelFormat);
Utils.ClipImage(destination, roundedSize, region, Pages[pageNumber].Bounds, pixelFormat);
}
}

/// <summary>
/// Render a page the specified destination.
/// Render a page to the specified destination.
/// </summary>
/// <param name="pageNumber">The number of the page to render (starting at 0).</param>
/// <param name="zoom">The scale at which the page will be rendered. This will determine the size in pixel of the image.</param>
Expand Down
12 changes: 10 additions & 2 deletions MuPDFCore/MuPDFMultiThreadedPageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,18 @@ private void RenderAction()
throw new MuPDFException("Unknown error", result);
}

RoundedRectangle roundedRegion = this.CurrentRenderData.Region.Round(this.CurrentRenderData.Zoom);
RoundedSize roundedSize = new RoundedSize(roundedRegion.Width, roundedRegion.Height);

if (this.CurrentRenderData.PixelFormat == PixelFormats.RGBA || this.CurrentRenderData.PixelFormat == PixelFormats.BGRA)
{
Utils.UnpremultiplyAlpha(this.CurrentRenderData.PixelStorage, roundedSize);
}

if (this.CurrentRenderData.ClipToPageBounds && !this.CurrentRenderData.PageBounds.Contains(this.CurrentRenderData.DisplayList.Bounds.Intersect(this.CurrentRenderData.Region)))
{
RoundedRectangle roundedRegion = this.CurrentRenderData.Region.Round(this.CurrentRenderData.Zoom);
Utils.ClipImage(this.CurrentRenderData.PixelStorage, new RoundedSize(roundedRegion.Width, roundedRegion.Height), this.CurrentRenderData.Region, this.CurrentRenderData.PageBounds, this.CurrentRenderData.PixelFormat);

Utils.ClipImage(this.CurrentRenderData.PixelStorage, roundedSize, this.CurrentRenderData.Region, this.CurrentRenderData.PageBounds, this.CurrentRenderData.PixelFormat);
}
}

Expand Down
30 changes: 29 additions & 1 deletion MuPDFCore/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static bool IsAcceptableNumber(int n)
if (n == 0)
{
return false;
}
}
else if (n == 1)
{
return true;
Expand Down Expand Up @@ -159,5 +159,33 @@ public static void ClipImage(IntPtr image, RoundedSize imageSize, Rectangle imag
}
}
}

/// <summary>
/// Converts an image with premultiplied alpha values into an image with unpremultiplied alpha values.
/// </summary>
/// <param name="image">A pointer to the address where the pixel data is stored.</param>
/// <param name="imageSize">The size in pixels of the image.</param>
public static void UnpremultiplyAlpha(IntPtr image, RoundedSize imageSize)
{
int stride = imageSize.Width * 4;

unsafe
{
byte* imageData = (byte*)image;

for (int y = 0; y < imageSize.Height; y++)
{
for (int x = 0; x < imageSize.Width; x++)
{
if (imageData[y * stride + x * 4 + 3] > 0)
{
imageData[y * stride + x * 4] = (byte)(imageData[y * stride + x * 4] * 255 / imageData[y * stride + x * 4 + 3]);
imageData[y * stride + x * 4 + 1] = (byte)(imageData[y * stride + x * 4 + 1] * 255 / imageData[y * stride + x * 4 + 3]);
imageData[y * stride + x * 4 + 2] = (byte)(imageData[y * stride + x * 4 + 2] * 255 / imageData[y * stride + x * 4 + 3]);
}
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ For convenience, these compiled files for MuPDF 1.17.0 are included in the [`nat

### 2. Building MuPDFWrapper

Once you have the required static library files, you should download the MuPDFCore source code: [MuPDFCore-1.2.0.tar.gz](https://github.com/arklumpus/MuPDFCore/archive/v1.2.0.tar.gz) (or clone the repository) and place the library files in the appropriate subdirectories in the `native/MuPDFWrapper/lib/` folder.
Once you have the required static library files, you should download the MuPDFCore source code: [MuPDFCore-1.2.1.tar.gz](https://github.com/arklumpus/MuPDFCore/archive/v1.2.1.tar.gz) (or clone the repository) and place the library files in the appropriate subdirectories in the `native/MuPDFWrapper/lib/` folder.

To compile `MuPDFWrapper` you will need [CMake](https://cmake.org/) and (on Windows) [Ninja](https://ninja-build.org/).

Expand Down
Binary file modified docs/MuPDFCore.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/_mu_p_d_f_8cs_source.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<td id="projectlogo"><img alt="Logo" src="icon.svg"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">MuPDFCore
&#160;<span id="projectnumber">1.2.0</span>
&#160;<span id="projectnumber">1.2.1</span>
</div>
<div id="projectbrief">Multiplatform .NET Core bindings for MuPDF</div>
</td>
Expand Down
2 changes: 1 addition & 1 deletion docs/_mu_p_d_f_context_8cs_source.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<td id="projectlogo"><img alt="Logo" src="icon.svg"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">MuPDFCore
&#160;<span id="projectnumber">1.2.0</span>
&#160;<span id="projectnumber">1.2.1</span>
</div>
<div id="projectbrief">Multiplatform .NET Core bindings for MuPDF</div>
</td>
Expand Down
2 changes: 1 addition & 1 deletion docs/_mu_p_d_f_display_list_8cs_source.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<td id="projectlogo"><img alt="Logo" src="icon.svg"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">MuPDFCore
&#160;<span id="projectnumber">1.2.0</span>
&#160;<span id="projectnumber">1.2.1</span>
</div>
<div id="projectbrief">Multiplatform .NET Core bindings for MuPDF</div>
</td>
Expand Down
Loading

0 comments on commit aea60b4

Please sign in to comment.