From c0742d3de00325a618334a62b4c5c9c4799ddd6b Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Mon, 6 Aug 2018 09:06:02 +0200 Subject: [PATCH] Mac uiImageAppend: handle stride correctly --- darwin/image.m | 20 ++++++++++++-------- uitable.h | 6 +++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/darwin/image.m b/darwin/image.m index 9492cd07..cf2bdf13 100644 --- a/darwin/image.m +++ b/darwin/image.m @@ -34,7 +34,7 @@ void uiImageAppend(uiImage *i, void *pixels, int pixelWidth, int pixelHeight, in { NSBitmapImageRep *repCalibrated, *repsRGB; uint8_t *swizzled, *bp, *sp; - int n; + int n, l; unsigned char *pix[1]; // OS X demands that R and B are in the opposite order from what we expect @@ -43,13 +43,17 @@ void uiImageAppend(uiImage *i, void *pixels, int pixelWidth, int pixelHeight, in swizzled = (uint8_t *) uiprivAlloc((byteStride * pixelHeight) * sizeof (uint8_t), "uint8_t[]"); bp = (uint8_t *) pixels; sp = swizzled; - for (n = 0; n < byteStride * pixelHeight; n += 4) { - sp[0] = bp[2]; - sp[1] = bp[1]; - sp[2] = bp[0]; - sp[3] = bp[3]; - sp += 4; - bp += 4; + for (l = 0; l < pixelHeight; l++){ + for (n = 0; n < pixelWidth; n++) { + sp[0] = bp[2]; + sp[1] = bp[1]; + sp[2] = bp[0]; + sp[3] = bp[3]; + sp += 4; + bp += 4; + } + // jump over unused bytes at end of line + bp += byteStride - pixelWidth * 4; } pix[0] = (unsigned char *) swizzled; diff --git a/uitable.h b/uitable.h index 15f33ebe..17a7abe8 100644 --- a/uitable.h +++ b/uitable.h @@ -19,7 +19,7 @@ // desktop systems at the time of writing (mid-2018) // // 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. typedef struct uiImage uiImage; @@ -36,8 +36,8 @@ _UI_EXTERN void uiFreeImage(uiImage *i); // uiImageAppend adds a representation to the uiImage. // 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 -// first pixel and [3] is the B of the first pixel). pixelWidth and +// stored in [R G B A] order (so ((uint8_t *) pixels)[0] is the R of the +// 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 // the number *of bytes* per row of the pixels array. Therefore, // pixels itself must be at least byteStride * pixelHeight bytes long.