fpwritexpm.pp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2003 by the Free Pascal development team
  5. XPM 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. {$mode objfpc}{$h+}
  13. unit FPWriteXPM;
  14. interface
  15. uses FPImage, classes, sysutils;
  16. type
  17. TFPWriterXPM = class (TFPCustomImageWriter)
  18. private
  19. FPalChars : string;
  20. protected
  21. procedure InternalWrite (Str:TStream; Img:TFPCustomImage); override;
  22. public
  23. constructor Create; override;
  24. property PalChars : string read FPalChars write FPalChars;
  25. end;
  26. implementation
  27. const
  28. DefPalChars = '.,-*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#;:=+%$()[]';
  29. constructor TFPWriterXPM.create;
  30. begin
  31. inherited create;
  32. PalChars := DefPalChars;
  33. end;
  34. function ColorToHex (c:TFPColor; size:integer) : string;
  35. var fmt : string;
  36. l : integer;
  37. begin
  38. {
  39. with c do
  40. write ('color=',red,',',green,',',blue,',',alpha);
  41. }
  42. l := size div 3;
  43. fmt := inttostr(l);
  44. fmt := '%'+fmt+'.'+fmt+'x';
  45. fmt := fmt+fmt+fmt;
  46. with c do
  47. result := format(fmt,[red,green,blue]);
  48. end;
  49. procedure TFPWriterXPM.InternalWrite (Str:TStream; Img:TFPCustomImage);
  50. var p, l : TStringList;
  51. c, len, r, t : integer;
  52. procedure BuildPaletteStrings;
  53. var r,c,e : integer;
  54. procedure MakeCodes (const head:string; charplace:integer);
  55. var r : integer;
  56. begin
  57. r := 1;
  58. dec (charplace);
  59. while (r <= e) and (c >= 0) do
  60. begin
  61. if Charplace = 1 then
  62. MakeCodes (head+PalChars[r],charplace)
  63. else
  64. p.Add (head+PalChars[r]);
  65. inc (r);
  66. dec(c);
  67. end;
  68. end;
  69. begin
  70. // Calculate length of codes
  71. len := 1;
  72. e := length(PalChars);
  73. r := e;
  74. c := img.palette.count;
  75. while (r <= c) do
  76. begin
  77. inc (len);
  78. r := r * e;
  79. end;
  80. MakeCodes ('',len);
  81. end;
  82. var s : string;
  83. begin
  84. l := TStringList.Create;
  85. p := TStringList.Create;
  86. try
  87. l.Add ('/* XPM */');
  88. l.Add ('static char *graphic[] = {');
  89. c := img.palette.count;
  90. BuildPaletteStrings;
  91. l.add (format('"%d %d %d %d",',[img.width,img.height,c,len]));
  92. for r := 0 to c-1 do
  93. begin
  94. if img.palette[r] <> colTransparent then
  95. l.Add (format('"%s c #%s",',[p[r],ColorToHex(img.palette.color[r],6)]))
  96. else
  97. l.Add (format('"%s c None",',[p[r]]));
  98. end;
  99. for r := 0 to img.Height-1 do
  100. begin
  101. s := p[img.pixels[0,r]];
  102. for t := 1 to img.Width-1 do
  103. s := s + p[img.pixels[t,r]];
  104. s := '"'+s+'"';
  105. if r < img.Height-1 then
  106. s := s + ',';
  107. l.Add (s);
  108. end;
  109. l.Add ('};');
  110. finally
  111. l.SaveToStream (Str);
  112. p.Free;
  113. l.Free;
  114. end;
  115. end;
  116. initialization
  117. ImageHandlers.RegisterImageWriter ('XPM Format', 'xpm', TFPWriterXPM);
  118. end.