From: jiyong.min Date: Mon, 16 Jul 2018 09:43:01 +0000 (+0900) Subject: Code optimization and fix typo X-Git-Tag: submit/tizen/20180802.005217^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7678602bf9d8352fa2baddc4f88ae039a0545c8f;p=platform%2Fcore%2Fmultimedia%2Flibmm-utility.git Code optimization and fix typo - Remove unused *pWidth and *pHeight variable - Replace multiple operand to shift operand due to overflow result - Fix typo and change logs Change-Id: Ifc7ceef3ef87159d6eaabca217ebea1a014d6681 --- diff --git a/magick/mm_util_info.c b/magick/mm_util_info.c index 89f8304..af2d00a 100755 --- a/magick/mm_util_info.c +++ b/magick/mm_util_info.c @@ -60,25 +60,36 @@ static unsigned int _IfegReadUINT(unsigned char *pBuffer) return (((*pBuffer) << 24) | ((*(pBuffer + 1)) << 16) | ((*(pBuffer + 2)) << 8) | (*(pBuffer + 3))); } +static unsigned int _ReadBE16bitsToUINT(unsigned char *pBuffer) +{ + return ((*pBuffer) << 8 | (*(pBuffer + 1))); +} + +static unsigned int _ReadLE16bitsToUINT(unsigned char *pBuffer) +{ + return ((*pBuffer) | ((*(pBuffer + 1)) << 8)); +} + +static unsigned int _ReadLE32bitsToUINT(unsigned char *pBuffer) +{ + return ((*pBuffer) | ((*(pBuffer + 1)) << 8) | ((*(pBuffer + 2)) << 16) | ((*(pBuffer + 3)) << 24)); +} + static int _ImgGetImageInfo(FILE *hFile, unsigned long fileSize, char *fileExt, mm_util_img_codec_type *type, unsigned int *width, unsigned int *height) { unsigned long fileread; unsigned char EncodedDataBuffer[4096]; - unsigned int *pWidth = NULL; - unsigned int *pHeight = NULL; int ret = MM_UTIL_ERROR_NONE; mm_util_retvm_if((type == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "type is null"); mm_util_retvm_if((width == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "width is null"); mm_util_retvm_if((height == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "height is null"); - pWidth = width; - pHeight = height; - + /* initialize the result */ *type = IMG_CODEC_UNKNOWN_TYPE; - *pWidth = 0; - *pHeight = 0; + *width = 0; + *height = 0; memset(EncodedDataBuffer, 0, 4096); @@ -93,7 +104,7 @@ static int _ImgGetImageInfo(FILE *hFile, unsigned long fileSize, char *fileExt, rewind(hFile); unsigned short block_length = EncodedDataBuffer[4] * 256 + EncodedDataBuffer[5]; - mm_util_debug("block length : %d", block_length); + mm_util_sec_debug("block length : %u", block_length); unsigned long long i = 4; if (fseek(hFile, block_length + 4, SEEK_CUR) < 0) { @@ -105,7 +116,7 @@ static int _ImgGetImageInfo(FILE *hFile, unsigned long fileSize, char *fileExt, i += block_length; if (i >= fileSize) { mm_util_warn("Failed to get w / h from jpeg at index [%llu]", i); - break; + break; } memset(header_type, 0, JPG_HEADER_TYPE_LENGTH); @@ -124,16 +135,16 @@ static int _ImgGetImageInfo(FILE *hFile, unsigned long fileSize, char *fileExt, fileread = fread(image_size, sizeof(char), JPG_IMAGE_SIZE_LENGTH, hFile); mm_util_retvm_if((fileread < JPG_IMAGE_SIZE_LENGTH), ret, "IMG_CODEC_UNKNOWN_TYPE in JPEG"); - *pWidth = image_size[5] * 256 + image_size[6]; - *pHeight = image_size[3] * 256 + image_size[4]; + *width = _ReadBE16bitsToUINT(image_size + 5); + *height = _ReadBE16bitsToUINT(image_size + 3); break; } else { i += JPG_HEADER_TYPE_LENGTH; memset(block_size, 0, JPG_BLOCK_SIZE_LENGTH); fileread = fread(block_size, sizeof(char), JPG_BLOCK_SIZE_LENGTH, hFile); mm_util_retvm_if((fileread < JPG_BLOCK_SIZE_LENGTH), ret, "IMG_CODEC_UNKNOWN_TYPE in JPEG"); - block_length = ((block_size[0] << 8 & 0xFF00) | (block_size[1] & 0xFF)); - mm_util_debug("new block length : %d", block_length); + block_length = (unsigned short)_ReadBE16bitsToUINT(block_size); + mm_util_sec_debug("new block length : %u", block_length); if (fseek(hFile, block_length - JPG_BLOCK_SIZE_LENGTH, SEEK_CUR) < 0) { mm_util_error("fseek was failed"); @@ -141,30 +152,19 @@ static int _ImgGetImageInfo(FILE *hFile, unsigned long fileSize, char *fileExt, } } } - mm_util_debug("IMG_CODEC_JPEG : W[%d] H[%d]", *pWidth, *pHeight); + mm_util_sec_debug("IMG_CODEC_JPEG"); *type = IMG_CODEC_JPEG; } /*********************** PNG *************************/ else if (memcmp(EncodedDataBuffer, gIfegPNGHeader, PNG_HEADER_LENGTH) == 0) { - unsigned char tmp; - fileread = fread(EncodedDataBuffer, sizeof(char), 32, hFile); mm_util_retvm_if((fileread < 32), ret, "IMG_CODEC_UNKNOWN_TYPE in PNG"); - /* Get Image Width */ - if (pWidth) - *pWidth = _IfegReadUINT((EncodedDataBuffer + 8)); - /* Get Image Height */ - if (pHeight) - *pHeight = _IfegReadUINT((EncodedDataBuffer + 12)); + /* Get Image width & height */ + *width = _IfegReadUINT((EncodedDataBuffer + 8)); + *height = _IfegReadUINT((EncodedDataBuffer + 12)); - /* Read Interlace byte */ - tmp = *(EncodedDataBuffer + 20); - /* If image is interlaced then multiple should be 2 */ - if (tmp) - mm_util_debug("Interlaced PNG Image."); - - mm_util_debug("IMG_CODEC_PNG"); + mm_util_sec_debug("IMG_CODEC_PNG"); *type = IMG_CODEC_PNG; } /*********************** BMP *************************/ @@ -172,18 +172,12 @@ static int _ImgGetImageInfo(FILE *hFile, unsigned long fileSize, char *fileExt, /* Parse BMP File and get image width and image height */ fileread = fread(&EncodedDataBuffer[8], sizeof(char), 18, hFile); mm_util_retvm_if((fileread < 18), ret, "IMG_CODEC_UNKNOWN_TYPE in BMP"); - if (pWidth) { - *pWidth = - EncodedDataBuffer[18] | (EncodedDataBuffer[19] << 8) - | (EncodedDataBuffer[20] << 16) | - (EncodedDataBuffer[21] << 24); - } - if (pHeight) { - // add the reference function abs(). may have negative height values in bmp header. - *pHeight = abs(EncodedDataBuffer[22] | (EncodedDataBuffer[23] << 8) | (EncodedDataBuffer[24] << 16) | (EncodedDataBuffer[25] << 24)); - } - mm_util_debug("IMG_CODEC_BMP"); + *width = _ReadLE32bitsToUINT(EncodedDataBuffer + 18); + // add the reference function abs(). may have negative height values in bmp header. + *height = abs(_ReadLE32bitsToUINT(EncodedDataBuffer + 22)); + + mm_util_sec_debug("IMG_CODEC_BMP"); *type = IMG_CODEC_BMP; } /*********************** GIF *************************/ @@ -202,26 +196,22 @@ static int _ImgGetImageInfo(FILE *hFile, unsigned long fileSize, char *fileExt, return ret; } - *pWidth = EncodedDataBuffer[6] | (EncodedDataBuffer[7] << 8); - *pHeight = EncodedDataBuffer[8] | (EncodedDataBuffer[9] << 8); + *width = _ReadLE16bitsToUINT(EncodedDataBuffer + 6); + *height = _ReadLE16bitsToUINT(EncodedDataBuffer + 8); - mm_util_debug("Logical width : %d, Height : %d", *pWidth, *pHeight); - - mm_util_debug("IMG_CODEC_GIF"); + mm_util_sec_debug("IMG_CODEC_GIF"); *type = IMG_CODEC_GIF; } /*********************** WBMP *************************/ else if ((memcmp(EncodedDataBuffer, gIfegWBMPHeader, WBMP_HEADER_LENGTH) == 0) && (strcasecmp(fileExt, "wbmp") == 0)) { /* Parse BMP File and get image width and image height */ - if (pWidth) - *pWidth = EncodedDataBuffer[2]; - - if (pHeight) - *pHeight = EncodedDataBuffer[3]; + *width = EncodedDataBuffer[2]; + *height = EncodedDataBuffer[3]; - mm_util_debug("IMG_CODEC_WBMP W[%d] H[%d]", *pWidth, *pHeight); + mm_util_sec_debug("IMG_CODEC_WBMP"); *type = IMG_CODEC_WBMP; } + mm_util_sec_debug("Image Width : %u, Height : %u", *width, *height); return ret; }