int mm_util_encode_bmp_to_file(mm_util_bmp_data * encoded, const char *filename);
/**
+ * This function encodes raw data to buffer
+ *
+ * @param encoded [in ] pointer of mm_util_bmp_data.
+ * After using it, please free the allocated memory.
+ * @param buffer [out] output buffer on to which the encoded stream will be written.
+ * @param size [out] The size of output buffer on to which the encoded stream will be written.
+ * @return This function returns zero on success, or negative value with error code.
+ * @remark
+ * @see mm_util_bmp_data
+ * @since R1, 1.0
+ */
+int mm_util_encode_bmp_to_memory(mm_util_bmp_data *encoded, void **buffer, size_t *size);
+
+/**
* This function sets width of the encoded image.
*
* @param data [in] pointer of mm_util_bmp_data.
#define BYTES_PER_PIXEL 4
+#define BMP_SAFE_FREE(src) { if(src != NULL) {free(src); src = NULL;} }
+
void *__bitmap_create(int width, int height, unsigned int state);
unsigned char *__bitmap_get_buffer(void *bitmap);
size_t __bitmap_get_bpp(void *bitmap);
uint8_t *image;
if ((bmp = bmp_create(encoded->width, encoded->height, BYTES_PER_PIXEL * 8)) == NULL) {
- printf("Invalid depth value.\n");
+ mm_util_error("Invalid depth value.");
+ return MM_UTIL_ERROR_INVALID_OPERATION;
+ }
+
+ image = (uint8_t *) encoded->data;
+ for (row = 0; row != encoded->height; row++) {
+ for (col = 0; col != encoded->width; col++) {
+ size_t z = (row * encoded->width + col) * BYTES_PER_PIXEL;
+ pixel.red = image[z];
+ pixel.green = image[z + 1];
+ pixel.blue = image[z + 2];
+ bmp_set_pixel(bmp, col, row, pixel);
+ }
+ }
+
+ if (bmp_save(bmp, filename) == false) {
+ mm_util_error("Saving bmp was failed.");
+ bmp_destroy(bmp);
+ return MM_UTIL_ERROR_INVALID_OPERATION;
+ }
+ bmp_destroy(bmp);
+ return MM_UTIL_ERROR_NONE;
+}
+
+int mm_util_encode_bmp_to_memory(mm_util_bmp_data *encoded, void **buffer, size_t *size)
+{
+ bmpfile_t *bmp;
+ rgb_pixel_t pixel = { 0, 0, 0, 0 };
+ uint16_t row, col;
+ uint8_t *image;
+
+ if ((bmp = bmp_create(encoded->width, encoded->height, BYTES_PER_PIXEL * 8)) == NULL) {
+ mm_util_error("Invalid depth value.");
return MM_UTIL_ERROR_INVALID_OPERATION;
}
}
}
- bmp_save(bmp, filename);
+ if (bmp_save2(bmp, buffer, size) == false) {
+ mm_util_error("Saving bmp was failed.");
+ bmp_destroy(bmp);
+ BMP_SAFE_FREE(buffer);
+ *size = 0;
+ return MM_UTIL_ERROR_INVALID_OPERATION;
+ }
bmp_destroy(bmp);
return MM_UTIL_ERROR_NONE;
}