fpwritetga.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. {$IFNDEF FPC_DOTTEDUNITS}
  15. unit FPWriteTGA;
  16. {$ENDIF FPC_DOTTEDUNITS}
  17. interface
  18. {$IFDEF FPC_DOTTEDUNITS}
  19. uses FpImage, System.Classes, System.SysUtils;
  20. {$ELSE FPC_DOTTEDUNITS}
  21. uses FpImage, classes, sysutils;
  22. {$ENDIF FPC_DOTTEDUNITS}
  23. type
  24. TFPWriterTarga = class (TFPCustomImageWriter)
  25. protected
  26. function SaveHeader(Stream:TStream; Img: TFPCustomImage):boolean; virtual;
  27. procedure InternalWrite (Stream:TStream; Img: TFPCustomImage); override;
  28. end;
  29. implementation
  30. {$IFDEF FPC_DOTTEDUNITS}
  31. uses FpImage.Common.Targa;
  32. {$ELSE FPC_DOTTEDUNITS}
  33. uses targacmn;
  34. {$ENDIF FPC_DOTTEDUNITS}
  35. function TFPWriterTarga.SaveHeader(Stream:TStream; Img : TFPCustomImage):boolean;
  36. var
  37. Header : TTargaHeader;
  38. ID : ShortString;
  39. begin
  40. Result:=False;
  41. ID:=Img.Extra[KeyIdentification];
  42. FillChar(Header,SizeOf(Header),0);
  43. With Header do
  44. begin
  45. IDLen:=Length(ID);
  46. MapType:=0; // No colormap. Uncompressed RGB Only.
  47. ImgType:=2; // Uncompressed RGB
  48. MapStart:=FromWord(0); // No data
  49. MapLength:=FromWord(0); // No colormap yet.
  50. MapEntrySize:=0; // No colormap yet.
  51. OriginX:= FromWord(0);
  52. OriginY:=FromWord(0);
  53. Width:=FromWord(Img.Width);
  54. Height:=FromWord(Img.Height);
  55. PixelSize:=24; // BGR data.
  56. Flags:=$20; // Top-town, non interlaced.
  57. end;
  58. Stream.WriteBuffer(Header,SizeOf(Header));
  59. If Header.IDlen>0 then
  60. Stream.WriteBuffer(Id[1],Header.IDLen);
  61. Result:=true;
  62. end;
  63. procedure TFPWriterTarga.InternalWrite (Stream:TStream; Img:TFPCustomImage);
  64. var
  65. Row,Col,WriteSize:Integer;
  66. Aline,P: PByte;
  67. C : TFPColor;
  68. begin
  69. SaveHeader(Stream,Img);
  70. WriteSize:=Img.Width*3;
  71. GetMem(aLine,WriteSize);
  72. Try
  73. for Row:=0 to Img.Height-1 do
  74. begin
  75. P:=ALine;
  76. For Col:=0 to Img.width-1 do
  77. begin
  78. C:=Img.Colors[Col,Row];
  79. P^:=C.Blue shr 8;
  80. Inc(P);
  81. P^:=C.Green shr 8;
  82. Inc(P);
  83. P^:=C.Red shr 8;
  84. Inc(P);
  85. end;
  86. Stream.Write(aLine[0],WriteSize);
  87. end;
  88. Finally
  89. FreeMem(aLine);
  90. end;
  91. end;
  92. initialization
  93. ImageHandlers.RegisterImageWriter ('TARGA Format', 'tga', TFPWriterTarga);
  94. end.