Parse the jpge file to get the file pixel size

Source: Internet
Author: User
Tags readfile

Size limit (lpctstr szfilename) {size sizepiex = {0, 0}; handle hfile = limit; do {hfile = createfile (szfilename, generic_read, file_into_write, null, open_existing, 0, null ); if (hfile = invalid_handle_value) {DWORD dwerr = getlasterror (); _ assert (false) ;}# Pragma pack (push, 1) struct _ tagimgsection_t {byte sectionstart; // It Must Be 0xff byte sectionid; // segment ID: byte sectionlenh, Sectionlenl; // 8-bit long segment // 8-bit low segment length} imgsection; # pragma pack (POP) DWORD dwreaded = 0; // The if ((! Readfile (hfile, & imgsection, 2, & dwreaded, null) | (dwreaded! = 2) | (imgsection. sectionstart! = 0xff) // unlabeled | (imgsection. sectionid! = 0xd8) // non-SOI start header | 0) {_ assert (false); break;} bool bdone = false; while (! Bdone) {memset (& imgsection, 0, sizeof (imgsection); If ((! Readfile (hfile, & imgsection, sizeof (imgsection), & dwreaded, null) | (dwreaded! = Sizeof (imgsection) | 0) break; // If (imgsection. sectionstart! = 0xff) // non-segment mark break; Switch (imgsection. sectionid) {Case (0xff): // The current ID is data {continue;} case (0xd9): // EOI, end of image, end of image {bdone = true; break;} case (0xc0): // sof0, start of frame, frame image start {# pragma pack (push, 1) // one byte of the struct is aligned with the typedef struct _ tagsof0struct_t {struct _ taghead_t {// byte wlenh, wlenl; // ① The data length is 2 bytes ① ~ ⑥ Total length of the six fields byte sample_precision; // ② precision 1 byte the number of digits of each data sample byte highpiexsh, highpiexsl; // ③ Image Height (unit: pixel) byte widthpiexsh, widthpiexsl; // ④ image width (unit: pixels) byte colortabnums; // ⑤ Number of color components} head; // ycrcb is used in jfif, therefore, here the color component quantity is constant 3 struct _ colortab {// 6 color component information color component quantity × 3 bytes (usually 9 bytes) byte Tabid; //) color component Id 1 byte vsampfactor: 4; // low 4 bits: Vertical Sampling factor byte hsampfactory: 4; // high 4 bits: horizontal sampling factor byte curtabid; // ID of the quantified table used by the current component} color Tag [1];} sof0struct; # pragma pack (POP) // restore struct alignment length sof0struct sofodata = {0}; int headlen = sizeof (sofodata. head); If ((! Readfile (hfile, & sofodata, headlen, & dwreaded, null) | dwreaded! = Sizeof (sofodata. head) break; sizepiex. cx = makeword (sofodata. head. widthpiexsl, sofodata. head. widthpiexsh); sizepiex. cy = makeword (sofodata. head. highpiexsl, sofodata. head. highpiexsh); // subsequent omitted ...... Bdone = true; break;} default: {long iSeek = (long) makeword (imgsection. sectionlenl, imgsection. sectionlenh)-2; setfilepointer (hfile, iSeek, null, file_current); break ;}}} while (0); If (hfile! = Invalid_handle_value) {closehandle (hfile);} return sizepiex ;}

// Test cfiledialog filedlg (true); If (filedlg. domodal () = idok) {size imgpiex = getjpgimagepiex (filedlg. getpathname (); cstring szmsg; szmsg. format (_ T ("% s \ n (width = % d Height = % d)"), filedlg. getfilename (), imgpiex. CX, imgpiex. cy); afxmessagebox (szmsg );}

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.