how read the GIF and JPG image size?

Pages: 123
i miss that.. it's the current position, but i continue with same problem :(
1
2
3
4
FileImage.seekg( 6 );
            height = FileImage.get()<<8;
            FileImage.seekg( 7 );
            width  =  FileImage.get()<<8;

now i'm reading 1 byte and use the little endian method. but i conitnues with zero :(
You don't seem to realize that seeking to position 6 gives you the last byte of the ICONDIR structure not the first byte of the ICONDIRENTRY structure.

Next what are the types of height and width?

What is the purpose of the shifts?


now i'm reading 1 byte and use the little endian method. but i conitnues with zero


So you still don't realize that there is no "endian method" involved when reading single bytes?

to be honest i'm confused :(
true i don't know that the single don't use it... from windows api structure, the width and height are byte types
to be honest i'm confused

Really? I would have never known.

from windows api structure, the width and height are byte types

Okay, did you read the documentation for a BYTE type?

Something like this perhaps?

BYTE A byte (8 bits). This type is declared in WinDef.h as follows: typedef unsigned char BYTE;


So do you understand what happens when you shift a BYTE left 8 bits?



yes. 1 byte = 8 bits.. so i can't shift 8 on a byte.
ok... i give up on shift the 8. but why i continue with zero?
1
2
3
4
FileImage.seekg( 6 );
            height = FileImage.get();
            FileImage.seekg( 7 );
            width  =  FileImage.get();

when is zero the image is 256X256. but the image is 128X128
Okay how are you printing out those values?

1
2
3
4
5
6
7
8
9
10
11
12
13
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 = (int)FileImage.get();
            FileImage.seekg( 7 );
            width  =  (int)FileImage.get();

            cout <<"File format ICO:\n" << "Width: " << width << "\nHeight: " << height;
        }
    }
Okay so what is with that horrible C style cast?

And what exactly does that snippet output?

And it would be better if you showed the smallest possible complete program that illustrates the problem. I really need to see exactly how all of the variables are declared.

And quit just throwing code at the problem until you understand the problem.

heres the entire function for read the image files size:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <winsock.h>

using namespace std;

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;
  }
}

struct Size
{
    int Width=0;
    int Height=0;
};

Size GetImageSize(const std::string& filename)
{

    //Get File Data:
    std::ifstream FileImage( filename, std::ios::binary );

    //Get Image Format:
    string strFormat = filename.substr( filename.length() - 3 );


    //getting the size, by it's own format:
    int width=0;
    int height=0;

    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 = (int)FileImage.get();
            FileImage.seekg( 7 );
            width  =  (int)FileImage.get();

            cout <<"File format ICO:\n" << "Width: " << width << "\nHeight: " << height;
        }
    }    

    FileImage.good();

    //return the image size:
    Size Image;
    Image.Width=width;
    Image.Height=height;
    return Image;
}

now heres how i use it:
1
2
3
4
5
6
7
8
9
10
11
int main()
{

    //initializate graphics:
    /*int gd=DETECT, gm;
    initgraph(&gd, &gm, "");*/
    initwindow(1000,1000);
    Size bmp=GetImageSize("C:\\New\\Custom-Icon-Design-Mono-General-2-Document.ico");//get the image size
readimagefile("C:\\New\\Custom-Icon-Design-Mono-General-2-Document.ico",0,0,0+bmp.Width,0+bmp.Height);//draw image
return 0;
}
when is zero the image is 256X256.

you need to stop and learn.
256 is zero in an unsigned byte.
show me the 8 bit pattern that means '256' ?
in base 2, binary, bits, whatever you want to call it... 256 is 9 bits.

if it is 128x128, that will fit in bytes, and you are doing something wrong if you don't get 128 there. It may do you a world of good to open the file in a hex editor and LOOK at its structure. What are the values, in the hex editor, of bytes # 6 and 7 or whatever they were?

Take it very slowly. Open the file in a hex editor, see what the bytes are and where they are. Make sure that 6 and 7 are not numbers from a human text that counts from 1 instead of zero, validate what you are doing. Maybe you are off by 1 due to 5/6 instead of 6/7 due to start at zero?
Last edited on
https://en.wikipedia.org/wiki/ICO_(file_format)

"ICONDIRENTRY structure
Offset# Size (in bytes) Purpose
0 1 Specifies image width in pixels. Can be any number between 0 and 255. Value 0 means image width is 256 pixels.
1 1 Specifies image height in pixels. Can be any number between 0 and 255. Value 0 means image height is 256 pixels."


"What are the values, in the hex editor, of bytes # 6 and 7 or whatever they were?"
i only know open it with Notepad.. is there another program for we see what you mean?
yes, fine 0 can mean 256. but YOU have to DO that.
if ==0 then set it 256, you need that IN your code, the computer doesn't know this special translation. If you just read it, you gonna get 0.

yes, get a hex editor (google this, find a free one, and use it). Notepad shows text only. You need to see the file in binary format. You may want to use it for a moment on a small text file to see how it works: try putting hello world with and end of line and a little more text in a file in notepad, and open that in the new tool. You will see the end of line byte(s) and the text and all and understand what you are seeing, then try the icon. end of lines are usually one or more of {10,13} or in hex, {A,D} (0X0D 0X0A) which you can see on the ascii table. You will see what I mean when you open it and look at it.
Last edited on
heres a txt file in hex way: https://files.fm/u/nwuhfzwp
By the way, a couple of quick questions:

1. Are you sure your file is actually opening properly? Since you don't test anything it may not be opening properly.

2. Why are your structure variables signed types? Do you know that performing some operations on signed variables produces undefined behavior?

This is more what I mean by the smallest possible program that illustrates the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <string.h>
#include <fstream>
#include <iostream>

using namespace std;

struct Size
{
    unsigned Width = 0;
    unsigned Height = 0;
};

Size GetImageSize(const std::string& filename)
{

    //Open data File:
    std::ifstream FileImage(filename, std::ios::binary);


    //getting the size, by it's own format:
    unsigned width = 0;
    unsigned height = 0;

    FileImage.seekg(6);
    height = FileImage.get();
    FileImage.seekg(7);
    width  =  FileImage.get();

    cout << "File format ICO:\n" << "Width: " << width << "\nHeight: " << height << '\n';

    return {width, height};
}

int main()
{
    //Size bmp = GetImageSize("C:\\New\\Custom-Icon-Design-Mono-General-2-Document.ico");//get the image size

//    Size bmp = GetImageSize("/usr/share/vlc/lua/http/favicon.ico");

    std::cout << bmp.Width << " " << bmp.Height << std::endl;
    return 0;
}


By the way using my icon file it produces values of 32 and 32 as I would expect for that file.

However don't forget that you're reading the "width" into the height variable in your code. Look at the order of the variables in your documentation.

I can't open remote files. I don't need to see it. I need you to see it for yourself. Ive edited executable files; I know how it works already.
wow i tested with another icon file, and i get the right size of the 1st(16,X16), but when i select it on explorer, i get 256X256.... what these means?
is windows and the readimagefile() reading the last icon instead the 1st?
Topic archived. No new replies allowed.
Pages: 123