Browse Source

Speed up Bitmap decoding

Former-commit-id: 8fe4bf1334dcb4589d5d6a7d1eae7ddbad9d647b
Former-commit-id: a35a47453657a8961fa95c5fd7e071dea649a7b0
Former-commit-id: 2c5e5865acd0c53cf2076f35a0289e9e5eda2b7e
pull/17/head
James Jackson-South 11 years ago
parent
commit
1df06f9fb1
  1. 5
      src/ImageProcessor/Formats/Bmp/BmpDecoder.cs
  2. 107
      src/ImageProcessor/Formats/Bmp/BmpDecoderCore.cs
  3. 1
      src/ImageProcessor/Formats/Bmp/BmpEncoder.cs

5
src/ImageProcessor/Formats/Bmp/BmpDecoder.cs

@ -67,9 +67,8 @@ namespace ImageProcessor.Formats
bool isBmp = false; bool isBmp = false;
if (header.Length >= 2) if (header.Length >= 2)
{ {
isBmp = isBmp = header[0] == 0x42 && // B
header[0] == 0x42 && // B header[1] == 0x4D; // M
header[1] == 0x4D; // M
} }
return isBmp; return isBmp;

107
src/ImageProcessor/Formats/Bmp/BmpDecoderCore.cs

@ -250,31 +250,34 @@ namespace ImageProcessor.Formats
int alignment; int alignment;
byte[] data = this.GetImageArray(width, height, 2, out alignment); byte[] data = this.GetImageArray(width, height, 2, out alignment);
for (int y = 0; y < height; y++) Parallel.For(
{ 0,
int rowOffset = y * ((width * 2) + alignment); height,
y =>
{
int rowOffset = y * ((width * 2) + alignment);
// Revert the y value, because bitmaps are saved from down to top // Revert the y value, because bitmaps are saved from down to top
int row = Invert(y, height); int row = Invert(y, height);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
int offset = rowOffset + (x * 2); int offset = rowOffset + (x * 2);
short temp = BitConverter.ToInt16(data, offset); short temp = BitConverter.ToInt16(data, offset);
byte r = (byte)(((temp & Rgb16RMask) >> 11) * ScaleR); byte r = (byte)(((temp & Rgb16RMask) >> 11) * ScaleR);
byte g = (byte)(((temp & Rgb16GMask) >> 5) * ScaleG); byte g = (byte)(((temp & Rgb16GMask) >> 5) * ScaleG);
byte b = (byte)((temp & Rgb16BMask) * ScaleR); byte b = (byte)((temp & Rgb16BMask) * ScaleR);
int arrayOffset = ((row * width) + x) * 4; int arrayOffset = ((row * width) + x) * 4;
imageData[arrayOffset + 0] = b; imageData[arrayOffset + 0] = b;
imageData[arrayOffset + 1] = g; imageData[arrayOffset + 1] = g;
imageData[arrayOffset + 2] = r; imageData[arrayOffset + 2] = r;
imageData[arrayOffset + 3] = 255; imageData[arrayOffset + 3] = 255;
} }
} });
} }
/// <summary> /// <summary>
@ -288,24 +291,27 @@ namespace ImageProcessor.Formats
int alignment; int alignment;
byte[] data = this.GetImageArray(width, height, 3, out alignment); byte[] data = this.GetImageArray(width, height, 3, out alignment);
for (int y = 0; y < height; y++) Parallel.For(
{ 0,
int rowOffset = y * ((width * 3) + alignment); height,
y =>
{
int rowOffset = y * ((width * 3) + alignment);
// Revert the y value, because bitmaps are saved from down to top // Revert the y value, because bitmaps are saved from down to top
int row = Invert(y, height); int row = Invert(y, height);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
int offset = rowOffset + (x * 3); int offset = rowOffset + (x * 3);
int arrayOffset = ((row * width) + x) * 4; int arrayOffset = ((row * width) + x) * 4;
imageData[arrayOffset + 0] = data[offset + 0]; imageData[arrayOffset + 0] = data[offset + 0];
imageData[arrayOffset + 1] = data[offset + 1]; imageData[arrayOffset + 1] = data[offset + 1];
imageData[arrayOffset + 2] = data[offset + 2]; imageData[arrayOffset + 2] = data[offset + 2];
imageData[arrayOffset + 3] = 255; imageData[arrayOffset + 3] = 255;
} }
} });
} }
/// <summary> /// <summary>
@ -319,24 +325,27 @@ namespace ImageProcessor.Formats
int alignment; int alignment;
byte[] data = this.GetImageArray(width, height, 4, out alignment); byte[] data = this.GetImageArray(width, height, 4, out alignment);
for (int y = 0; y < height; y++) Parallel.For(
{ 0,
int rowOffset = y * ((width * 4) + alignment); height,
y =>
{
int rowOffset = y * ((width * 4) + alignment);
// Revert the y value, because bitmaps are saved from down to top // Revert the y value, because bitmaps are saved from down to top
int row = Invert(y, height); int row = Invert(y, height);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
int offset = rowOffset + (x * 4); int offset = rowOffset + (x * 4);
var arrayOffset = ((row * width) + x) * 4; var arrayOffset = ((row * width) + x) * 4;
imageData[arrayOffset + 0] = data[offset + 0]; imageData[arrayOffset + 0] = data[offset + 0];
imageData[arrayOffset + 1] = data[offset + 1]; imageData[arrayOffset + 1] = data[offset + 1];
imageData[arrayOffset + 2] = data[offset + 2]; imageData[arrayOffset + 2] = data[offset + 2];
imageData[arrayOffset + 3] = 255; // Can we get alpha here? imageData[arrayOffset + 3] = 255; // Can we get alpha here?
} }
} });
} }
/// <summary> /// <summary>

1
src/ImageProcessor/Formats/Bmp/BmpEncoder.cs

@ -7,7 +7,6 @@ namespace ImageProcessor.Formats
{ {
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks;
/// <summary> /// <summary>
/// Image encoder for writing an image to a stream as a Windows bitmap. /// Image encoder for writing an image to a stream as a Windows bitmap.

Loading…
Cancel
Save