Skip to content

Commit

Permalink
Fixed image loading to zero transparent pixels at the native code level.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomspilman committed Sep 22, 2024
1 parent 494b2ba commit 839b001
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion MonoGame.Framework/Graphics/Texture2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream,
/// </remarks>
public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream)
{
return FromStream(graphicsDevice, stream, DefaultColorProcessors.ZeroTransparentPixels);
return FromStream(graphicsDevice, stream, null);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions MonoGame.Framework/Platform/Native/Image.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal static unsafe partial class MGI
public static partial void ReadRGBA(
byte* data,
int dataBytes,
[MarshalAs(UnmanagedType.U1)] bool zeroTransparentPixels,
out int width,
out int height,
out byte* rgba);
Expand Down
2 changes: 2 additions & 0 deletions MonoGame.Framework/Platform/Native/Texture2D.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private static unsafe Texture2D PlatformFromStream(GraphicsDevice graphicsDevice
MGI.ReadRGBA(
(byte*)handle.AddrOfPinnedObject(),
dataLength,
colorProcessor == null ? true : false,
out width,
out height,
out rgba);
Expand Down Expand Up @@ -240,6 +241,7 @@ private unsafe void PlatformReload(Stream stream)
MGI.ReadRGBA(
(byte*)handle.AddrOfPinnedObject(),
dataLength,
true,
out width,
out height,
out rgba);
Expand Down
40 changes: 20 additions & 20 deletions native/monogame/common/MGI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@
#include "stb_image_write.h"


void MGI_ReadRGBA(mgbyte* data, mgint dataBytes, mgint* width, mgint* height, mgbyte** rgba)
void MGI_ReadRGBA(mgbyte* data, mgint dataBytes, mgbool zeroTransparentPixels, mgint& width, mgint& height, mgbyte*& rgba)
{
*width = 0;
*height = 0;
*rgba = nullptr;
width = 0;
height = 0;
rgba = nullptr;

int c, w, h;
auto image = stbi_load_from_memory(data, dataBytes, &w, &h, &c, 4);
if (image == nullptr)
{
*width = 0;
*height = 0;
width = 0;
height = 0;
return;
}

// If the original image before conversion had alpha...
if (c == 4)
if (zeroTransparentPixels && c == 4)
{
// XNA blacks out any pixels with an alpha of zero.
for (int i = 0; i < w * h; i += 4)
Expand All @@ -52,9 +52,9 @@ void MGI_ReadRGBA(mgbyte* data, mgint dataBytes, mgint* width, mgint* height, mg
}
}

*rgba = image;
*width = w;
*height = h;
rgba = image;
width = w;
height = h;
}

struct mem_image
Expand Down Expand Up @@ -92,26 +92,26 @@ static void mem_image_write(void* context, void* data, int size)
image->offset = offset;
}

void MGI_WriteJpg(mgbyte* data, mgint dataBytes, mgint width, mgint height, mgint quality, mgbyte** jpg, mgint* jpgBytes)
void MGI_WriteJpg(mgbyte* data, mgint dataBytes, mgint width, mgint height, mgint quality, mgbyte*& jpg, mgint& jpgBytes)
{
*jpg = nullptr;
*jpgBytes = 0;
jpg = nullptr;
jpgBytes = 0;

mem_image image;
stbi_write_jpg_to_func(mem_image_write, &image, width, height, 4, data, quality);

*jpg = (mgbyte*)realloc(image.data, image.offset);
*jpgBytes = image.dataBytes;
jpg = (mgbyte*)realloc(image.data, image.offset);
jpgBytes = image.dataBytes;
}

void MGI_WritePng(mgbyte* data, mgint dataBytes, mgint width, mgint height, mgbyte** png, mgint* pngBytes)
void MGI_WritePng(mgbyte* data, mgint dataBytes, mgint width, mgint height, mgbyte*& png, mgint& pngBytes)
{
*png = nullptr;
*pngBytes = 0;
png = nullptr;
pngBytes = 0;

mem_image image;
stbi_write_png_to_func(mem_image_write, &image, width, height, 4, data, 4);

*png = (mgbyte*)realloc(image.data, image.offset);
*pngBytes = image.dataBytes;
png = (mgbyte*)realloc(image.data, image.offset);
pngBytes = image.dataBytes;
}
2 changes: 1 addition & 1 deletion native/monogame/include/api_MGI.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@



MG_EXPORT void MGI_ReadRGBA(mgbyte* data, mgint dataBytes, mgint& width, mgint& height, mgbyte*& rgba);
MG_EXPORT void MGI_ReadRGBA(mgbyte* data, mgint dataBytes, mgbool zeroTransparentPixels, mgint& width, mgint& height, mgbyte*& rgba);
MG_EXPORT void MGI_WriteJpg(mgbyte* data, mgint dataBytes, mgint width, mgint height, mgint quality, mgbyte*& jpg, mgint& jpgBytes);
MG_EXPORT void MGI_WritePng(mgbyte* data, mgint dataBytes, mgint width, mgint height, mgbyte*& png, mgint& pngBytes);

0 comments on commit 839b001

Please sign in to comment.