delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Viewing Targa Bitmap File Format in Delphi (256-colors)Format this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Graphic
Skill Level:
Scoring:
Last Update:
03/20/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Targa File Format Viewer
Times Scored:
1
Visits:
3218
Uploader: h4ry p
Company:
Reference: www.geocities.com/h4ryp/delphi.html
 
Question/Problem/Abstract:
How to view Bitmap in Targa File Format (*.tga) using Delphi ?
Answer:



This is quite simple way to answer above question: viewing Targa file format using Delphi (not compress and limited only 256 colors).

Here is the example code:
const
  FERRORMSG2 = 'Sorry, Unsupported Compressed(RLE) File Format';
  FERRORMSG3 = 'Sorry, Unsupported More Than 256 Colours File Format';

type
  TArrBuff = Array [1..512] of Byte;
  TPalette_Cell = Record
    b2, g2, r2 : byte;
  End;
  TPal = Array [0..255] of TPalette_Cell;
  TPPal = ^TPal;
  TTGA_Header = Record  // Targa(TGA) HEADER //
    IDLength, ColorMap, ImageType : byte;
    ClrMapSpes : array[1..5] of byte;
    XAwal, YAwal, Width, Height : SmallInt;
    BpPixel, ImageDescription : byte;
  end;

var
  pal: TPPal;
  pFile: File;
  buffer: TArrBuff;
  FTgaHeader: TTGA_Header;

procedure THPTGA.ReadImageData2Bitmap;
var
  i, j, idx : integer;
begin
  Seek(pFile, sizeof(FtgaHeader)+FtgaHeader.IDLength+768);
  for i:=FtgaHeader.Height-1 downto FtgaHeader.YAwal do
  begin
    BlockRead(pFile, buffer, FtgaHeader.Width);
    for j:=FtgaHeader.XAwal to FtgaHeader.Width-1 do begin
      idx := j - FtgaHeader.XAwal + 1;
        SetPixel(Bitmap.Canvas.Handle, j, i, rgb(pal^[buffer[idx]].r2, pal^[buffer[idx]].g2, pal^[buffer[idx]].b2));
    end;
  end;
end;

procedure THPTGA.LoadFromFile(const FileName: string);
begin
  AssignFile(pFile, FileName);
  {$I-} Reset(pFile, 1); {$I+}
  if (IOResult = 0) then begin
    try
      BlockRead(pFile, FtgaHeader, SizeOf(FtgaHeader));
      // checking unsupported features here
      if (FtgaHeader.ImageType>3) then begin
        MessageBox(Application.Handle, FERRORMSG2, 'TGA Viewer Error', MB_ICONHAND);
        exit;
      end;
      if (FtgaHeader.BpPixel>8) then begin
        MessageBox(Application.Handle, FERRORMSG3, 'TGA Viewer Error', MB_ICONHAND);
        exit;
      end;
      GetMem(pal, 768);
      try
        Bitmap.Width  := FtgaHeader.Width;
        Bitmap.Height := FtgaHeader.Height;
        // if use Color-Map and Uncompressed then read it
        if (FtgaHeader.ImageType=1) then
          BlockRead(pFile, pal^, 768);
        ReadImageData2Bitmap;
      finally
        FreeMem(pal);
      end;
    finally
      CloseFile(pFile);
    end;
  end
  else MessageBox(Application.Handle, 'Error Opening File', 'TGA Viewer Error', MB_ICONHAND);
end;

How to try this code ?? Just call the "LoadFromFile" procedure above in your application (probably with little modification offcourse, especially about the name of mainForm that I used here [THPTGA]).

Hopefully It can help you.

For full source code and simple application that use this, you can look and download from my website: www.geocities.com/h4ryp/delphi.html.






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
GraphicEx.pas
    Werner Pamler (Mar 27 2002 5:12PM)

At www.lischke-online.de/Graphics.html you can download the freeware Delphi unit GraphicEx.pas which makes a bunch of graphics formats available to Delphi programs, among them PCX, TIFF, TGA, etc. The formats are embedded into Delphi in the same way as Borland's jpeg unit.
Respond

Great
    Tommy Andersen (Mar 21 2002 8:12AM)

Good to see that you've covered Targa images.

When I get the time (and if I find my old source) I'll send you an e-mail with some of my old TP image handling sources so that you can expand your targa support etc.! :)

Best Regards
Tommy
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
Peganza
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)