Fix rgb vs bgr especially for grayscale conversion

This commit is contained in:
Ben Olden-Cooligan 2022-07-28 18:40:40 -07:00
parent 0626d352ed
commit 9394262da4
11 changed files with 13 additions and 12 deletions

View File

@ -4,6 +4,6 @@ public enum ImagePixelFormat
{
Unsupported,
BW1,
RGB24,
RGB24, // This is actually BGR in the binary representation
ARGB32
}

View File

@ -5,6 +5,8 @@ namespace NAPS2.Images.Storage;
// TODO: Make internal?
// TODO: Improve preconditions to guard against misuse causing access violations
// TODO: Verify test coverage
// TODO: If we add a new IMemoryImage implementation, we need to figure out RGB vs BGR (bgr is the Bitmap default)
// TODO: A lot of the r/g/b naming is off, should be b/g/r (I only corrected it where colors are treated differently)
public static class UnsafeImageOps
{
public static unsafe void ChangeBrightness(IMemoryImage bitmap, float brightnessAdjusted)
@ -309,9 +311,9 @@ public static class UnsafeImageOps
if (x + k < w)
{
byte* pixel = row + (x + k) * bytesPerPixel;
byte r = *pixel;
byte b = *pixel;
byte g = *(pixel + 1);
byte b = *(pixel + 2);
byte r = *(pixel + 2);
// Use standard values for grayscale conversion to weight the RGB values
int luma = r * 299 + g * 587 + b * 114;
if (luma >= thresholdAdjusted)
@ -422,9 +424,9 @@ public static class UnsafeImageOps
for (int x = 0; x < w; x++)
{
byte* pixel = row + x * bytesPerPixel;
byte r = *pixel;
byte b = *pixel;
byte g = *(pixel + 1);
byte b = *(pixel + 2);
byte r = *(pixel + 2);
// Use standard values for grayscale conversion to weight the RGB values
int luma = r * 299 + g * 587 + b * 114;
outRow[x] = luma < thresholdAdjusted;

View File

@ -131,7 +131,7 @@
<value>Resources\word_p1_rotated.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="word_p2_bw" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\word_p2_bw.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
<value>Resources\word_p2_bw.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="image_pdf" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\image_pdf.pdf;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

View File

@ -43,9 +43,9 @@ public static class BlankDetector
{
for (int y = 0; y < image.Height; y++)
{
int r = bytes[stride * y + x * 3];
int b = bytes[stride * y + x * 3];
int g = bytes[stride * y + x * 3 + 1];
int b = bytes[stride * y + x * 3 + 2];
int r = bytes[stride * y + x * 3 + 2];
// Use standard values for grayscale conversion to weight the RGB values
int luma = r * 299 + g * 587 + b * 114;
if (luma < whiteThreshold * 1000)

View File

@ -100,15 +100,14 @@ public static class TwainMemoryBufferReader
{
int x = memoryBuffer.XOffset + dx;
int y = memoryBuffer.YOffset + dy;
// TODO: This might actually be the other way around
// Colors are provided as BGR, they need to be swapped to RGB
// R
// Colors are provided as RGB, they need to be swapped to BGR
// B
*(dstPtr + y * dstBytesPerRow + x * 3) =
*(srcPtr + dy * srcBytesPerRow + dx * 3 + 2);
// G
*(dstPtr + y * dstBytesPerRow + x * 3 + 1) =
*(srcPtr + dy * srcBytesPerRow + dx * 3 + 1);
// B
// R
*(dstPtr + y * dstBytesPerRow + x * 3 + 2) =
*(srcPtr + dy * srcBytesPerRow + dx * 3);
}