Skip to content

Commit 2845f72

Browse files
introfogiText-CI
authored andcommitted
Add logging that ICC profile to be set to the image is invalid
DEVSIX-5045 Autoported commit. Original commit hash: [aa44e3692]
1 parent 49ed6a7 commit 2845f72

File tree

5 files changed

+104
-34
lines changed

5 files changed

+104
-34
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using System;
24+
using System.IO;
25+
using iText.IO.Util;
26+
using iText.Test;
27+
using iText.Test.Attributes;
28+
29+
namespace iText.IO.Image {
30+
public class JpegImageHelperTest : ExtendedITextTest {
31+
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
32+
.CurrentContext.TestDirectory) + "/resources/itext/io/image/";
33+
34+
[NUnit.Framework.Test]
35+
[LogMessage(iText.IO.LogMessageConstant.DURING_CONSTRUCTION_OF_ICC_PROFILE_ERROR_OCCURRED, LogLevel = LogLevelConstants
36+
.ERROR)]
37+
public virtual void AttemptToSetInvalidIccProfileToImageTest() {
38+
using (FileStream fis = new FileStream(SOURCE_FOLDER + "WP_20140410_001.jpg", FileMode.Open, FileAccess.Read
39+
)) {
40+
ImageData img = ImageDataFactory.CreateJpeg(StreamUtil.InputStreamToArray(fis));
41+
int size = 100;
42+
// Instantiate new byte[size][] instead new byte[size][size] necessary for autoporting
43+
byte[][] icc = new byte[size][];
44+
for (int i = 0; i < size; i++) {
45+
icc[i] = new byte[size];
46+
for (int j = 0; j < size; j++) {
47+
icc[i][j] = (byte)j;
48+
}
49+
}
50+
NUnit.Framework.Assert.DoesNotThrow(() => JpegImageHelper.AttemptToSetIccProfileToImage(icc, img));
51+
}
52+
}
53+
54+
[NUnit.Framework.Test]
55+
public virtual void AttemptToSetNullIccProfileToImageTest() {
56+
using (FileStream fis = new FileStream(SOURCE_FOLDER + "WP_20140410_001.jpg", FileMode.Open, FileAccess.Read
57+
)) {
58+
byte[][] icc = new byte[][] { null, null };
59+
ImageData img = ImageDataFactory.CreateJpeg(StreamUtil.InputStreamToArray(fis));
60+
NUnit.Framework.Assert.DoesNotThrow(() => JpegImageHelper.AttemptToSetIccProfileToImage(icc, img));
61+
}
62+
}
63+
}
64+
}

itext.tests/itext.io.tests/itext/io/image/JpegTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ source product.
4747

