mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-08-20 08:49:11 +08:00
#710 set colorspace based on jpeg info
This commit is contained in:
parent
83519b27b1
commit
c061603ad2
@ -29,7 +29,6 @@
|
||||
switch (marker)
|
||||
{
|
||||
case JpegMarker.StartOfImage:
|
||||
case JpegMarker.EndOfImage:
|
||||
case JpegMarker.Restart0:
|
||||
case JpegMarker.Restart1:
|
||||
case JpegMarker.Restart2:
|
||||
@ -49,8 +48,9 @@
|
||||
var bpp = stream.ReadByte();
|
||||
var height = ReadShort(stream, shortBuffer);
|
||||
var width = ReadShort(stream, shortBuffer);
|
||||
var numberOfComponents = stream.ReadByte();
|
||||
|
||||
return new JpegInformation(width, height, bpp);
|
||||
return new JpegInformation(width, height, bpp, numberOfComponents);
|
||||
}
|
||||
case JpegMarker.ApplicationSpecific0:
|
||||
case JpegMarker.ApplicationSpecific1:
|
||||
|
||||
@ -20,14 +20,20 @@
|
||||
/// </summary>
|
||||
public int BitsPerComponent { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 1 grayscale, 3 RGB, 4 CMYK.
|
||||
/// </summary>
|
||||
public int NumberOfComponents { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="JpegInformation"/>.
|
||||
/// </summary>
|
||||
public JpegInformation(int width, int height, int bitsPerComponent)
|
||||
public JpegInformation(int width, int height, int bitsPerComponent, int numberOfComponents)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
BitsPerComponent = bitsPerComponent;
|
||||
NumberOfComponents = numberOfComponents;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -21,13 +21,13 @@
|
||||
using Tokens;
|
||||
using Graphics.Operations.PathPainting;
|
||||
using Images.Png;
|
||||
using UglyToad.PdfPig.Actions;
|
||||
using Actions;
|
||||
|
||||
internal class NameConflictSolver
|
||||
{
|
||||
private string prefix;
|
||||
private readonly string prefix;
|
||||
private int key = 0;
|
||||
private HashSet<string> xobjectNamesUsed = new HashSet<string>();
|
||||
private readonly HashSet<string> xobjectNamesUsed = new HashSet<string>();
|
||||
|
||||
public NameConflictSolver(string prefix)
|
||||
{
|
||||
@ -45,7 +45,7 @@
|
||||
}
|
||||
|
||||
|
||||
return i != 0 ? name.Substring(0,i) : prefix;
|
||||
return i != 0 ? name.Substring(0, i) : prefix;
|
||||
}
|
||||
|
||||
public string NewName(string orginalName = null)
|
||||
@ -53,11 +53,14 @@
|
||||
var newPrefix = ExtractPrefix(orginalName);
|
||||
|
||||
var name = $"{newPrefix}{key}";
|
||||
|
||||
while (xobjectNamesUsed.Contains(name))
|
||||
{
|
||||
name = $"{newPrefix}{++key}";
|
||||
}
|
||||
|
||||
xobjectNamesUsed.Add(name);
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
@ -67,12 +70,10 @@
|
||||
{
|
||||
return NewName(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
xobjectNamesUsed.Add(name);
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -135,7 +136,7 @@
|
||||
PageNumber = number;
|
||||
|
||||
currentStream = new DefaultContentStream();
|
||||
contentStreams = new List<IPageContentStream>() {currentStream};
|
||||
contentStreams = new List<IPageContentStream>() { currentStream };
|
||||
}
|
||||
|
||||
internal PdfPageBuilder(int number, PdfDocumentBuilder documentBuilder, IEnumerable<CopiedContentStream> copied,
|
||||
@ -587,7 +588,13 @@
|
||||
var info = JpegHandler.GetInformation(fileStream);
|
||||
|
||||
if (placementRectangle.Equals(default(PdfRectangle)))
|
||||
placementRectangle = new PdfRectangle(0, 0, info.Width, info.Height);
|
||||
{
|
||||
placementRectangle = new PdfRectangle(
|
||||
0,
|
||||
0,
|
||||
info.Width,
|
||||
info.Height);
|
||||
}
|
||||
|
||||
byte[] data;
|
||||
using (var memory = new MemoryStream())
|
||||
@ -597,6 +604,20 @@
|
||||
data = memory.ToArray();
|
||||
}
|
||||
|
||||
NameToken colorSpace;
|
||||
if (info.NumberOfComponents == 1)
|
||||
{
|
||||
colorSpace = NameToken.Devicegray;
|
||||
}
|
||||
else if (info.NumberOfComponents == 4)
|
||||
{
|
||||
colorSpace = NameToken.Devicecmyk;
|
||||
}
|
||||
else
|
||||
{
|
||||
colorSpace = NameToken.Devicergb;
|
||||
}
|
||||
|
||||
var imgDictionary = new Dictionary<NameToken, IToken>
|
||||
{
|
||||
{NameToken.Type, NameToken.Xobject },
|
||||
@ -604,7 +625,7 @@
|
||||
{NameToken.Width, new NumericToken(info.Width) },
|
||||
{NameToken.Height, new NumericToken(info.Height) },
|
||||
{NameToken.BitsPerComponent, new NumericToken(info.BitsPerComponent)},
|
||||
{NameToken.ColorSpace, NameToken.Devicergb},
|
||||
{NameToken.ColorSpace, colorSpace},
|
||||
{NameToken.Filter, NameToken.DctDecode},
|
||||
{NameToken.Length, new NumericToken(data.Length)}
|
||||
};
|
||||
@ -613,12 +634,12 @@
|
||||
var resources = pageDictionary.GetOrCreateDict(NameToken.Resources);
|
||||
var xObjects = resources.GetOrCreateDict(NameToken.Xobject);
|
||||
|
||||
var key = NameToken.Create( xobjectsNames.NewName());
|
||||
var key = NameToken.Create(xobjectsNames.NewName());
|
||||
xObjects[key] = reference;
|
||||
|
||||
currentStream.Add(Push.Value);
|
||||
// This needs to be the placement rectangle.
|
||||
currentStream.Add(new ModifyCurrentTransformationMatrix(new []
|
||||
currentStream.Add(new ModifyCurrentTransformationMatrix(new[]
|
||||
{
|
||||
(decimal)placementRectangle.Width, 0,
|
||||
0, (decimal)placementRectangle.Height,
|
||||
@ -839,7 +860,7 @@
|
||||
|
||||
// Special cases
|
||||
// Since we don't directly add font's to the pages resources, we have to go look at the document's font
|
||||
if(srcResourceDictionary.TryGet(NameToken.Font, srcPage.pdfScanner, out DictionaryToken fontsDictionary))
|
||||
if (srcResourceDictionary.TryGet(NameToken.Font, srcPage.pdfScanner, out DictionaryToken fontsDictionary))
|
||||
{
|
||||
var pageFontsDictionary = resources.GetOrCreateDict(NameToken.Font);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user