Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e7a4eb

Browse files
Merge pull request JimBobSquarePants#301 from optlink/LosslessWebP
When WebP quality is set to 100, encode losslessly.
2 parents d36f9ae + fea2558 commit 2e7a4eb

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/Plugins/ImageProcessor/ImageProcessor.Plugins.WebP/Imaging/Formats/NativeMethods.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,30 @@ static NativeMethods()
115115
[DllImport("libwebp", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPEncodeBGRA")]
116116
public static extern int WebPEncodeBGRA(IntPtr rgb, int width, int height, int stride, float qualityFactor, out IntPtr output);
117117

118+
/// <summary>
119+
/// Lossless encoding of images pointed to by *data in WebP format
120+
/// </summary>
121+
/// <param name="rgb">
122+
/// Pointer to RGB image data
123+
/// </param>
124+
/// <param name="width">
125+
/// The width range is limited currently from 1 to 16383
126+
/// </param>
127+
/// <param name="height">
128+
/// The height range is limited currently from 1 to 16383
129+
/// </param>
130+
/// <param name="stride">
131+
/// The stride.
132+
/// </param>
133+
/// <param name="output">
134+
/// output_buffer with WebP image
135+
/// </param>
136+
/// <returns>
137+
/// Size of WebP Image
138+
/// </returns>
139+
[DllImport("libwebp", CallingConvention = CallingConvention.Cdecl, EntryPoint = "WebPEncodeLosslessBGRA")]
140+
public static extern int WebPEncodeLosslessBGRA(IntPtr rgb, int width, int height, int stride, out IntPtr output);
141+
118142
/// <summary>
119143
/// Frees the unmanaged memory.
120144
/// </summary>

src/Plugins/ImageProcessor/ImageProcessor.Plugins.WebP/Imaging/Formats/WebPFormat.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ public override Image Save(string path, Image image)
126126
byte[] bytes;
127127

128128
// Encode in webP format.
129-
if (EncodeLossly((Bitmap)image, this.Quality, out bytes))
129+
// If Quality is 100, encode losslessly instead of lossily
130+
if (this.Quality == 100 ? EncodeLosslessly((Bitmap)image, out bytes) : EncodeLossly((Bitmap)image, this.Quality, out bytes))
130131
{
131132
File.WriteAllBytes(path, bytes);
132133
}
@@ -147,7 +148,8 @@ public override Image Save(Stream stream, Image image)
147148
byte[] bytes;
148149

149150
// Encode in webP format.
150-
if (EncodeLossly((Bitmap)image, this.Quality, out bytes))
151+
// If Quality is 100, encode losslessly instead of lossily
152+
if (this.Quality == 100 ? EncodeLosslessly((Bitmap)image, out bytes) : EncodeLossly((Bitmap)image, this.Quality, out bytes))
151153
{
152154
using (MemoryStream memoryStream = new MemoryStream(bytes))
153155
{
@@ -271,5 +273,50 @@ private static bool EncodeLossly(Bitmap bitmap, int quality, out byte[] webpData
271273

272274
return encoded;
273275
}
276+
277+
/// <summary>
278+
/// Losslessly encodes the image in bitmap.
279+
/// </summary>
280+
/// <param name="bitmap">
281+
/// Bitmap with the image
282+
/// </param>
283+
/// <param name="webpData">
284+
/// The byte array containing the encoded image data.
285+
/// </param>
286+
/// <returns>
287+
/// True if success; False otherwise
288+
/// </returns>
289+
private static bool EncodeLosslessly(Bitmap bitmap, out byte[] webpData)
290+
{
291+
webpData = null;
292+
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
293+
IntPtr unmanagedData = IntPtr.Zero;
294+
bool encoded;
295+
296+
try
297+
{
298+
// Attempt to losslessly encode the image.
299+
int size = NativeMethods.WebPEncodeLosslessBGRA(bmpData.Scan0, bitmap.Width, bitmap.Height, bmpData.Stride, out unmanagedData);
300+
301+
// Copy image compress data to output array
302+
webpData = new byte[size];
303+
Marshal.Copy(unmanagedData, webpData, 0, size);
304+
encoded = true;
305+
}
306+
catch
307+
{
308+
encoded = false;
309+
}
310+
finally
311+
{
312+
// Unlock the pixels
313+
bitmap.UnlockBits(bmpData);
314+
315+
// Free memory
316+
NativeMethods.WebPFree(unmanagedData);
317+
}
318+
319+
return encoded;
320+
}
274321
}
275322
}

0 commit comments

Comments
 (0)