Merge pull request #402 from mischnic/table-image-fix

"Fix uiImage on mac"

uiImageAppend()'s swizzling only works if the pixel stride is a multiple of four. Don't swizzle the pixel stride pixels.

Also a minor documentation fix.
This commit is contained in:
Pietro Gagliardi 2018-08-06 08:15:36 -04:00 committed by GitHub
commit 976279ef33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 11 deletions

View File

@ -34,7 +34,7 @@ void uiImageAppend(uiImage *i, void *pixels, int pixelWidth, int pixelHeight, in
{ {
NSBitmapImageRep *repCalibrated, *repsRGB; NSBitmapImageRep *repCalibrated, *repsRGB;
uint8_t *swizzled, *bp, *sp; uint8_t *swizzled, *bp, *sp;
int n; int x, y;
unsigned char *pix[1]; unsigned char *pix[1];
// OS X demands that R and B are in the opposite order from what we expect // OS X demands that R and B are in the opposite order from what we expect
@ -43,7 +43,8 @@ void uiImageAppend(uiImage *i, void *pixels, int pixelWidth, int pixelHeight, in
swizzled = (uint8_t *) uiprivAlloc((byteStride * pixelHeight) * sizeof (uint8_t), "uint8_t[]"); swizzled = (uint8_t *) uiprivAlloc((byteStride * pixelHeight) * sizeof (uint8_t), "uint8_t[]");
bp = (uint8_t *) pixels; bp = (uint8_t *) pixels;
sp = swizzled; sp = swizzled;
for (n = 0; n < byteStride * pixelHeight; n += 4) { for (y = 0; y < pixelHeight; y++){
for (x = 0; x < pixelWidth; x++) {
sp[0] = bp[2]; sp[0] = bp[2];
sp[1] = bp[1]; sp[1] = bp[1];
sp[2] = bp[0]; sp[2] = bp[0];
@ -51,6 +52,9 @@ void uiImageAppend(uiImage *i, void *pixels, int pixelWidth, int pixelHeight, in
sp += 4; sp += 4;
bp += 4; bp += 4;
} }
// jump over unused bytes at end of line
bp += byteStride - pixelWidth * 4;
}
pix[0] = (unsigned char *) swizzled; pix[0] = (unsigned char *) swizzled;
repCalibrated = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:pix repCalibrated = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:pix

View File

@ -19,7 +19,7 @@
// desktop systems at the time of writing (mid-2018) // desktop systems at the time of writing (mid-2018)
// //
// uiImage is very simple: it only supports non-premultiplied 32-bit // uiImage is very simple: it only supports non-premultiplied 32-bit
// ARGB images, and libui does not provide any image file loading // RGBA images, and libui does not provide any image file loading
// or image format conversion utilities on top of that. // or image format conversion utilities on top of that.
typedef struct uiImage uiImage; typedef struct uiImage uiImage;
@ -36,8 +36,8 @@ _UI_EXTERN void uiFreeImage(uiImage *i);
// uiImageAppend adds a representation to the uiImage. // uiImageAppend adds a representation to the uiImage.
// pixels should point to a byte array of non-premultiplied pixels // pixels should point to a byte array of non-premultiplied pixels
// stored in [R G B A] order (so ((uint8_t *) pixels)[0] is the A of the // stored in [R G B A] order (so ((uint8_t *) pixels)[0] is the R of the
// first pixel and [3] is the B of the first pixel). pixelWidth and // first pixel and [3] is the A of the first pixel). pixelWidth and
// pixelHeight is the size *in pixels* of the image, and pixelStride is // pixelHeight is the size *in pixels* of the image, and pixelStride is
// the number *of bytes* per row of the pixels array. Therefore, // the number *of bytes* per row of the pixels array. Therefore,
// pixels itself must be at least byteStride * pixelHeight bytes long. // pixels itself must be at least byteStride * pixelHeight bytes long.