Thread: how read an image file?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    434

    how read an image file?

    i'm trying reading a JPG\GIF file, it's a binary file true and i must see it's structure for it... now i have these link about JPG format image:
    JPEG File Layout and Format

    like we know the file is combined with 1 or 2 or 4 bytes on types.
    for now, i just need read the size. so heres the information:
    "SOF0 (Start Of Frame 0) marker:

    Field Size Description

    Marker Identifier 2 bytes 0xff, 0xc0 to identify SOF0 marker

    Length 2 bytes This value equals to 8 + components*3 value

    Data precision 1 byte This is in bits/sample, usually 8
    (12 and 16 not supported by most software).

    Image height 2 bytes This must be > 0

    Image Width 2 bytes This must be > 0"

    i must find the "SOF0" mark... but how can i search?
    the file data is on these variable:
    Code:
    BMPSize bmp;    FILE* img = fopen(filename, "rb");   //read the file
        char header[256];
        fread(header, sizeof(unsigned char), 256, img); // read the 54-byte header
    now heres how i test if is "SOF0":
    Code:
    size_t i =0;
    
            do
            {
                if( strcmp(header, "SOF0")==0)
                {
                    printf("hello");
                    width = *(int*)&header[i+2];
                    height= *(int*)&header[i+4];
                    break;
                }
                i++;
            }while(i<strlen(header));
    so what i'm doing wrong for compare?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    930
    You've been given more than enough information in your thread on the other site: how read the GIF and JPG image size? - C++ Forum

    And from the code you've shown you clearly don't know what you are doing, so you are basically just asking for someone to write it for you.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    434
    maybe you have right, but i'm trying understand why i'm wrong.. i'm trying learning
    i'm trying adapt that code to my code

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    434
    why allways you get that conclusion?????
    true i don't know, that's i'm asking, but tell me how i compare correctly is for make my work?????

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,824
    Hint: "Marker Name" is what that part is called; it is not in the file!
    "Marker Identifier" is the value in the file.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    434
    that's true... i'm trying understand more 2 functions then i can share some code and information..

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,974
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2017
    Posts
    5
    Didn't know this was this easy. I have the same concern with the TS. Thanks!

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    3,824
    Quote Originally Posted by stahta01 View Post
    Hint: "Marker Name" is what that part is called; it is not in the file!
    "Marker Identifier" is the value in the file.

    Tim S.
    Quote Originally Posted by joaquim View Post
    that's true...
    Code:
    if( strcmp(header, "SOF0")==0)
    Then, you know the above code is wrong!
    Why are you still using it?

    Edit2: And you are not comparing strings! Read about memcmp - C++ Reference

    Tim S.
    Last edited by stahta01; 03-24-2020 at 04:41 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merge multiple image file into one image
    By mr_empty in forum C++ Programming
    Replies: 7
    Last Post: 12-09-2009, 02:12 PM
  2. Replies: 13
    Last Post: 11-20-2009, 04:43 PM
  3. how to convert a bitmap image to a jpg image file using C++?
    By nomer in forum Windows Programming
    Replies: 4
    Last Post: 06-04-2006, 07:40 PM
  4. Image class - loading image file
    By GaPe in forum Windows Programming
    Replies: 2
    Last Post: 07-11-2004, 01:35 PM
  5. How to read a image in C++?
    By DramaKing in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 12:34 AM

Tags for this Thread