CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    how get the JPG image size?

    from these page: https://www.fileformat.info/format/jpeg/egff.htm
    i get the JPG file format:
    Code:
    typedef struct _JFIFHeader{
      BYTE SOI[2];          /* 00h  Start of Image Marker     */
      BYTE APP0[2];         /* 02h  Application Use Marker    */
      BYTE Length[2];       /* 04h  Length of APP0 Field      */
      BYTE Identifier[5];   /* 06h  "JFIF" (zero terminated) Id String */
      BYTE Version[2];      /* 07h  JFIF Format Revision      */
      BYTE Units;           /* 09h  Units used for Resolution */
      BYTE Xdensity[2];     /* 0Ah  Horizontal Resolution     */
      BYTE Ydensity[2];     /* 0Ch  Vertical Resolution       */
      BYTE XThumbnail;      /* 0Eh  Horizontal Pixel Count    */
      BYTE YThumbnail;      /* 0Fh  Vertical Pixel Count      */ } JFIFHEAD;
    but i need a correction: the Xdensity isn't the horizontal size?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: how get the JPG image size?

    From the documentation

    Units, Xdensity, and Ydensity identify the unit of measurement used to describe the image resolution. Units may be 01h for dots per inch, 02h for dots per centimeter, or 00h for none (use measurement as pixel aspect ratio). Xdensity and Ydensity are the horizontal and vertical resolution of the image data, respectively. If the Units field value is 00h, the Xdensity and Ydensity fields will contain the pixel aspect ratio (Xdensity : Ydensity) rather than the image resolution. Because non-square pixels are discouraged for portability reasons, the Xdensity and Ydensity values normally equal 1 when the Units value is 0.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how get the JPG image size?

    1 st i must found the the SOF marker that contains the image dimensions.
    Code:
    while (FileImage)            {
                    // seek for next marker
                    int b;
                    while (FileImage and (     FileImage.get()  != 0xFF)) /* no body */ ;
                    while (FileImage and ((b = FileImage.get()) == 0xFF)) /* no body */ ;
    
    
                    // the SOF marker contains the image dimensions
                    if ((0xC0 <= b) and (b <= 0xCF) and (b != 0xC4) and (b != 0xC8) and (b != 0xCC))
                    {
                        FileImage.seekg( 3, std::ios::cur );
                        height = big_endian::read_word( FileImage );
                        width  = big_endian::read_word( FileImage );
                    }
    
    
                    // Otherwise we must skip stuff (like thumbnails...)
                    else
                    {
                      FileImage.seekg( big_endian::read_word( FileImage ) - 2, std::ios::cur );
                    }
                  }
    the big_endian() and little_endian():
    Code:
    namespace little_endian
    {
      unsigned read_word( std::istream& ins )
      {
        unsigned a = ins.get();
        unsigned b = ins.get();
        return b << 8 | a;
      }
    }
    
    
    namespace big_endian
    {
      unsigned read_word( std::istream& ins )
      {
        unsigned a = ins.get();
        unsigned b = ins.get();
        return a << 8 | b;
      }
    }
    can explain to me these 2 functions?

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: how get the JPG image size?

    Do you understand the terms bigendian and littleendian? if not, do an internet search.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how get the JPG image size?

    from what i read, have to with data order

  6. #6
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how get the JPG image size?

    big endian is from 0 to 10 and little endian is from 10 to 0

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how get the JPG image size?

    i get these, but why i must use for read data on image files?

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: how get the JPG image size?

    When dealing with binary files that store numbers (integers), you have to know the byte ordering of both the binary data and the CPU used in order to correctly read and obtain numbers.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how get the JPG image size?

    and the file information tells us what is used.
    i tried do the same for ICO but don't works
    https://en.wikipedia.org/wiki/ICO_(file_format)
    from that i the byte 6 give me the width and the 7 height... so i did these code:
    Code:
    else if(strFormat== "ICO"|| strFormat=="ico")    {
            FileImage.seekg(2);
            if(little_endian::read_word( FileImage )==1) //testing if is realy an ICO
            {
                FileImage.seekg( 6 );
                height = little_endian::read_word( FileImage );
                FileImage.seekg( 7);
                width  = little_endian::read_word( FileImage );
    
    
                cout <<"File format ICO:\n" << "Width: " << width << "\nHeight: " << height;
            }
        }
    and yes it uses the little endian

  10. #10
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how get the JPG image size?

    the output is both zero and i don't know why
    Last edited by Cambalinho; March 22nd, 2020 at 12:18 PM. Reason: left information

  11. #11
    Join Date
    Nov 2018
    Posts
    121

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured