PNGファイルの幅と高さ

http://www14.ocn.ne.jp/~setsuki/ext/png.htm


/**
* PNGファイルの画像サイズを取得する
*/
bool getPngSize(LPCTSTR path, UINT* width, UINT* height)
{
FILE* f = _tfopen(path, _T("rb"));
if(!f) return false;

BYTE header[8];// PNGファイルシグネチャ
if(fread(header, sizeof(BYTE), 8, f) < 8){ fclose(f); return false; }

const static BYTE png[] = { 0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a };
if(memcmp(header, png, 8) != 0){ fclose(f); return false; }

BYTE ihdr[25];// IHDRチャンク(イメージヘッダ)
if(fread(ihdr, sizeof(BYTE), 25, f) < 25){ fclose(f); return false; }

// length = 13 (0x0D)
const static BYTE length[] = { 0x00, 0x00, 0x00, 0x0D };
if(memcmp(ihdr, length, 4) != 0){ fclose(f); return false; }

// IHDR
if(memcmp(ihdr+4, "IHDR", 4) != 0){ fclose(f); return false; }

BYTE* p;

DWORD w;
p = (BYTE*)&w;
p[0] = ihdr[8+3];
p[1] = ihdr[8+2];
p[2] = ihdr[8+1];
p[3] = ihdr[8+0];

DWORD h;
p = (BYTE*)&h;
p[0] = ihdr[12+3];
p[1] = ihdr[12+2];
p[2] = ihdr[12+1];
p[3] = ihdr[12+0];

*width  = (UINT)w;
*height = (UINT)h;

fclose(f);
return true;
}
最終更新:2012年04月25日 10:57
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。