forum.boolean.name

forum.boolean.name (http://forum.boolean.name/index.php)
-   FAQ и уроки (http://forum.boolean.name/forumdisplay.php?f=110)
-   -   Заголовок PNG (http://forum.boolean.name/showthread.php?t=18901)

Жека 31.01.2014 12:16

Заголовок PNG
 
Иногда бывает полезно знать в каком формате сохранена png-картинка. В частности, используется ли в ней палитра или произвольный набор цветов, а также есть ли альфа-канал.
Я для этого написал класс. Может кому-то пригодится.

Код:
Код:

Type TPngIHDR
        Global w:Int, h:Int
        Global depth:Byte, colorType:Byte, compression:Byte, filter:Byte, interlace:Byte
               
        Function readInfo(filename:String)
                Local f:TStream = OpenStream(filename)
                f.SkipBytes(8) 'признак пнг-файла
                Local buf:Byte[4]
                f.ReadBytes(buf, 4) 'length
                f.ReadBytes(buf, 4) 'ihdr
                f.ReadBytes(buf, 4)
                w = bytes2int(buf) 'w
                f.ReadBytes(buf, 4)
                h = bytes2int(buf) 'h
                depth = f.ReadByte() 'bit depth
                colorType = f.ReadByte() 'color type
                compression = f.ReadByte() 'compression method
                filter = f.ReadByte() 'filter method
                interlace = f.ReadByte() 'interlace method
                CloseStream(f)
        End Function
       
        Function hasPlte:Int()
                Return (colorType = 3)
        End Function
       
        Function hasAlpha:Int()
                Return (colorType = 4 Or colorType = 6)
        End Function

        Function bytes2int:Int(buf:Byte[])
                Return (((buf[0] Shr 24) & $ff000000) | ((buf[1] Shr 16) & $00ff0000) | ((buf[2] Shr 8) & $0000ff00) Or buf[3])
        End Function
       
End Type

Пример использования:
Код:

Local path:String = "gfx/bullet.png"
TPngIHDR.readInfo(file)
Local plte:Int = TPngIHDR.hasPlte()
Local alpha:Int = TPngIHDR.hasAlpha()
if (plte)
    'что-то делаем так
else
    'что-то делаем эдак
    if (alpha)
        '
    endif
endif

Ссылки по теме:
Стандарт PNG (eng.)
Статья на хабре (рус.)


Часовой пояс GMT +4, время: 07:59.

vBulletin® Version 3.6.5.
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Перевод: zCarot