add ToBitmapBytes func (#204)

This commit is contained in:
vinhjaxt 2019-03-16 20:12:07 +07:00 committed by vz
parent 19c54fff4f
commit d280b98394
2 changed files with 23 additions and 0 deletions

View File

@ -320,6 +320,16 @@ int saveMMBitmapAsBMP(MMBitmapRef bitmap, const char *path)
return 0;
}
uint8_t *saveMMBitmapAsBytes(MMBitmapRef bitmap, size_t *dataLen)
{
uint8_t *data;
if ((data = createBitmapData(bitmap, dataLen)) == NULL) {
*dataLen = -1;
return NULL;
}
return data;
}
static uint8_t *readImageData(FILE *fp, size_t width, size_t height,
uint8_t bytesPerPixel, size_t bytewidth)
{

View File

@ -797,6 +797,19 @@ func ToCBitmap(bit Bitmap) C.MMBitmapRef {
return cbitmap
}
// ToBitmapBytes saves Bitmap to bitmap format in bytes
func ToBitmapBytes(bit C.MMBitmapRef) []byte {
var len C.size_t
ptr := C.saveMMBitmapAsBytes(bit, &len)
if int(len) < 0 {
return nil
}
bs := C.GoBytes(unsafe.Pointer(ptr), C.int(len))
C.free(unsafe.Pointer(ptr))
return bs
}
// ToMMBitmapRef trans CBitmap to C.MMBitmapRef
func ToMMBitmapRef(bit CBitmap) C.MMBitmapRef {
return C.MMBitmapRef(bit)