4848
namespace iText.IO.Image {
4949
public class JpegTest : ExtendedITextTest {
50-
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
50+
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
5151
.CurrentContext.TestDirectory) + "/resources/itext/io/image/";
5252

5353
[NUnit.Framework.Test]
5454
public virtual void OpenJpeg1() {
55-
using (FileStream fis = new FileStream(sourceFolder + "WP_20140410_001.jpg", FileMode.Open, FileAccess.Read
55+
using (FileStream fis = new FileStream(SOURCE_FOLDER + "WP_20140410_001.jpg", FileMode.Open, FileAccess.Read
5656
)) {
5757
// Test this a more specific entry point
5858
ImageData img = ImageDataFactory.CreateJpeg(StreamUtil.InputStreamToArray(fis));
@@ -65,15 +65,15 @@ public virtual void OpenJpeg1() {
6565
[NUnit.Framework.Test]
6666
public virtual void OpenJpeg2() {
6767
// Test this a more specific entry point
68-
ImageData img = ImageDataFactory.CreateJpeg(UrlUtil.ToURL(sourceFolder + "WP_20140410_001_gray.jpg"));
68+
ImageData img = ImageDataFactory.CreateJpeg(UrlUtil.ToURL(SOURCE_FOLDER + "WP_20140410_001_gray.jpg"));
6969
NUnit.Framework.Assert.AreEqual(2592, img.GetWidth(), 0);
7070
NUnit.Framework.Assert.AreEqual(1456, img.GetHeight(), 0);
7171
NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
7272
}
7373

7474
[NUnit.Framework.Test]
7575
public virtual void OpenJpeg3() {
76-
using (FileStream fis = new FileStream(sourceFolder + "WP_20140410_001_monochrome.jpg", FileMode.Open, FileAccess.Read
76+
using (FileStream fis = new FileStream(SOURCE_FOLDER + "WP_20140410_001_monochrome.jpg", FileMode.Open, FileAccess.Read
7777
)) {
7878
// Test this a more specific entry point
7979
ImageData img = ImageDataFactory.Create(StreamUtil.InputStreamToArray(fis));
@@ -85,23 +85,23 @@ public virtual void OpenJpeg3() {
8585

8686
[NUnit.Framework.Test]
8787
public virtual void OpenJpeg4() {
88-
ImageData img = ImageDataFactory.Create(sourceFolder + "WP_20140410_001_negate.jpg");
88+
ImageData img = ImageDataFactory.Create(SOURCE_FOLDER + "WP_20140410_001_negate.jpg");
8989
NUnit.Framework.Assert.AreEqual(2592, img.GetWidth(), 0);
9090
NUnit.Framework.Assert.AreEqual(1456, img.GetHeight(), 0);
9191
NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
9292
}
9393

9494
[NUnit.Framework.Test]
9595
public virtual void OpenJpeg5() {
96-
ImageData img = ImageDataFactory.Create(sourceFolder + "WP_20140410_001_year1900.jpg");
96+
ImageData img = ImageDataFactory.Create(SOURCE_FOLDER + "WP_20140410_001_year1900.jpg");
9797
NUnit.Framework.Assert.AreEqual(2592, img.GetWidth(), 0);
9898
NUnit.Framework.Assert.AreEqual(1456, img.GetHeight(), 0);
9999
NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
100100
}
101101

102102
[NUnit.Framework.Test]
103103
public virtual void OpenJpeg6() {
104-
ImageData img = ImageDataFactory.Create(sourceFolder + "WP_20140410_001_year1980.jpg");
104+
ImageData img = ImageDataFactory.Create(SOURCE_FOLDER + "WP_20140410_001_year1980.jpg");
105105
NUnit.Framework.Assert.AreEqual(2592, img.GetWidth(), 0);
106106
NUnit.Framework.Assert.AreEqual(1456, img.GetHeight(), 0);
107107
NUnit.Framework.Assert.AreEqual(8, img.GetBpc());

itext/itext.io/itext/io/LogMessageConstant.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public sealed class LogMessageConstant {
116116

117117
public const String DOCUMENT_SERIALIZATION_EXCEPTION_RAISED = "Unhandled exception while serialization";
118118

119+
public const String DURING_CONSTRUCTION_OF_ICC_PROFILE_ERROR_OCCURRED = "During the construction of the ICC profile, the {0} error with message \"{1}\" occurred, the ICC profile will not be installed in the image.";
120+
119121
public const String ELEMENT_DOES_NOT_FIT_AREA = "Element does not fit current area. {0}";
120122

121123
public const String ELEMENT_WAS_FORCE_PLACED_KEEP_WITH_NEXT_WILL_BE_IGNORED = "Element was placed in a forced way. Keep with next property will be ignored";

itext/itext.io/itext/io/image/JpegImageHelper.cs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ source product.
5050

5151
namespace iText.IO.Image {
5252
internal class JpegImageHelper {
53+
private static readonly ILog LOGGER = LogManager.GetLogger(typeof(JpegImageHelper));
54+
5355
/// <summary>This is a type of marker.</summary>
5456
private const int NOT_A_MARKER = -1;
5557

@@ -130,6 +132,31 @@ public static void ProcessImage(ImageData image) {
130132
UpdateAttributes(image);
131133
}
132134

135+
internal static void AttemptToSetIccProfileToImage(byte[][] icc, ImageData image) {
136+
if (icc != null) {
137+
int total = 0;
138+
foreach (byte[] value in icc) {
139+
if (value == null) {
140+
return;
141+
}
142+
total += value.Length - 14;
143+
}
144+
byte[] ficc = new byte[total];
145+
total = 0;
146+
foreach (byte[] bytes in icc) {
147+
Array.Copy(bytes, 14, ficc, total, bytes.Length - 14);
148+
total += bytes.Length - 14;
149+
}
150+
try {
151+
image.SetProfile(IccProfile.GetInstance(ficc, image.GetColorSpace()));
152+
}
153+
catch (Exception e) {
154+
LOGGER.Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.DURING_CONSTRUCTION_OF_ICC_PROFILE_ERROR_OCCURRED
155+
, e.GetType().Name, e.Message));
156+
}
157+
}
158+
}
159+
133160
private static void UpdateAttributes(ImageData image) {
134161
image.filter = "DCTDecode";
135162
if (image.GetColorTransform() == 0) {
@@ -295,8 +322,7 @@ private static void ProcessParameters(Stream jpegStream, String errorID, ImageDa
295322
dx = (unitsx == 2 ? (int)(dx * 2.54f + 0.5f) : dx);
296323
// make sure this is consistent with JFIF data
297324
if (image.GetDpiX() != 0 && image.GetDpiX() != dx) {
298-
ILog logger = LogManager.GetLogger(typeof(JpegImageHelper));
299-
logger.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiX: {0} vs {1})", image.GetDpiX(), dx));
325+
LOGGER.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiX: {0} vs {1})", image.GetDpiX(), dx));
300326
}
301327
else {
302328
image.SetDpi(dx, image.GetDpiY());
@@ -306,8 +332,7 @@ private static void ProcessParameters(Stream jpegStream, String errorID, ImageDa
306332
dy = (unitsy == 2 ? (int)(dy * 2.54f + 0.5f) : dy);
307333
// make sure this is consistent with JFIF data
308334
if (image.GetDpiY() != 0 && image.GetDpiY() != dy) {
309-
ILog logger = LogManager.GetLogger(typeof(JpegImageHelper));
310-
logger.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiY: {0} vs {1})", image.GetDpiY(), dy));
335+
LOGGER.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiY: {0} vs {1})", image.GetDpiY(), dy));
311336
}
312337
else {
313338
image.SetDpi(image.GetDpiX(), dx);
@@ -342,30 +367,9 @@ private static void ProcessParameters(Stream jpegStream, String errorID, ImageDa
342367
}
343368
}
344369
}
345-
if (icc != null) {
346-
int total = 0;
347-
for (int k = 0; k < icc.Length; ++k) {
348-
if (icc[k] == null) {
349-
icc = null;
350-
return;
351-
}
352-
total += icc[k].Length - 14;
353-
}
354-
byte[] ficc = new byte[total];
355-
total = 0;
356-
for (int k = 0; k < icc.Length; ++k) {
357-
Array.Copy(icc[k], 14, ficc, total, icc[k].Length - 14);
358-
total += icc[k].Length - 14;
359-
}
360-
try {
361-
image.SetProfile(IccProfile.GetInstance(ficc, image.GetColorSpace()));
362-
}
363-
catch (ArgumentException) {
364-
}
365-
}
370+
AttemptToSetIccProfileToImage(icc, image);
366371
}
367372

368-
// ignore ICC profile if it's invalid.
369373
/// <summary>Reads a short from the <c>InputStream</c>.</summary>
370374
/// <param name="jpegStream">the <c>InputStream</c></param>
371375
/// <returns>an int</returns>

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3380a3db4a6a60be0ec2e9286231067407346fa6
1+
aa44e369232f6929d6515d44cf60c5ac35c1d10e

0 commit comments

Comments
 (0)