Some specifications of WebPEncodeLosslessBGRA function from libwebp Api
WebPEncodeLosslessBGRAThis function encodes images pointed to by bgra parameter and returns the WebP image file and it´s size.
It compresses the image using the lossless format. Files are usually larger than lossy format, but will not suffer any compression loss. For lossy compression, you can use the WebPEncodeRGBA family.
The lossy functions (WebPEncodeLosslessRGB, WebPEncodeLosslessBGR, WebPEncodeLosslessRGBA and WebPEncodeLosslessBGRA), use the library's default settings. For lossless this means exact is disabled. RGB values in transparent areas will be modified to improve compression.
To avoid this, use WebPEncode() and set WebPConfig::exact to 1.
Parameters: bgra (in) - Pointer to the Pixel data of the image in BGRA format. So, it points to a Array of RGBQUAD structures. You can retrieve the Pixels from gdiplus, simple gdi or whatever other library
Width (in) - An integer representing the width of the image (In pixels)
Height (in) - An integer representing the height of the image (In pixels)
stride (in) - An integer specifying the stride of the image (In bytes). It specifies the byte offset between the beginning of one scan line and the next.
This is usually (but not necessarily) the number of bytes in the pixel format (for example, 2 for 16 bits per pixel and 4 for 24 or 32bpp) multiplied by the width of the bitmap.
An easier way to calculate the stride is with GetStride function showed below
output(out) - Pointer to a variable (int) where the WebpImage will be stored
Return Values: If the function suceeds, it returns the size of the converted image
If the function fails, it returns 0
Remarks: The function allocates internally enough memory to do the convertion, so, after using the function, you must deallocate the memory with WebPFree Api.
Example of usage: call GetStride D@ImgWidth, 32
lea ecx D@OutData | mov D$ecx 0
call 'libwebp.WebPEncodeLosslessBGRA' D@PixData, D@ImgWidth, D@ImgHeight, eax, ecx
call 'libwebp.WebPFree' D@OutData ; release the allocated memory used internally in libwebp library
More info (although very very incomplete) can be found at:
https://developers.google.com/speed/webp/docs/apiAdditional Function:
GetStride;;
GetStride
This function calculates the Line Stride of the Image given a BitCount, biPlane and Width input.
Similar to GetLineStrideFromBmpNfoHdr Api, Line Strides (also known as RowSize, line padding or Stride) are basically
the number of bytes occupied in memory by a line of an image.
It specifies the byte offset between the beginning of one scan line and the next.
This is usually (but not necessarily) the number of bytes in the pixel format (for example, 2 for 16 bits per pixel) multiplied by the width of the bitmap.
In GdiPlus Api, the value passed as a Stride (in GdipCreateBitmapFromScan0 for example), must be a multiple of four.
Parameters:
Width - A integer representing the width of the image
BitCount - A integer representing the bit count of the image. It specifies the number of bits per pixel (bpp).
For uncompressed formats, this value is the average number of bits per pixel.
For compressed formats, this value is the implied bit depth of the uncompressed image,
after the image has been decoded.
Allowed values are: 1, 4, 8, 16, 24 and 32
Return value:
The function will return in eax the stride of a given bitmap image. (In bytes)
Remarks:
Stride is calculated as a padding bytes. The following table illustrates the relationship between width and stride.
width stride
1 4
2 4
3 4
4 4
5 8
6 8
7 8
8 8
9 12
10 12
11 12
12 12
......
Be carefull when using the returned value, because it is always in Bytes, but some Apis may uses the stride as a multiple of 4 (to represent the RGBQUAD values)
On google Libwebp library, gdiplus, normal gdi, FGetDIBits or GdipSaveWebpImage, the returned value are in Bytes, so we don´t need to divide it by 4.
The function works for:
BITMAPINFOHEADER, BITMAPV2INFOHEADER, BITMAPV3INFOHEADER, BITMAPV4INFOHEADER, BITMAPV5INFOHEADER
Example of usage:
a) With libwebp:
call GetStride D@ImgWidth, 32
lea ecx D@OutData | mov D$ecx 0
call 'libwebp.WebPEncodeLosslessBGRA' D@PixData, D@ImgWidth, D@ImgHeight, eax, ecx
b) With gdiplus:
call GetStride D@ImgWidth, 32
call 'gdiplus.GdipCreateBitmapFromScan0' D@ImageWidth, D@ImageHeight, eax, PIXELFORMAT_32BPPARGB, D@ImgBits, D@pImage
c) with gdi to calculate the amount of memory neded to build the pixels data
call GetLineStrideFromBmpNfoHdr OutBmpNfo
Align_On 4 eax | imul eax D@BITMAP.bmHeightDis | mov D@RawImgDataSize eax
mov D@TmpMem 0 | lea eax D@TmpMem
call 'RosMem.VMemAlloc' eax, D@RawImgDataSize
References: http://mapw.elte.hu/elek/bmpinmemory.html
https://medium.com/@oleg.shipitko/what-does-stride-mean-in-image-processing-bba158a72bcd
https://www.collabora.com/news-and-blog/blog/2016/02/16/a-programmers-view-on-digital-images-the-essentials/
;;
Proc GetStride::
Arguments @Width, @BitCount
Local @cClrBits
Uses edx
; Convert the color format to a count of bits. No need to calculate the bitplanes sicne it is always 1 for bitmaps
movzx eax W@BitCount
If ax <= 1
mov eax 1
Else_If ax <= 4
mov eax 4
Else_If ax <= 8
mov eax 8
Else_If ax <= 16
mov eax 16
Else_If ax <= 24
mov eax 24
Else
mov eax 32
End_If
mov D@cClrBits eax
; Compute the number of bytes in the array of color indices and store the result in biSizeImage.
; For Windows NT, the width must be DWORD aligned unless the bitmap is RLE compressed. This example shows this.
; For Windows 95/98/Me, the width must be WORD aligned unless the bitmap is RLE compressed.
movzx eax W@Width
imul eax D@cClrBits | add eax 31 | and eax (0-32)
cdq | and edx 7 | add eax edx | sar eax 3
EndP
Libwebp (v 1.1.0) Library attached