Advertisement
Guest User

Untitled

a guest
Jan 31st, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.77 KB | None | 0 0
  1.   procedure ParsePNG(const AFileName: TFileName);
  2.   var
  3.     VFile: TFileStream;
  4.     VReader: TPNGReader;
  5.     VColSpace: ShortString;
  6.     VHeight, VWidth: LongWord;
  7.     VBitDepth, VColorType: Byte;
  8.   begin
  9.     VHeight := 0;
  10.     VWidth := 0;
  11.     VBitDepth := 0;
  12.     VColorType := 0;
  13.     VFile := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
  14.     VReader := TPNGReader.Create;
  15.     try
  16.       if not VReader.CheckContents(VFile) then
  17.         raise Exception.CreateFmt('Not a PNG file: %s', [AFileName]);
  18.       if VReader.GetChunk.aType <> ctIHDR then
  19.         raise Exception.CreateFmt('Incorrect PNG file: %s', [AFileName]);
  20.       VBitDepth := VReader.Header.BitDepth;
  21.       if VBitDepth > 8 then
  22.         raise Exception.CreateFmt('16-bit depth not supported: %s', [AFileName]);
  23.       VColorType := VReader.Header.ColorType;
  24.       case VColorType of
  25.         0, 4: VColSpace := 'DeviceGray';
  26.         2, 6: VColSpace := 'DeviceRGB';
  27.         3: VColSpace := 'Indexed';
  28.       else
  29.         raise Exception.CreateFmt('Unknown color type: %s', [AFileName]);
  30.       end;
  31.       if VReader.Header.Compression <> 0 then
  32.         raise Exception.CreateFmt('Unknown compression method: %s', [AFileName]);
  33.       if VReader.Header.Filter <> 0 then
  34.         raise Exception.CreateFmt('Unknown filter method: %s', [AFileName]);
  35.       if VReader.Header.Interlace <> 0 then
  36.         raise Exception.CreateFmt('Interlacing not supported: %s', [AFileName]);
  37.       VHeight := VReader.Header.Height;
  38.       VWidth := VReader.Header.Width;
  39.  
  40.       { TODO: implement compress/uncompress, trns, pal, index, flate etc. }
  41.  
  42.       WriteLn('h: ', VHeight, ' w: ', VWidth, ' bpc: ', VBitDepth,
  43.         ' colspace: ', VColSpace);
  44.     finally
  45.       VReader.Free;
  46.       VFile.Free;
  47.     end;
  48.   end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement