fpwritetga.pp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. {*****************************************************************************}
  2. {
  3. $Id$
  4. This file is part of the Free Pascal's "Free Components Library".
  5. Copyright (c) 2003 by Michael Van Canneyt of the Free Pascal development team
  6. TARGA writer implementation.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. }
  13. {*****************************************************************************}
  14. {$mode objfpc}{$h+}
  15. unit FPWriteTGA;
  16. interface
  17. uses FPImage, classes, sysutils;
  18. type
  19. TFPWriterTarga = class (TFPCustomImageWriter)
  20. protected
  21. function SaveHeader(Stream:TStream; Img: TFPCustomImage):boolean; virtual;
  22. procedure InternalWrite (Stream:TStream; Img: TFPCustomImage); override;
  23. end;
  24. implementation
  25. uses targacmn;
  26. function TFPWriterTarga.SaveHeader(Stream:TStream; Img : TFPCustomImage):boolean;
  27. var
  28. Header : TTargaHeader;
  29. ID : ShortString;
  30. begin
  31. Result:=False;
  32. ID:=Img.Extra[KeyIdentification];
  33. FillChar(Header,SizeOf(Header),0);
  34. With Header do
  35. begin
  36. IDLen:=Length(ID);
  37. MapType:=0; // No colormap. Uncompressed RGB Only.
  38. ImgType:=2; // Uncompressed RGB
  39. MapStart:=FromWord(0); // No data
  40. MapLength:=FromWord(0); // No colormap yet.
  41. MapEntrySize:=0; // No colormap yet.
  42. OriginX:= FromWord(0);
  43. OriginY:=FromWord(0);
  44. Width:=FromWord(Img.Width);
  45. Height:=FromWord(Img.Height);
  46. PixelSize:=24; // BGR data.
  47. Flags:=$20; // Top-town, non interlaced.
  48. end;
  49. Stream.WriteBuffer(Header,SizeOf(Header));
  50. If Header.IDlen>0 then
  51. Stream.WriteBuffer(Id[1],Header.IDLen);
  52. Result:=true;
  53. end;
  54. procedure TFPWriterTarga.InternalWrite (Stream:TStream; Img:TFPCustomImage);
  55. var
  56. Row,Col,WriteSize:Integer;
  57. Aline,P: PByte;
  58. C : TFPColor;
  59. begin
  60. SaveHeader(Stream,Img);
  61. WriteSize:=Img.Width*3;
  62. GetMem(aLine,WriteSize);
  63. Try
  64. for Row:=0 to Img.Height-1 do
  65. begin
  66. P:=ALine;
  67. For Col:=0 to Img.width-1 do
  68. begin
  69. C:=Img.Colors[Col,Row];
  70. P^:=C.Blue shr 8;
  71. Inc(P);
  72. P^:=C.Green shr 8;
  73. Inc(P);
  74. P^:=C.Red shr 8;
  75. Inc(P);
  76. end;
  77. Stream.Write(aLine[0],WriteSize);
  78. end;
  79. Finally
  80. FreeMem(aLine);
  81. end;
  82. end;
  83. initialization
  84. ImageHandlers.RegisterImageWriter ('TARGA Format', 'tgha', TFPWriterTarga);
  85. end.
  86. {
  87. $Log$
  88. Revision 1.1 2004-03-02 21:26:17 michael
  89. + Implemented TARGA writing. Currently only 24 bit
  90. }