Skip to content

Commit 056c604

Browse files
authored
Resize image transformer cannot handle images with unknown pixel format. (#3612)
* Detect invalid pixel format. * Add test. * Force convert pixel format to Format32bppArgb in ImageResize transform. * Fix comment.. * Convert image pixel format to Format32bppArgb in ImagePixelExtractor transform if there is a need. * fix test. * Update taco image license information. * cleanup.
1 parent f154bb0 commit 056c604

File tree

7 files changed

+24
-7
lines changed

7 files changed

+24
-7
lines changed

src/Microsoft.ML.ImageAnalytics/ImagePixelExtractor.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Drawing;
8+
using System.Drawing.Imaging;
89
using System.Linq;
910
using System.Runtime.InteropServices;
1011
using System.Text;
@@ -346,11 +347,15 @@ private ValueGetter<VBuffer<TValue>> GetGetterCore<TValue>(DataViewRow input, in
346347
return;
347348
}
348349

349-
Host.Check(src.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb
350-
|| src.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb,
351-
"Transform only supports pixel formats Format24bppRgb and Format32bppArgb");
352350
Host.Check(src.Height == height && src.Width == width);
353351

352+
if (src.PixelFormat != PixelFormat.Format32bppArgb && src.PixelFormat != PixelFormat.Format24bppRgb)
353+
{
354+
var clone = src.Clone(new Rectangle(0, 0, src.Width, src.Height), PixelFormat.Format32bppArgb);
355+
src.Dispose();
356+
src = clone;
357+
}
358+
354359
var editor = VBufferEditor.Create(ref dst, size);
355360
var values = editor.Values;
356361

src/Microsoft.ML.ImageAnalytics/ImageResizer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,16 @@ protected override Delegate MakeGetter(DataViewRow input, int iinfo, Func<int, b
384384

385385
// Graphics.DrawImage() does not support PixelFormat.Indexed. Hence convert the
386386
// pixel format to Format32bppArgb as described here https://stackoverflow.com/questions/17313285/graphics-on-indexed-image
387-
if ((src.PixelFormat & PixelFormat.Indexed) != 0)
387+
// For images with invalid pixel format also use Format32bppArgb to draw the resized image.
388+
// For images with Format16bppGrayScale or Format16bppArgb1555 GDI+ does not
389+
// support these formats, ref: https://bytes.com/topic/c-sharp/answers/278572-out-memory-graphics-fromimage
390+
if ((src.PixelFormat & PixelFormat.Indexed) != 0 ||
391+
src.PixelFormat == PixelFormat.Format16bppGrayScale ||
392+
src.PixelFormat == PixelFormat.Format16bppArgb1555 ||
393+
!Enum.IsDefined(typeof(PixelFormat), src.PixelFormat))
394+
{
388395
dst = new Bitmap(info.ImageWidth, info.ImageHeight);
396+
}
389397
else
390398
dst = new Bitmap(info.ImageWidth, info.ImageHeight, src.PixelFormat);
391399

test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ public void TensorFlowTransformCifar()
874874
Assert.Equal(10, buffer.Length);
875875
numRows += 1;
876876
}
877-
Assert.Equal(5, numRows);
877+
Assert.Equal(7, numRows);
878878
}
879879
}
880880

test/data/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,6 @@ The dataset is distributed under [GPLv2](https://www.gnu.org/licenses/old-licens
112112
>
113113
> "[Hot dog with mustard](https://visualsonline.cancer.gov/details.cfm?imageid=2669)" by Renee Comet is in the public domain - this image was released by the [National Cancer Institute](https://visualsonline.cancer.gov/details.cfm?imageid=2669)
114114
>
115-
> "[Bright red tomato and cross section02](https://upload.wikimedia.org/wikipedia/commons/8/88/Bright_red_tomato_and_cross_section02.jpg)" by [fir0002](https://en.wikipedia.org/wiki/User:Fir0002) is licensed under the [CC BY-NC](https://creativecommons.org/licenses/by/2.0/)
115+
> "[Bright red tomato and cross section02](https://upload.wikimedia.org/wikipedia/commons/8/88/Bright_red_tomato_and_cross_section02.jpg)" by [fir0002](https://en.wikipedia.org/wiki/User:Fir0002) is licensed under the [CC BY-NC](https://creativecommons.org/licenses/by/2.0/)
116+
>
117+
> "[Grilled Portabella and Poblano Tacos](https://www.foodista.com/sites/default/files/78307aH.jpg)" by [foodista](https://www.foodista.com) is licensed under the [CC BY-NC](https://creativecommons.org/licenses/by/2.0/) as per [Bing Images search using All Creative Commons license filter](https://www.bing.com/images/search?q=78307aH&qs=n&form=QBIR&qft=%20filterui%3AlicenseType-Any&sp=-1&pq=78307ah&sc=8-7&sk=&cvid=C44379B4A4FA42C09D7629976997390C)

test/data/images/imagesmixedpixelformat.tsv

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ tomato.bmp tomato
22
banana.jpg banana
33
hotdog.jpg hotdog
44
tomato.jpg tomato
5-
tomato_indexedpixelformat.gif tomato
5+
tomato_indexedpixelformat.gif tomato
6+
tomato_format32bppPArgb.jpg tomato
7+
taco_invalidpixelformat.jpg taco
1.35 MB
Loading
34 KB
Loading

0 commit comments

Comments
 (0)