} mm_util_bmp_data;
/**
- * This function extracts raw data from bmp file
+ * This function decodes bmp image(file) to color image(raw data)
*
- * @param filename [in] input file name, encoded stream file
- * @param decoded [out] pointer of mm_util_bmp_data.
+ * @param filename [in] the input file, encoded bmp image
+ * @param decoded [out] the pointer of mm_util_bmp_data.
* After using it, please free the allocated memory.
* @return This function returns zero on success, or negative value with error code.
* @remark
int mm_util_decode_from_bmp_file(const char *filename, mm_util_bmp_data * decoded);
/**
- * This function extracts raw data from bmp memory
+ * This function decodes bmp image(memory) to color image(raw data)
*
- * @param memory [in] input memory, encoded stream file
- * @param src_size [in] input src size, encoded stream file
- * @param decoded [out] pointer of mm_util_bmp_data.
+ * @param memory [in] the input memory, encoded bmp image
+ * @param src_size [in] the size of input memory
+ * @param decoded [out] the pointer of mm_util_bmp_data.
* After using it, please free the allocated memory.
* @return This function returns zero on success, or negative value with error code.
* @remark
int mm_util_decode_from_bmp_memory(void *memory, const size_t src_size, mm_util_bmp_data * decoded);
/**
- * This function encodes raw data to bmp file
+ * This function encodes color image(raw data) to bmp file
*
- * @param decoded [in] pointer of mm_util_bmp_data.
+ * @param decoded [in] the pointer of mm_util_bmp_data.
* After using it, please free the allocated memory.
- * @param filename [out] output file name on to which the encoded stream will be written.
+ * @param filename [out] the output file name on to which the encoded bmp stream will be written.
* @return This function returns zero on success, or negative value with error code.
* @remark
* @see mm_util_bmp_data
int mm_util_encode_bmp_to_file(mm_util_bmp_data * decoded, const char *filename);
/**
- * This function encodes raw data to buffer
+ * This function encodes color image(raw data) to buffer
*
- * @param decoded [in] pointer of mm_util_bmp_data.
+ * @param decoded [in] the 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 buffer [out] the output memory on to which the encoded bmp 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
#define BYTES_PER_PIXEL 4
+/* for bmp_bitmap_callback_vt of nsbmp */
void *__bitmap_create(int width, int height, unsigned int state);
unsigned char *__bitmap_get_buffer(void *bitmap);
size_t __bitmap_get_bpp(void *bitmap);
void __bitmap_destroy(void *bitmap);
+void *__bitmap_create(int width, int height, unsigned int state)
+{
+ return calloc(width * height, BYTES_PER_PIXEL);
+}
+
+unsigned char *__bitmap_get_buffer(void *bitmap)
+{
+ return bitmap;
+}
+
+size_t __bitmap_get_bpp(void *bitmap)
+{
+ return BYTES_PER_PIXEL;
+}
+
+void __bitmap_destroy(void *bitmap)
+{
+ MMUTIL_SAFE_FREE(bitmap);
+}
+
static unsigned char *__load_file(const char *path, size_t * data_size)
{
FILE *fd;
}
}
-void *__bitmap_create(int width, int height, unsigned int state)
-{
- return calloc(width * height, BYTES_PER_PIXEL);
-}
-
-unsigned char *__bitmap_get_buffer(void *bitmap)
-{
- return bitmap;
-}
-
-size_t __bitmap_get_bpp(void *bitmap)
-{
- return BYTES_PER_PIXEL;
-}
-
-void __bitmap_destroy(void *bitmap)
-{
- MMUTIL_SAFE_FREE(bitmap);
-}
-
-static int __read_bmp(mm_util_bmp_data *decoded, const char *filename, void *memory, size_t src_size)
+/* decodes bmp image to color image */
+static int _read_bmp(const char *filename, void *memory, size_t src_size, mm_util_bmp_data *decoded)
{
bmp_bitmap_callback_vt bitmap_callbacks = {
__bitmap_create,
int res = MM_UTIL_ERROR_NONE;
unsigned char *data = NULL;
+ mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+ mm_util_retvm_if(!MMUTIL_STRING_VALID(filename) && (memory == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+
if (filename) {
data = __load_file(filename, &size);
if (data == NULL)
return MM_UTIL_ERROR_INVALID_OPERATION;
- } else if (memory) {
+ } else {
data = (unsigned char *)memory;
size = src_size;
- } else {
- mm_util_error("Bmp File wrong decode parameters");
- return MM_UTIL_ERROR_INVALID_OPERATION;
}
nsbmp_create(&bmp, &bitmap_callbacks);
return res;
}
-int mm_util_decode_from_bmp_file(const char *filename, mm_util_bmp_data *decoded)
-{
- int ret;
-
- mm_util_debug("mm_util_decode_from_gif_file");
-
- ret = __read_bmp(decoded, filename, NULL, 0);
-
- return ret;
-}
-
-int mm_util_decode_from_bmp_memory(void *memory, const size_t src_size, mm_util_bmp_data *decoded)
-{
- int ret;
-
- mm_util_debug("mm_util_decode_from_gif_file");
-
- ret = __read_bmp(decoded, NULL, memory, src_size);
-
- return ret;
-}
-
-int mm_util_encode_bmp_to_file(mm_util_bmp_data *decoded, const char *filename)
+/* encodes color image to bmp image */
+static int _write_bmp(mm_util_bmp_data *decoded, const char *filename, void **memory, size_t *src_size)
{
bmpfile_t *bmp;
rgb_pixel_t pixel = { 0, 0, 0, 0 };
uint16_t row, col;
uint8_t *image;
+ mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+ mm_util_retvm_if(!MMUTIL_STRING_VALID(filename) && (memory == NULL || src_size == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+
+ /* header of bmp image is create at 'bmp' */
if ((bmp = bmp_create(decoded->width, decoded->height, BYTES_PER_PIXEL * 8)) == NULL) {
mm_util_error("Invalid depth value.");
return MM_UTIL_ERROR_INVALID_OPERATION;
}
+ /* pixcels of bmp image is stored at 'bmp' */
image = (uint8_t *) decoded->data;
for (row = 0; row != decoded->height; row++) {
for (col = 0; col != decoded->width; col++) {
}
}
- if (bmp_save(bmp, filename) == false) {
- mm_util_error("Saving bmp was failed.");
- bmp_destroy(bmp);
- return MM_UTIL_ERROR_INVALID_OPERATION;
+ /* write 'bmp' to file or memory */
+ if (MMUTIL_STRING_VALID(filename)) {
+ if (bmp_save(bmp, filename) == false) {
+ mm_util_error("Saving bmp was failed.");
+ bmp_destroy(bmp);
+ return MM_UTIL_ERROR_INVALID_OPERATION;
+ }
+ } else {
+ if (bmp_save2(bmp, memory, src_size) == false) {
+ mm_util_error("Saving bmp was failed.");
+ bmp_destroy(bmp);
+ MMUTIL_SAFE_FREE(*memory);
+ *src_size = 0;
+ return MM_UTIL_ERROR_INVALID_OPERATION;
+ }
}
+
bmp_destroy(bmp);
return MM_UTIL_ERROR_NONE;
}
+int mm_util_decode_from_bmp_file(const char *filename, mm_util_bmp_data *decoded)
+{
+ int ret = MM_UTIL_ERROR_NONE;
+
+ mm_util_retvm_if(!MMUTIL_STRING_VALID(filename), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+ mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+
+ mm_util_fenter();
+
+ ret = _read_bmp(filename, NULL, 0, decoded);
+ if (ret != MM_UTIL_ERROR_NONE)
+ mm_util_error("_read_bmp failed (%d)", ret);
+
+ mm_util_fleave();
+
+ return ret;
+}
+
+int mm_util_decode_from_bmp_memory(void *memory, const size_t src_size, mm_util_bmp_data *decoded)
+{
+ int ret = MM_UTIL_ERROR_NONE;
+
+ mm_util_retvm_if((memory == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+ mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+
+ mm_util_fenter();
+
+ ret = _read_bmp(NULL, memory, src_size, decoded);
+ if (ret != MM_UTIL_ERROR_NONE)
+ mm_util_error("_read_bmp failed (%d)", ret);
+
+ mm_util_fleave();
+
+ return ret;
+}
+
+int mm_util_encode_bmp_to_file(mm_util_bmp_data *decoded, const char *filename)
+{
+ int ret = MM_UTIL_ERROR_NONE;
+
+ mm_util_retvm_if(!MMUTIL_STRING_VALID(filename), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+ mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+
+ mm_util_fenter();
+
+ ret = _write_bmp(decoded, filename, NULL, NULL);
+ if (ret != MM_UTIL_ERROR_NONE)
+ mm_util_error("_write_bmp failed (%d)", ret);
+
+ mm_util_fleave();
+
+ return ret;
+}
+
int mm_util_encode_bmp_to_memory(mm_util_bmp_data *decoded, void **buffer, size_t *size)
{
- bmpfile_t *bmp;
- rgb_pixel_t pixel = { 0, 0, 0, 0 };
- uint16_t row, col;
- uint8_t *image;
+ int ret = MM_UTIL_ERROR_NONE;
- if ((bmp = bmp_create(decoded->width, decoded->height, BYTES_PER_PIXEL * 8)) == NULL) {
- mm_util_error("Invalid depth value.");
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
+ mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+ mm_util_retvm_if((buffer == NULL || size == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
- image = (uint8_t *) decoded->data;
- for (row = 0; row != decoded->height; row++) {
- for (col = 0; col != decoded->width; col++) {
- size_t z = (row * decoded->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);
- }
- }
+ mm_util_fenter();
- if (bmp_save2(bmp, buffer, size) == false) {
- mm_util_error("Saving bmp was failed.");
- bmp_destroy(bmp);
- MMUTIL_SAFE_FREE(*buffer);
- *size = 0;
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
- bmp_destroy(bmp);
- return MM_UTIL_ERROR_NONE;
+ ret = _write_bmp(decoded, NULL, buffer, size);
+ if (ret != MM_UTIL_ERROR_NONE)
+ mm_util_error("_write_bmp failed (%d)", ret);
+
+ mm_util_fleave();
+
+ return ret;
}