fpwritebmp.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. {*****************************************************************************}
  2. {
  3. $Id$
  4. This file is part of the Free Pascal's "Free Components Library".
  5. Copyright (c) 2003 by Mazen NEIFER of the Free Pascal development team
  6. BMP 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 FPWriteBMP;
  16. interface
  17. uses FPImage, classes, sysutils;
  18. type
  19. TFPWriterBMP = class (TFPCustomImageWriter)
  20. private
  21. BytesPerPixel:Integer;
  22. procedure SetColorSize (AValue : byte);
  23. protected
  24. procedure InternalWrite (Stream:TStream; Img:TFPCustomImage); override;
  25. public
  26. constructor Create; override;
  27. end;
  28. implementation
  29. uses BMPcomn;
  30. constructor TFPWriterBMP.create;
  31. begin
  32. inherited create;
  33. BytesPerPixel := 3
  34. end;
  35. procedure TFPWriterBMP.SetColorSize (AValue : byte);
  36. begin
  37. if AValue >= 3
  38. then
  39. BytesPerPixel := 3
  40. else if AValue = 0
  41. then
  42. BytesPerPixel := 1
  43. else
  44. BytesPerPixel := AValue;
  45. end;
  46. procedure TFPWriterBMP.InternalWrite (Stream:TStream; Img:TFPCustomImage);
  47. function SaveHeader(stream:TStream):boolean;
  48. var
  49. BFH:TBitMapFileHeader;
  50. BFI:TBitMapInfoHeader;
  51. begin
  52. SaveHeader := false;
  53. with BFI do
  54. begin
  55. Size:=sizeof(TBitMapInfoHeader);
  56. Width:=Img.Width;
  57. Height:=Img.Height;
  58. Planes:=1;
  59. BitCount:=BytesPerPixel SHL 3;
  60. Compression:=0;
  61. SizeImage:=Width*Height;
  62. XPelsPerMeter:=100;
  63. YPelsPerMeter:=100;
  64. ClrUsed:=0;
  65. ClrImportant:=0;
  66. end;
  67. with BFH do
  68. begin
  69. bfType:=BMmagic;//'BM'
  70. bfOffset:=sizeof(TBitMapFileHeader)+sizeof(TBitMapInfoHeader);
  71. bfReserved:=0;
  72. bfSize:=bfOffset+BFI.SizeImage*BytesPerPixel;
  73. end;
  74. stream.seek(0,soFromBeginning);
  75. stream.Write(bfh,sizeof(TBitMapFileHeader));
  76. stream.Write(bfi,sizeof(TBitMapInfoHeader));
  77. if(bfi.bitCount = 8)
  78. then
  79. begin
  80. // stream.Write(Palet, bfh.bfOffset - 54);
  81. end;
  82. SaveHeader := true;
  83. end;
  84. var
  85. Row,Coulumn,nBpLine,WriteSize:Integer;
  86. aColor:TFPcolor;
  87. {$IFDEF UseDynArray}
  88. aLine:ARRAY OF TColorRGB;
  89. {$ELSE UseDynArray}
  90. aLine:^TColorRGB;
  91. {$ENDIF UseDynArray}
  92. begin
  93. SaveHeader(Stream);
  94. nBpLine:=Img.Width*SizeOf(TColorRGB);
  95. WriteSize:=(nBpLine+3)AND $FFFFFFFC;//BMP needs evry line 4Bytes aligned
  96. {$IFDEF UseDynArray}
  97. SetLength(aLine,Img.Width+1);//3 extra byte for BMP 4Bytes alignement.
  98. {$ELSE UseDynArray}
  99. GetMem(aLine,(Img.Width+1)*SizeOf(TColorRGB));//3 extra byte for BMP 4Bytes alignement.
  100. {$ENDIF UseDynArray}
  101. for Row:=img.Height-1 downto 0 do
  102. begin
  103. for Coulumn:=0 to img.Width-1 do
  104. with aLine[Coulumn],aColor do
  105. begin
  106. aColor := img.colors[Coulumn,Row];
  107. {Use only the high byte to convert the color}
  108. R:=(Red and $FF00) shr 8;
  109. G:=(Green and $FF00) shr 8;
  110. B:=(Blue and $FF00) shr 8;
  111. end;
  112. Stream.Write(aLine{$IFNDEF UseDynArray}^{$ENDIF UseDynArray},WriteSize);
  113. end;
  114. {$IFNDEF UseDynArray}
  115. FreeMem(aLine,(Img.Width+1)*SizeOf(TColorRGB));
  116. {$ENDIF UseDynArray}
  117. end;
  118. initialization
  119. ImageHandlers.RegisterImageWriter ('BMP Format', 'bmp', TFPWriterBMP);
  120. end.
  121. {
  122. $Log$
  123. Revision 1.5 2003-09-09 11:28:23 mazen
  124. * fixing copyright section in the file header
  125. Revision 1.4 2003/09/08 14:08:48 mazen
  126. - all common defintions are now included into bmpcomn unit
  127. - removed erronous code (causing exception)
  128. Revision 1.3 2003/09/08 10:38:56 luk
  129. - removed debug info
  130. * prevented exceptions when using non indexed images
  131. Revision 1.2 2003/09/04 22:29:43 luk
  132. * correct color conversion (prevent range check errors)
  133. Revision 1.1 2003/09/04 12:02:21 mazen
  134. + fpwritebmp.pas renamed to fpwritebmp.pp
  135. Revision 1.1 2003/09/04 08:44:32 mazen
  136. + Adds support of writing BMP files
  137. }