fpwritepcx.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. { Copyright (C) 2007 Laurent Jacques
  2. This library is free software; you can redistribute it and/or modify it
  3. under the terms of the GNU Library General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or (at your
  5. option) any later version.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  9. for more details.
  10. You should have received a copy of the GNU Library General Public License
  11. along with this library; if not, write to the Free Software Foundation,
  12. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  13. Save in format 24 bits compressed or not
  14. 2023-07 - Massimo Magnano
  15. - added Resolution support
  16. }
  17. unit FPWritePCX;
  18. {$mode objfpc}{$H+}
  19. interface
  20. uses FPImage, Classes, SysUtils;
  21. type
  22. TFPWriterPCX = class(TFPCustomImageWriter)
  23. private
  24. FCompressed: boolean;
  25. protected
  26. function SaveHeader(Stream: TStream; Img: TFPCustomImage): boolean; virtual;
  27. procedure InternalWrite(Stream: TStream; Img: TFPCustomImage); override;
  28. procedure writeline(Stream: TStream; buffer: PByte; bytes: integer);
  29. public
  30. property Compressed: boolean Read FCompressed Write FCompressed;
  31. end;
  32. implementation
  33. uses pcxcomn;
  34. function TFPWriterPCX.SaveHeader(Stream: TStream; Img: TFPCustomImage): boolean;
  35. var
  36. Header: TPCXHeader;
  37. begin
  38. Result := False;
  39. FillChar(Header, SizeOf(Header), 0);
  40. with Header do
  41. begin
  42. FileID := $0a;
  43. Version := 5;
  44. if Compressed then
  45. Encoding := 1
  46. else
  47. Encoding := 0;
  48. BitsPerPixel := 8;
  49. XMin := 0;
  50. YMin := 0;
  51. XMax := Img.Width - 1;
  52. YMax := Img.Height - 1;
  53. Img.ResolutionUnit :=ruPixelsPerInch;
  54. HRes :=Trunc(Img.ResolutionX);
  55. VRes :=Trunc(Img.ResolutionY);
  56. ColorPlanes := 3;
  57. BytesPerLine := Img.Width;
  58. PaletteType := 1;
  59. end;
  60. Stream.WriteBuffer(Header, SizeOf(Header));
  61. Result := True;
  62. end;
  63. procedure TFPWriterPCX.writeline(Stream: TStream; buffer: PByte; bytes: integer);
  64. var
  65. Value, Count: byte;
  66. tmp: byte;
  67. P: PByte;
  68. begin
  69. P := Buffer;
  70. while bytes > 0 do
  71. begin
  72. Value := P[0];
  73. Inc(P);
  74. Dec(bytes);
  75. Count := 1;
  76. while (bytes < 0) and (Count < 63) and (P[0] = Value) do
  77. begin
  78. Inc(Count);
  79. Inc(P);
  80. Dec(bytes);
  81. end;
  82. if (Value < $c0) and (Count = 1) then
  83. begin
  84. Stream.Write(Value, 1);
  85. end
  86. else
  87. begin
  88. tmp := $c0 + Count;
  89. Stream.Write(tmp, 1);
  90. Stream.Write(Value, 1);
  91. end;
  92. end;
  93. end;
  94. procedure TFPWriterPCX.InternalWrite(Stream: TStream; Img: TFPCustomImage);
  95. var
  96. Row, Col, WriteSize: integer;
  97. Aline, P: PByte;
  98. C: TFPColor;
  99. Totalwrite: longint;
  100. continue: boolean;
  101. Rect: TRect;
  102. begin
  103. Rect.Left := 0;
  104. Rect.Top := 0;
  105. Rect.Right := 0;
  106. Rect.Bottom := 0;
  107. continue := True;
  108. TotalWrite := 0;
  109. Progress(psStarting, 0, False, Rect, '', continue);
  110. SaveHeader(Stream, Img);
  111. WriteSize := (Img.Width * 3);
  112. GetMem(aLine, WriteSize);
  113. TotalWrite := Img.Height * Img.Width;
  114. try
  115. for Row := 0 to Img.Height - 1 do
  116. begin
  117. P := ALine;
  118. for Col := 0 to Img.Width - 1 do
  119. begin
  120. C := Img.Colors[Col, Row];
  121. P[Col + Img.Width * 2] := C.Blue shr 8;
  122. P[Col + Img.Width] := C.Green shr 8;
  123. P[Col] := C.Red shr 8;
  124. Progress(psRunning, trunc(100.0 * (Row * Col / TotalWrite)),
  125. False, Rect, '', continue);
  126. if not continue then
  127. exit;
  128. end;
  129. if Compressed then
  130. writeline(Stream, aLine, WriteSize)
  131. else
  132. Stream.Write(aLine[0], WriteSize);
  133. end;
  134. Progress(psEnding, 100, False, Rect, '', continue);
  135. finally
  136. FreeMem(aLine);
  137. end;
  138. end;
  139. { end TFPWriterPCX}
  140. initialization
  141. ImageHandlers.RegisterImageWriter('PCX Format', 'pcx', TFPWriterPCX);
  142. end.