fpwritebmp.pp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. {*****************************************************************************}
  2. {
  3. This file is part of the Free Pascal's "Free Components Library".
  4. Copyright (c) 2003 by Mazen NEIFER of the Free Pascal development team
  5. BMP 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. { 08/2005 by Giulio Bernardi:
  14. - Removed FBytesPerPixel, BytesPerPixel property is now deprecated, use BitsPerPixel instead.
  15. - Rewritten a large part of the file, so we can handle all bmp color depths
  16. - Support for RLE4 and RLE8 encoding
  17. }
  18. {$mode objfpc}{$h+}
  19. unit FPWriteBMP;
  20. interface
  21. uses FPImage, classes, sysutils, BMPComn;
  22. type
  23. TFPWriterBMP = class (TFPCustomImageWriter)
  24. private
  25. StartPosition : int64; { save start of bitmap in the stream, if we must go back and fix something }
  26. FBpp : byte;
  27. FRLECompress : boolean;
  28. BFH : TBitMapFileHeader;
  29. BFI : TBitMapInfoHeader;
  30. Colinfo : array of TColorRGBA;
  31. procedure SetColorSize (AValue : Byte);
  32. function GetColorSize : byte;
  33. procedure SetBpp (const abpp : byte);
  34. procedure FillColorMap(Img : TFPCustomImage);
  35. procedure Setup16bpp;
  36. function PackWord555(const col : TFPColor) : word;
  37. function PackWord565(const col : TFPColor) : word;
  38. function Pack4bpp(const img : TFPCustomImage; var Col : integer; const Row : integer) : byte;
  39. function Pack1bpp(const img : TFPCustomImage; var Col : integer; const Row : integer) : byte;
  40. procedure CompressScanLineRLE8(ALine : pbyte; const Row, Width : Integer; Stream : TStream);
  41. procedure CompressScanLineRLE4(ALine : pbyte; const Row, Width : Integer; Stream : TStream);
  42. protected
  43. function SaveHeader(Stream:TStream; Img: TFPCustomImage):boolean; virtual;
  44. procedure InternalWrite (Stream:TStream; Img: TFPCustomImage); override;
  45. public
  46. constructor Create; override;
  47. property BitsPerPixel : byte read FBpp write SetBpp;
  48. property RLECompress : boolean read FRleCompress write FRleCompress;
  49. Property BytesPerPixel : Byte Read GetColorSize Write SetColorSize; deprecated;
  50. end;
  51. implementation
  52. Function FPColorToRGB(Const Color : TFPColor) : TColorRGB;
  53. begin
  54. With Result,Color do
  55. begin
  56. R:=(Red and $FF00) shr 8;
  57. G:=(Green and $FF00) shr 8;
  58. B:=(Blue and $FF00) shr 8;
  59. end;
  60. end;
  61. Function FPColorToRGBA(Const Color : TFPColor) : TColorRGBA;
  62. begin
  63. With Result,Color do
  64. begin
  65. R:=(Red and $FF00) shr 8;
  66. G:=(Green and $FF00) shr 8;
  67. B:=(Blue and $FF00) shr 8;
  68. A:=(Alpha and $FF00) shr 8;
  69. end;
  70. end;
  71. constructor TFPWriterBMP.create;
  72. begin
  73. inherited create;
  74. FBpp:=24;
  75. FRleCompress:=false;
  76. end;
  77. { Only for compatibility, BytesPerPixel should be removed }
  78. { ******************************************************* }
  79. procedure TFPWriterBMP.SetColorSize (AValue : byte);
  80. begin
  81. SetBpp(AValue*8);
  82. end;
  83. function TFPWriterBMP.GetColorSize : byte;
  84. begin
  85. if FBpp<>15 then Result:=FBpp div 8
  86. else Result:=2;
  87. end;
  88. { ******************************************************* }
  89. procedure TFPWriterBMP.SetBpp (const abpp : byte);
  90. begin
  91. if not (abpp in [1,4,8,15,16,24,32]) then
  92. raise FPImageException.Create('Invalid color depth');
  93. FBpp:=abpp;
  94. end;
  95. procedure TFPWriterBMP.FillColorMap(Img : TFPCustomImage);
  96. var BadPalette : boolean;
  97. i : integer;
  98. begin
  99. BadPalette:=false;
  100. if not Img.UsePalette then BadPalette:=true
  101. else if Img.Palette.Count>(1 shl FBpp) then BadPalette:=true;
  102. if BadPalette then
  103. raise FPImageException.Create('Image palette is too big or absent');
  104. setlength(ColInfo,Img.Palette.Count);
  105. BFI.ClrUsed:=Img.Palette.Count;
  106. for i:=0 to BFI.ClrUsed-1 do
  107. begin
  108. ColInfo[i]:=FPColorToRGBA(Img.Palette.Color[i]);
  109. ColInfo[i].A:=0;
  110. end;
  111. end;
  112. { True 16 bit color is 5 bits red, 6 bits green and 5 bits blue.
  113. Compression must be set to BI_BITFIELDS and we must specify masks for red, green and blue.
  114. 16 bit without compression and masks is 5 bits per channel, so it's 15 bit even if in the header we
  115. must write 16.
  116. It's possible to provide custom masks but this is not compatible with windows9x, so we use 555 for 15 bit
  117. and 565 for 16 bit.
  118. Masks are longwords stored in the palette instead of palette entries (which are 4 bytes long too, with
  119. components stored in following order: B G R A. Since we must write a low-endian longword, B is LSB and A
  120. is the MSB).
  121. We must write first red mask, then green and then blue.
  122. This sounds terribly confusing, if you don't understand take a look at
  123. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1rw2.asp
  124. }
  125. procedure TFPWriterBMP.Setup16bpp;
  126. var col : TColorRGBA;
  127. begin
  128. BFI.Compression:=BI_BITFIELDS;
  129. setlength(ColInfo,3);
  130. { A R G B
  131. r := $0000F800
  132. g := $000007E0
  133. b := $0000001F
  134. }
  135. col.A:=0; Col.R:=0; { These are 0 for all the three masks}
  136. { Red Mask }
  137. Col.G:=$F8; Col.B:=0;
  138. ColInfo[0]:=Col;
  139. { Green Mask }
  140. Col.G:=$07; Col.B:=$E0;
  141. ColInfo[1]:=Col;
  142. { Blue Mask }
  143. Col.G:=$00; Col.B:=$1F;
  144. ColInfo[2]:=Col;
  145. end;
  146. { 16 bit bpp with 555 packing (that is, 15 bit color)
  147. This is bit dislocation:
  148. 0RRR RRGG GGGB BBBB }
  149. function TFPWriterBMP.PackWord555(const col : TFPColor) : word;
  150. var tmpcol : TColorRGB;
  151. tmpr, tmpg, tmpb : word;
  152. begin
  153. tmpcol:=FPColorToRGB(col);
  154. tmpb:=tmpcol.b shr 3;
  155. tmpg:=tmpcol.g and $F8; tmpg:= tmpg shl 2;
  156. tmpr:=tmpcol.r and $F8; tmpr:= tmpr shl 7;
  157. tmpb:= tmpr or tmpg or tmpb;
  158. {$IFDEF ENDIAN_BIG}
  159. tmpb:=swap(tmpb);
  160. {$ENDIF}
  161. Result:=tmpb;
  162. end;
  163. { 16 bit bpp with 565 packing )
  164. This is bit dislocation:
  165. RRRR RGGG GGGB BBBB }
  166. function TFPWriterBMP.PackWord565(const col : TFPColor) : word;
  167. var tmpcol : TColorRGB;
  168. tmpr, tmpg, tmpb : word;
  169. begin
  170. tmpcol:=FPColorToRGB(col);
  171. tmpb:=tmpcol.b shr 3;
  172. tmpg:=tmpcol.g and $FC; tmpg:= tmpg shl 3;
  173. tmpr:=tmpcol.r and $F8; tmpr:= tmpr shl 8;
  174. tmpb:= tmpr or tmpg or tmpb;
  175. {$IFDEF ENDIAN_BIG}
  176. tmpb:=swap(tmpb);
  177. {$ENDIF}
  178. Result:=tmpb;
  179. end;
  180. { First pixel in the most significant nibble, second one in LSN. If we are at the end of the line,
  181. pad with zero }
  182. function TFPWriterBMP.Pack4bpp(const img : TFPCustomImage; var Col : integer; const Row : integer) : byte;
  183. var b : byte;
  184. begin
  185. b:=(img.Pixels[Col,Row] and $F) shl 4;
  186. if Col<img.Width-1 then
  187. begin
  188. inc(Col);
  189. b:=b + (img.Pixels[Col,Row] and $F);
  190. end;
  191. Result:=b;
  192. inc(col);
  193. end;
  194. { First pixel in the most significant bit, last one in LSN. If we are at the end of the line,
  195. pad with zero }
  196. function TFPWriterBMP.Pack1bpp(const img : TFPCustomImage; var Col : integer; const Row : integer) : byte;
  197. var b : byte;
  198. sh : shortint;
  199. begin
  200. b:=0;
  201. sh:=7;
  202. while ((Col<Img.Width) and (sh>=0)) do
  203. begin
  204. if img.Pixels[Col,Row]<>0 then { set this bit }
  205. b:=b+(1 shl sh);
  206. dec(sh);
  207. inc(Col);
  208. end;
  209. Result:=b;
  210. end;
  211. function TFPWriterBMP.SaveHeader(Stream:TStream; Img : TFPCustomImage):boolean;
  212. begin
  213. Result:=False;
  214. with BFI do
  215. begin
  216. Size:=sizeof(TBitMapInfoHeader);
  217. Width:=Img.Width;
  218. Height:=Img.Height;
  219. Planes:=1;
  220. if FBpp=15 then BitCount:=16
  221. else BitCount:=FBpp;
  222. XPelsPerMeter:=100;
  223. YPelsPerMeter:=100;
  224. ClrImportant:=0;
  225. end;
  226. with BFH do
  227. begin
  228. bfType:=BMmagic;//'BM'
  229. bfOffset:=sizeof(TBitMapFileHeader)+sizeof(TBitMapInfoHeader)+length(ColInfo)*4;
  230. bfReserved:=0;
  231. bfSize:=bfOffset+BFI.SizeImage;
  232. end;
  233. {$IFDEF ENDIAN_BIG}
  234. SwapBMPFileHeader(BFH);
  235. SwapBMPInfoHeader(BFI);
  236. {$ENDIF}
  237. Stream.seek(0,soFromBeginning);
  238. Stream.Write(bfh,sizeof(TBitMapFileHeader));
  239. Stream.Write(bfi,sizeof(TBitMapInfoHeader));
  240. {$IFDEF ENDIAN_BIG}
  241. SwapBMPFileHeader(BFH);
  242. SwapBMPInfoHeader(BFI);
  243. {$ENDIF}
  244. Result:=true;
  245. end;
  246. { This code is rather ugly and difficult to read, but compresses better than gimp.
  247. Brief explanation:
  248. A repetition is good if it's made of 3 elements at least: we have 2 bytes instead of 1. Let's call this a
  249. "repetition" or "true repetition".
  250. So we start finding the first repetition from current position.
  251. Once found, we must decide how to handle elements between current position (i) and the repetition position (j)
  252. if j-i = 0 we are on the repetition, so we encode it
  253. if j-i = 1 there is only one pixel. We can't do anything but encode it as a repetition of 1 element.
  254. if j-i = 2 we have two pixels. These can be a couple (a repetition of 2 elements) or 2 singles
  255. (2 repetitions of 1 element)
  256. if j-i > 2 we have two choices. In fact, we must consider that absolute mode is 2 bytes + length of chunk.
  257. A repetition is always 2 bytes, so for 1 element we leak 1 byte, while for 2 elements we don't leak
  258. any byte.
  259. So if we have at most 1 single this means that everything else is made up of couples: it's best to
  260. use repetitions so that we leak 0 to 1 byte.
  261. If we have 2 singles or more it's better to use absolute mode, since we leak 2 bytes always,
  262. without regard to the size of chunk. }
  263. procedure TFPWriterBMP.CompressScanLineRLE8(ALine : pbyte; const Row, Width : Integer; Stream : TStream);
  264. var i, j, k, couples, singles : integer;
  265. prev,tmp : byte;
  266. begin
  267. i:=0;
  268. while (i<Width) do
  269. begin
  270. { let's see how bytes are disposed, so that we can choose the best way to compress }
  271. couples:=0; singles:=1;
  272. prev:=Aline[i];
  273. j:=i+1;
  274. while ((j<Width) and ((j-i)<255)) do
  275. begin
  276. if Aline[j]=prev then { this is a couple at least }
  277. begin
  278. dec(singles); { so the previous one wasn't a single }
  279. if (((j+1)<Width) and (Aline[j+1]=prev)) then { at least three equal items, it's a repetition }
  280. begin
  281. dec(j); { repetition starts at j-1, since j is the middle pixel and j+1 is the third pixel }
  282. break;
  283. end
  284. else inc(couples) { ok it's a couple }
  285. end
  286. else inc(singles); { this is a single if next isn't a couple }
  287. prev:=Aline[j];
  288. inc(j);
  289. end;
  290. { ok, now that we know more about byte disposition we write data }
  291. case (j-i) of
  292. 0 : begin { there is a repetition with count>=3 }
  293. prev:=Aline[i];
  294. j:=i+1;
  295. while ((j<Width) and ((j-i)<255)) do
  296. begin
  297. if Aline[j]<>prev then break;
  298. inc(j);
  299. end;
  300. tmp:=j-i;
  301. Stream.Write(tmp,1);
  302. Stream.Write(prev,1);
  303. end;
  304. 1 : begin { single value: we write a repetition of 1 }
  305. tmp:=1;
  306. Stream.Write(tmp,1);
  307. Stream.Write(Aline[i],1);
  308. end;
  309. 2 : begin
  310. if couples=1 then { a couple: we write a repetition of 2 }
  311. begin
  312. tmp:=2;
  313. Stream.Write(tmp,1);
  314. Stream.Write(Aline[i],1);
  315. end
  316. else { two singles: we write two repetitions of 1 each }
  317. begin
  318. tmp:=1;
  319. Stream.Write(tmp,1);
  320. Stream.Write(Aline[i],1);
  321. Stream.Write(tmp,1);
  322. Stream.Write(Aline[i+1],1);
  323. end;
  324. end;
  325. else { here we have two choices }
  326. begin
  327. if singles>1 then { it's cheaper to use absolute mode }
  328. begin
  329. tmp:=0; Stream.Write(tmp,1); { escape }
  330. tmp:=j-i; Stream.Write(tmp,1); { number of pixels in absolute mode }
  331. Stream.Write(Aline[i],j-i); { write these pixels... }
  332. if ((tmp mod 2)<>0) then { we must end on a 2-byte boundary }
  333. begin
  334. tmp:=0; Stream.Write(tmp,1); { so pad with an additional zero }
  335. end;
  336. end
  337. else { they're nearly all couples, don't use absolute mode }
  338. begin
  339. k:=i;
  340. while (k<j) do
  341. begin
  342. if ((k+1<j) and (Aline[k]=Aline[k+1])) then
  343. begin
  344. tmp:=2;
  345. inc(k);
  346. end
  347. else tmp:=1;
  348. Stream.Write(tmp,1);
  349. Stream.Write(Aline[k],1);
  350. inc(k);
  351. end;
  352. end;
  353. end;
  354. end;
  355. i:=j;
  356. end;
  357. tmp:=0; Stream.Write(tmp,1); { escape }
  358. if Row=0 then { last line, end of file }
  359. tmp:=1;
  360. Stream.Write(tmp,1);
  361. end;
  362. { Ok, this is even uglier than the RLE8 version above, and this time gimp compresses better :\
  363. Differences with RLE8: repetition count is pixel-relative, not byte-relative, but repetition data is made
  364. of 2 pixels. So you have a repetition when you have pixels repeated in an alternate way, even if you can do
  365. something like:
  366. 01E0 => E
  367. 0316 => 161.
  368. A repetition is good if it's made of five elements at least (2 bytes instead of 3).
  369. In rle4 we consider "single" either a single nibble or 2 (a byte), while a couple is a repetition of 3 or 4
  370. elements. }
  371. procedure TFPWriterBMP.CompressScanLineRLE4(ALine : pbyte; const Row, Width : Integer; Stream : TStream);
  372. var i, j, k, couples, singles, lastsingle : integer;
  373. prev1, prev2, prev : word;
  374. tmp : byte;
  375. nibline : pbyte; { temporary array of nibbles }
  376. even : boolean;
  377. begin
  378. getmem(nibline,width);
  379. try
  380. k:=(Width div 2) + (Width mod 2);
  381. i:=0;
  382. while (i<k) do
  383. begin
  384. nibline[i*2]:=aline[i] shr 4;
  385. nibline[i*2+1]:=aline[i] and $F;
  386. inc(i);
  387. end;
  388. i:=0;
  389. while (i<Width) do
  390. begin
  391. { let's see how nibbles are disposed, so that we can choose the best way to compress }
  392. couples:=0; singles:=1; lastsingle:=-10;
  393. prev1:=nibline[i];
  394. prev2:=nibline[i+1];
  395. j:=i+2;
  396. while ((j<Width) and ((j-i)<255)) do
  397. begin
  398. if nibline[j]=prev1 then { this is a half-couple at least (repetition of 3) }
  399. begin
  400. dec(singles); { so the previous one wasn't a single }
  401. if (((j+1)<Width) and (nibline[j+1]=prev2)) then { at least a couple (repetition of 4) }
  402. begin
  403. if (((j+2)<Width) and (nibline[j+2]=prev1)) then { at least a repetition of 5, good }
  404. begin
  405. dec(j,2); { repetition starts at j-2: prev1 prev2 prev1* prev2 prev1, we are here * }
  406. break;
  407. end
  408. else
  409. begin { ok it's a couple }
  410. inc(couples);
  411. if (j-i)=254 then { in this rare case, j-i becomes 256. So, force a half-couple and exit }
  412. begin
  413. inc(j);
  414. break;
  415. end;
  416. prev1:=256; { this is a couple, don't consider these positions in further scanning }
  417. prev2:=256;
  418. inc(j,2);
  419. continue;
  420. end
  421. end
  422. else
  423. begin { ok it's a half-couple }
  424. inc(couples);
  425. prev:=256; //this is a half-couple, don't consider this position in further scanning.
  426. end;
  427. end
  428. else
  429. begin
  430. if lastsingle<>(j-1) then
  431. begin
  432. inc(singles); { this is a single if next isn't a couple }
  433. lastsingle:=j;
  434. end;
  435. prev:=nibline[j];
  436. end;
  437. prev1:=prev2;
  438. prev2:=prev;
  439. even:=not even;
  440. inc(j);
  441. end;
  442. if j>Width then j:=Width; { if j was Width-1 loop was skipped and j is Width+1, so we fix it }
  443. { ok, now that we know more about byte disposition we write data }
  444. case (j-i) of
  445. 0 : begin { there is a repetition with count>=5 }
  446. even:=true;
  447. prev1:=nibline[i];
  448. prev2:=nibline[i+1];
  449. j:=i+2;
  450. while ((j<Width) and ((j-i)<255)) do
  451. begin
  452. if even then if nibline[j]<>prev1 then break;
  453. if not even then if nibline[j]<>prev2 then break;
  454. even:=not even;
  455. inc(j);
  456. end;
  457. tmp:=j-i;
  458. Stream.Write(tmp,1);
  459. prev:=(prev1 shl 4) + (prev2 and $F);
  460. tmp:=prev;
  461. Stream.Write(tmp,1);
  462. end;
  463. 1 : begin { single value: we write a repetition of 1 }
  464. tmp:=1;
  465. Stream.Write(tmp,1);
  466. tmp:=nibline[i] shl 4;
  467. Stream.Write(tmp,1);
  468. end;
  469. 2 : begin { 2 singles in the same byte: we write a repetition of 2 }
  470. tmp:=2;
  471. Stream.Write(tmp,1);
  472. tmp:=(nibline[i] shl 4) + (nibline[i+1] and $F);
  473. Stream.Write(tmp,1);
  474. end;
  475. 3 : begin
  476. if couples=1 then { a couple: we write a repetition of 3 }
  477. begin
  478. tmp:=3;
  479. Stream.Write(tmp,1);
  480. tmp:=(nibline[i] shl 4) + (nibline[i+1] and $F);
  481. Stream.Write(tmp,1);
  482. end
  483. else
  484. begin { 2 singles, 2 repetitions of 2 and 1 respectively }
  485. tmp:=2;
  486. Stream.Write(tmp,1);
  487. tmp:=(nibline[i] shl 4) + (nibline[i+1] and $F);
  488. Stream.Write(tmp,1);
  489. tmp:=1;
  490. Stream.Write(tmp,1);
  491. tmp:=nibline[i+2] shl 4;
  492. Stream.Write(tmp,1);
  493. end;
  494. end;
  495. 4 : begin
  496. if singles=0 then { a couple: we write a repetition of 4 }
  497. begin
  498. tmp:=4;
  499. Stream.Write(tmp,1);
  500. tmp:=(nibline[i] shl 4) + (nibline[i+1] and $F);
  501. Stream.Write(tmp,1);
  502. end
  503. else
  504. begin { 2 singles, 2 repetitions of 2 each }
  505. tmp:=2;
  506. Stream.Write(tmp,1);
  507. tmp:=(nibline[i] shl 4) + (nibline[i+1] and $F);
  508. Stream.Write(tmp,1);
  509. tmp:=2;
  510. Stream.Write(tmp,1);
  511. tmp:=(nibline[i+2] shl 4) + (nibline[i+3] and $F);
  512. Stream.Write(tmp,1);
  513. end;
  514. end;
  515. else { here we have two choices }
  516. begin
  517. if singles>1 then { it's cheaper to use absolute mode }
  518. begin
  519. tmp:=0; Stream.Write(tmp,1); { escape }
  520. tmp:=j-i; Stream.Write(tmp,1); { number of pixels in absolute mode }
  521. k:=i;
  522. while (k<j) do { write these pixels... }
  523. begin
  524. tmp:=nibline[k] shl 4;
  525. inc(k);
  526. if k<j then
  527. begin
  528. tmp:=tmp+(nibline[k] and $F);
  529. inc(k);
  530. end;
  531. Stream.Write(tmp,1);
  532. end;
  533. k:=j-i;
  534. k:=k+(k mod 2);
  535. if (k mod 4)<>0 then { we must end on a 2-byte boundary }
  536. begin
  537. tmp:=0; Stream.Write(tmp,1); { so pad with an additional zero }
  538. end;
  539. end
  540. else { they're nearly all couples, don't use absolute mode }
  541. begin
  542. k:=i;
  543. while (k<j) do
  544. begin
  545. if ((k+2<j) and (nibline[k]=nibline[k+2])) then
  546. begin
  547. if ((k+3<j) and (nibline[k+1]=nibline[k+3])) then tmp:=4
  548. else tmp:=3;
  549. end
  550. else
  551. begin
  552. if (k+1>=j) then tmp:=1
  553. else if ((k+3<j) and (nibline[k+1]=nibline[k+3])) then tmp:=1
  554. else tmp:=2;
  555. end;
  556. Stream.Write(tmp,1);
  557. prev:=tmp;
  558. tmp:=nibline[k] shl 4;
  559. if tmp<>1 then tmp:=tmp+(nibline[k+1] and $F);
  560. Stream.Write(tmp,1);
  561. inc(k,prev);
  562. end;
  563. end;
  564. end;
  565. end;
  566. i:=j;
  567. end;
  568. tmp:=0; Stream.Write(tmp,1); { escape }
  569. if Row=0 then { last line, end of file }
  570. tmp:=1;
  571. Stream.Write(tmp,1);
  572. finally
  573. FreeMem(nibline);
  574. end;
  575. end;
  576. procedure TFPWriterBMP.InternalWrite (Stream:TStream; Img:TFPCustomImage);
  577. var
  578. Row,Col,RowSize:Integer;
  579. PadCount : byte;
  580. aLine: PByte;
  581. i : Integer;
  582. tmppos : int64;
  583. continue : boolean;
  584. percent : byte;
  585. percentinterval : longword;
  586. percentacc : longword;
  587. Rect : TRect;
  588. begin
  589. Rect.Left:=0; Rect.Top:=0; Rect.Right:=0; Rect.Bottom:=0;
  590. continue:=true;
  591. percent:=0;
  592. percentinterval:=(Img.Height*4) div 100;
  593. if percentinterval=0 then percentinterval:=$FFFFFFFF;
  594. percentacc:=0;
  595. Progress(psStarting,0,false,Rect,'',continue);
  596. if not continue then exit;
  597. if (FRLECompress and (not (FBpp in [4,8]))) then
  598. raise FPImageException.Create('Can''t use RLE compression with '+IntToStr(FBpp)+' bits per pixel');
  599. if FRLECompress and (FBpp=4) then BFI.Compression:=BI_RLE4
  600. else if FRLECompress and (FBpp=8) then BFI.Compression:=BI_RLE8
  601. else BFI.Compression:=BI_RGB;
  602. BFI.ClrUsed:=0;
  603. try
  604. if FBpp<=8 then FillColorMap(Img); { sets colormap and ClrUsed}
  605. if FBpp=16 then Setup16bpp; { sets colormap with masks and Compression }
  606. RowSize:=0; { just to keep the compiler quiet. }
  607. case FBpp of
  608. 1 : begin
  609. RowSize:=Img.Width div 8;
  610. if (Img.Width mod 8)<>0 then
  611. inc(RowSize);
  612. end;
  613. 4 : begin
  614. RowSize:=Img.Width div 2;
  615. if (Img.Width mod 2)<>0 then
  616. inc(RowSize);
  617. end;
  618. 8 : RowSize:=Img.Width;
  619. 15 : RowSize:=Img.Width*2;
  620. 16 : RowSize:=Img.Width*2;
  621. 24 : RowSize:=Img.Width*3;
  622. 32 : RowSize:=Img.Width*4;
  623. end;
  624. PadCount:=(4-(RowSize mod 4)) mod 4; { every row must end on 4 byte boundary }
  625. inc(RowSize,PadCount);
  626. BFI.SizeImage:=RowSize*Img.Height;
  627. SaveHeader(Stream,Img); { write the headers }
  628. for i:=0 to length(ColInfo)-1 do { write the palette (or the masks in 16bpp case) }
  629. Stream.Write(ColInfo[i],sizeof(TColorRGBA));
  630. GetMem(aLine,RowSize);
  631. try
  632. for Row:=Img.Height-1 downto 0 do
  633. begin
  634. i:=0; Col:=0;
  635. case FBpp of
  636. 1 : while(Col<img.Width) do
  637. begin
  638. PByte(aline)[i]:=Pack1bpp(img,Col,Row); { increases Col by 8 each time }
  639. inc(i);
  640. end;
  641. 4 : while(Col<img.Width) do
  642. begin
  643. PByte(aline)[i]:=Pack4bpp(img,Col,Row); { increases Col by 2 each time }
  644. inc(i);
  645. end;
  646. 8 : for Col:=0 to img.Width-1 do
  647. PByte(aline)[Col]:=img.Pixels[Col,Row];
  648. 15 : for Col:=0 to img.Width-1 do
  649. PWord(aline)[Col]:=PackWord555(img.colors[Col,Row]);
  650. 16 : for Col:=0 to img.Width-1 do
  651. PWord(aline)[Col]:=PackWord565(img.colors[Col,Row]);
  652. 24 : for Col:=0 to img.Width-1 do
  653. PColorRGB(aLine)[Col]:=FPColorToRGB(img.colors[Col,Row]);
  654. 32 : for Col:=0 to img.Width-1 do
  655. PColorRGBA(aLine)[Col]:=FPColorToRGBA(img.colors[Col,Row]);
  656. end;
  657. { pad the scanline with zeros }
  658. for i:=RowSize-PadCount to RowSize-1 do
  659. Pbyte(aline)[i]:=0;
  660. if BFI.Compression=BI_RLE8 then CompressScanLineRLE8(aLine,Row,img.Width,Stream)
  661. else if BFI.Compression=BI_RLE4 then CompressScanLineRLE4(aLine,Row,img.Width,Stream)
  662. else Stream.Write(aLine[0],RowSize);
  663. inc(percentacc,4);
  664. if percentacc>=percentinterval then
  665. begin
  666. percent:=percent+(percentacc div percentinterval);
  667. percentacc:=percentacc mod percentinterval;
  668. Progress(psRunning,percent,false,Rect,'',continue);
  669. if not continue then exit;
  670. end;
  671. end;
  672. { If image is compressed we must fix the headers since we now know the size of the image }
  673. if BFI.Compression in [BI_RLE4,BI_RLE8] then
  674. begin
  675. tmppos:=Stream.Position-StartPosition-BFH.bfOffset;
  676. BFI.SizeImage:=tmppos; { set size of the image }
  677. tmppos:=Stream.Position; { remember where we are }
  678. Stream.Position:=StartPosition; { rewind to the beginning }
  679. SaveHeader(Stream,Img); { rewrite headers (this will update BFH.Size too) }
  680. Stream.Position:=tmppos; { restore our position }
  681. end;
  682. Progress(psEnding,100,false,Rect,'',continue);
  683. finally
  684. FreeMem(aLine);
  685. end;
  686. finally
  687. setlength(ColInfo,0);
  688. end;
  689. end;
  690. initialization
  691. ImageHandlers.RegisterImageWriter ('BMP Format', 'bmp', TFPWriterBMP);
  692. end.