No third-party decoding library is required to obtain image width and height. Complete C ++ algorithm implementation code and third-party Algorithm

Source: Internet
Author: User

No third-party decoding library is required to obtain image width and height. Complete C ++ algorithm implementation code and third-party Algorithm

In specific application scenarios, sometimes we just want to get the image width and height,

However, you do not want to obtain this information by decoding the image.

You must know the width and height of the image in advance to accelerate image loading, preprocessing, and other operations to improve the user experience.

There is a discussion in stackoverflow.

Get Image size WITHOUT loading image into memory
Http://stackoverflow.com/questions/15800704/python-get-image-size-without-loading-image-into-memory/

The image size is obtained without adding the image to the memory.

This technique has some practical value, and the bloggers have developed corresponding codes accordingly.

 

The commonly used image formats (png, jpeg, ico, bmp, and gif) are parsed using the get_image_size_without_decode_image function instead of third-party decoding libraries.

Bool get_image_size_without_decode_image (const char * file_path, int * width, int * height );

Complete code:

# Include <stdio. h> # include <sys/stat. h> unsigned long byteswap_ulong (unsigned long I) {unsigned int j; j = (I <24); j + = (I <8) & 0x00FF0000; j + = (I> 8) & 0x0000FF00; j + = (I> 24); return j;} inline int Abs (int x) {return (x ^ (x> 31)-(x> 31);} unsigned short byteswap_ushort (unsigned short I) {unsigned short j; j = (I <8); j + = (I> 8); return j;} // Get Image size WITHOUT loadin G image into memory // ref: http://stackoverflow.com/questions/15800704/python-get-image-size-without-loading-image-into-memory/// blog: http://tntmonks.cnblogs.com // mail: gaozhihan@vip.qq.com bool get_image_size_without_decode_image (const char * file_path, int * width, int * height) {bool has_image_size = false; * height =-1; * width =-1; int file_size =-1; FILE * fp = fopen (file_path, "rb"); if (fp = NULL) return has_image_size; struct stat st; char sigBuf [26]; if (fstat (fileno (fp), & st) <0) {fclose (fp); return has_image_size ;} else {file_size = st. st_size;} if (fread (& sigBuf, 26, 1, fp) <1) {fclose (fp); return has_image_size ;} char * png_signature = "\ 211PNG \ r \ n \ 032 \ n"; unsigned char ihdr_signature [4] = {'I', 'h', 'D ', 'R'}; char * gif87_signature = "GIF87a"; char * gif89_signature = "GIF89a"; char * jpeg_signature = "\ 377 \ 330"; char * BMP _signature = "BM"; if (file_size> = 10) & (memcmp (sigBuf, gif87_signature, strlen (gif87_signature) = 0 | memcmp (sigBuf, gif89_signature, strlen (gif89_signature) = 0) {// image type: gif unsigned short * size_info = (unsigned short *) (sigBuf + 6); * width = size_info [0]; * height = size_info [1]; has_image_size = true ;} else if (file_size> = 24) & (memcmp (sigBuf, png_signature, strlen (png_signature) = 0 & memcmp (sigBuf + 12, ihdr_signature, strlen (ihdr_signature )) = 0) {// image type: png unsigned long * size_info = (unsigned long *) (sigBuf + 16); * width = byteswap_ulong (size_info [0]); * height = byteswap_ulong (size_info [1]); has_image_size = true;} else if (file_size> = 16) & (memcmp (sigBuf, png_signature, strlen (png_signat Ure) = 0) {// image type: old png unsigned long * size_info = (unsigned long *) (sigBuf + 8 ); * width = byteswap_ulong (size_info [0]); * height = byteswap_ulong (size_info [1]); has_image_size = true;} else if (file_size> = 2) & (memcmp (sigBuf, pai_signature, strlen (pai_signature) = 0) {// image type: jpeg fseek (fp, 0, SEEK_SET); char B = 0; fread (& sigBuf, 2, 1, fp); fread (& B, 1, 1, fp); int W =-1; int h =-1; while (B & (unsigned char) B & 0xff )! = 0xDA) {while (unsigned char) B & 0xff )! = 0xFF) {fread (& B, 1, 1, fp);} while (unsigned char) B & 0xff) = 0xFF) {fread (& B, 1, 1, fp);} if (unsigned char) B & 0xff) >=0xc0 & (unsigned char) B & 0xff) <= 0xC3) {fread (& sigBuf, 3, 1, fp); fread (& sigBuf, 4, 1, fp); unsigned short * size_info = (unsigned short *) (sigBuf ); h = byteswap_ushort (size_info [0]); w = byteswap_ushort (size_info [1]);} else {unsigned short chunk_size = 0; fread (& Chunk_size, 2, 1, fp); if (fseek (fp, byteswap_ushort (chunk_size)-2, SEEK_CUR )! = 0) break;} fread (& B, 1, 1, fp);} if (w! =-1 & h! =-1) {* width = w; * height = h;} has_image_size = true;} else if (file_size> = 26) & (memcmp (sigBuf, BMP _signature, strlen (bmp _signature) = 0) {// image type: bmp unsigned int header_size = (* (sigBuf + 14); if (header_size = 12) {unsigned short * size_info = (unsigned short *) (sigBuf + 18); * width = size_info [0]; * height = size_info [1];} else if (header_size> = 40) {unsigned int * size_ I Nfo = (unsigned int *) (sigBuf + 18); * width = size_info [0]; * height = Abs (size_info [1]);} has_image_size = true ;} else if (file_size> = 2) {// image type: ico fseek (fp, 0, SEEK_SET); unsigned short format =-1; unsigned short reserved =-1; fread (& reserved, 2, 1, fp); fread (& format, 2, 1, fp); if (reserved = 0 & format = 1) {unsigned short num =-1; fread (& num, 2, 1, fp); if (num> 1) {pr Intf ("ico contains multiple images");} else {char w = 0, h = 0; fread (& w, 1, 1, fp); fread (& h, 1, 1, fp); * width = int (unsigned char) w & 0xff); * height = int (unsigned char) h & 0xff );}} has_image_size = true;} if (fp! = NULL) fclose (fp); return has_image_size ;}

Call method:

Const char * file_path = "d: \ test.png ";

Int h, w;
Get_image_size_without_decode_image (file_path, & w, & h );

When the position of the input image is-1, the resolution fails.

The code is relatively simple and does not need to be commented out.

If you have any other questions or requirements, contact us via email.

The email address is:
Gaozhihan@vip.qq.com

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.