This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
Shrink.cs
217 lines (197 loc) · 7.17 KB
/
Shrink.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// AForge Image Processing Library
// AForge.NET framework
//
// Copyright © Andrew Kirillov, 2005-2008
// andrew.kirillov@gmail.com
//
namespace Accord.Imaging.Filters
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
/// Shrink an image by removing specified color from its boundaries.
/// </summary>
///
/// <remarks><para>Removes pixels with specified color from image boundaries making
/// the image smaller in size.</para>
///
/// <para>The filter accepts 8 bpp grayscale and 24 bpp color images for processing.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create filter
/// Shrink filter = new Shrink( Color.Black );
/// // apply the filter
/// Bitmap newImage = filter.Apply( image );
/// </code>
///
/// <para><b>Initial image:</b></para>
/// <img src="..\images\imaging\sample2.jpg" width="320" height="240" />
/// <para><b>Result image:</b></para>
/// <img src="..\images\imaging\shrink.jpg" width="295" height="226" />
/// </remarks>
///
public class Shrink : BaseTransformationFilter
{
private Color colorToRemove = Color.FromArgb( 0, 0, 0 );
// top-left coordinates of the object (calculated by CalculateNewImageSize())
private int minX, minY;
// format translation dictionary
private Dictionary<PixelFormat, PixelFormat> formatTranslations = new Dictionary<PixelFormat, PixelFormat>( );
/// <summary>
/// Format translations dictionary.
/// </summary>
public override Dictionary<PixelFormat, PixelFormat> FormatTranslations
{
get { return formatTranslations; }
}
/// <summary>
/// Color to remove from boundaries.
/// </summary>
///
public Color ColorToRemove
{
get { return colorToRemove; }
set { colorToRemove = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref="Shrink"/> class.
/// </summary>
///
public Shrink( )
{
formatTranslations[PixelFormat.Format8bppIndexed] = PixelFormat.Format8bppIndexed;
formatTranslations[PixelFormat.Format24bppRgb] = PixelFormat.Format24bppRgb;
}
/// <summary>
/// Initializes a new instance of the <see cref="Shrink"/> class.
/// </summary>
///
/// <param name="colorToRemove">Color to remove from boundaries.</param>
///
public Shrink( Color colorToRemove ) : this( )
{
this.colorToRemove = colorToRemove;
}
/// <summary>
/// Calculates new image size.
/// </summary>
///
/// <param name="sourceData">Source image data.</param>
///
/// <returns>New image size - size of the destination image.</returns>
///
protected override System.Drawing.Size CalculateNewImageSize( UnmanagedImage sourceData )
{
// get source image size
int width = sourceData.Width;
int height = sourceData.Height;
int offset = sourceData.Stride -
( ( sourceData.PixelFormat == PixelFormat.Format8bppIndexed ) ? width : width * 3 );
// color to remove
byte r = colorToRemove.R;
byte g = colorToRemove.G;
byte b = colorToRemove.B;
minX = width;
minY = height;
int maxX = 0;
int maxY = 0;
// find rectangle which contains something except color to remove
unsafe
{
byte* src = (byte*) sourceData.ImageData.ToPointer( );
if ( sourceData.PixelFormat == PixelFormat.Format8bppIndexed )
{
// grayscale
for ( int y = 0; y < height; y++ )
{
for ( int x = 0; x < width; x++, src++ )
{
if ( *src != g )
{
if ( x < minX )
minX = x;
if ( x > maxX )
maxX = x;
if ( y < minY )
minY = y;
if ( y > maxY )
maxY = y;
}
}
src += offset;
}
}
else
{
// RGB
for ( int y = 0; y < height; y++ )
{
for ( int x = 0; x < width; x++, src += 3 )
{
if (
( src[RGB.R] != r ) ||
( src[RGB.G] != g ) ||
( src[RGB.B] != b ) )
{
if ( x < minX )
minX = x;
if ( x > maxX )
maxX = x;
if ( y < minY )
minY = y;
if ( y > maxY )
maxY = y;
}
}
src += offset;
}
}
}
// check
if ( ( minX == width ) && ( minY == height ) && ( maxX == 0 ) && ( maxY == 0 ) )
{
minX = minY = 0;
}
return new Size( maxX - minX + 1, maxY - minY + 1 );
}
/// <summary>
/// Process the filter on the specified image.
/// </summary>
///
/// <param name="sourceData">Source image data.</param>
/// <param name="destinationData">Destination image data.</param>
///
protected override unsafe void ProcessFilter( UnmanagedImage sourceData, UnmanagedImage destinationData )
{
// get destination image size
int newWidth = destinationData.Width;
int newHeight = destinationData.Height;
int srcStride = sourceData.Stride;
int dstStride = destinationData.Stride;
int copySize = newWidth;
// do the job
byte* src = (byte*) sourceData.ImageData.ToPointer( );
byte* dst = (byte*) destinationData.ImageData.ToPointer( );
src += ( minY * srcStride );
if ( destinationData.PixelFormat == PixelFormat.Format8bppIndexed )
{
src += minX;
}
else
{
src += minX * 3;
copySize *= 3;
}
// copy image
for ( int y = 0; y < newHeight; y++ )
{
Accord.SystemTools.CopyUnmanagedMemory( dst, src, copySize );
dst += dstStride;
src += srcStride;
}
}
}
}