Recent Changes - Search:

Support

Site

Links

FAQ /

ImageFormatRelated

<< Alpha transparency | Frequently asked questions | Resampling >>

Image formats in general

The Graphics32 library only supports BMP files natively. Alternatively files can be handled using TBitmap internally and thus loading of all supported TBitmap file formats such as JPG, GIF and PNG is possible using the VCL. However, since reading and writing utilizes an intermediate TBitmap object, unnecessary additional memory is required to store the bitmap data. Also by converting data to and from the TBitmap format additional information might get lost or handled wrong.

1. I can not load JPEG images at run-time, but at design-time this does work. What's wrong?

Include JPEG unit into the uses clause in your main application unit.

2. Is there a PNG library for GR32 which supports loading the image and its alpha channel at the same time?

To handle PNG files natively by Graphics32 the thirdparty library GR32 PNG Library can be used. This library use the native PNG format as intermediate storage. By assigning a TBitmap32 object the data is encoded/decoded to/from PNG on the fly. Using this library it is also possible to access additional information, which is stored in the PNG file. It's also possible to transcode the data to different encodings.

3. I want to support as much other file formats as possible! What options do I have?

You might want to take a look at GraphicEx. It loads several different file formats, but only write a few of them. For example loading PNGs with alpha channel is supported out of the box, but the PNG support in GraphicEx is very basic and there is no way to save back to PNG (see 3). for more information on how to load PNG files).

Alternatively it is possible to load other file formats using GDI+.

4. What's up with the TPNGImage library?

In the past a library for handling PNG files was available called PNGImage (using a TPNGImage/TPNGObject). It was brought by Embarcadero and is now part of Delphi. The original open source project has been shut down. Since it is based on a TBitmap, it can be used with Graphics32, but doesn't handle transparency out of the box.

Due to this reason is not recommended to use this library in the first place (see 2). However, if required you can use the following code:

unit GR32_PNG;

interface

uses
  SysUtils, Windows, Classes, GR32, Graphics;

procedure LoadPNGintoBitmap32(DstBitmap: TBitmap32; Filename: String; out AlphaChannelUsed: Boolean); overload;
procedure LoadPNGintoBitmap32(DstBitmap: TBitmap32; SrcStream: TStream; out AlphaChannelUsed: Boolean); overload;

implementation

uses PNGImage;

procedure LoadPNGintoBitmap32(DstBitmap: TBitmap32; SrcStream: TStream; out AlphaChannelUsed: Boolean);
var
  PNGObject: TPNGObject;
  TransparentColor: TColor32;
  PixelPtr: PColor32;
  AlphaPtr: PByte;
  X, Y: Integer;
begin
  PNGObject := nil;
  try
    PNGObject := TPngObject.Create;
    PNGObject.LoadFromStream(SrcStream);

    DstBitmap.Assign(PNGObject);
    DstBitmap.ResetAlpha;

    case PNGObject.TransparencyMode of
      ptmPartial:
        begin
          if (PNGObject.Header.ColorType = COLOR_GRAYSCALEALPHA) or
             (PNGObject.Header.ColorType = COLOR_RGBALPHA) then
          begin
            PixelPtr := PColor32(@DstBitmap.Bits[0]);
            for Y := 0 to DstBitmap.Height - 1 do
            begin
              AlphaPtr := PByte(PNGObject.AlphaScanline[Y]);
              for X := 0 to DstBitmap.Width - 1 do
              begin
                PixelPtr^ := (PixelPtr^ and $00FFFFFF) or (TColor32(AlphaPtr^) shl 24);
                Inc(PixelPtr);
                Inc(AlphaPtr);
              end;
            end;

            AlphaChannelUsed := True;
          end;
        end;
      ptmBit:
        begin
          TransparentColor := Color32(PNGObject.TransparentColor);
          PixelPtr := PColor32(@DstBitmap.Bits[0]);
          for X := 0 to DstBitmap.Height * DstBitmap.Width - 1 do
          begin
            if PixelPtr^ = TransparentColor then
              PixelPtr^ := PixelPtr^ and $00FFFFFF;
            Inc(PixelPtr);
          end;

          AlphaChannelUsed := True;
        end;
      ptmNone:
        AlphaChannelUsed := False;
    end;
  finally
    if Assigned(PNGObject) then PNGObject.Free;
  end;
end;

procedure LoadPNGintoBitmap32(DstBitmap: TBitmap32; Filename: String; out AlphaChannelUsed: Boolean);
var
  FileStream: TFileStream;
begin
  FileStream := TFileStream.Create(Filename, fmOpenRead);
  try
    LoadPNGintoBitmap32(DstBitmap, FileStream, AlphaChannelUsed);
  finally
    FileStream.Free;
  end;
end;

end.

Code usage example:

var
  AlphaChannelUsed: Boolean
begin
  LoadPNGintoBitmap32(MyBitmap, 'someimage.png', AlphaChannelUsed);
  if AlphaChannelUsed then
    // add anything else that should be done if we have
    // an alpha channel with our PNG image...
    MyBitmap.DrawMode := dmBlend
  else
    MyBitmap.DrawMode := dmOpaque;
end;
Edit - History - Print - Recent Changes - Search
Page last modified on September 06, 2012, at 03:07 PM