fpwritetga.pp 2.5 KB

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