imgconv.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. Image conversion example.
  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. program ImgConv;
  14. {_$define UseFile}
  15. uses FPImage, FPWriteXPM, FPWritePNG, FPReadXPM, FPReadPNG,
  16. {$ifndef UseFile}classes,{$endif}
  17. sysutils;
  18. var img : TFPMemoryImage;
  19. reader : TFPCustomImageReader;
  20. Writer : TFPCustomimageWriter;
  21. ReadFile, WriteFile, WriteOptions : string;
  22. procedure Init;
  23. var t : char;
  24. begin
  25. if paramcount = 4 then
  26. begin
  27. T := upcase (paramstr(1)[1]);
  28. if T = 'X' then
  29. Reader := TFPReaderXPM.Create
  30. else
  31. Reader := TFPReaderPNG.Create;
  32. ReadFile := paramstr(2);
  33. WriteOptions := paramstr(3);
  34. WriteFile := paramstr(4);
  35. end
  36. else
  37. begin
  38. Reader := nil;
  39. ReadFile := paramstr(1);
  40. WriteOptions := paramstr(2);
  41. WriteFile := paramstr(3);
  42. end;
  43. WriteOptions := uppercase (writeoptions);
  44. T := WriteOptions[1];
  45. if T = 'X' then
  46. Writer := TFPWriterXPM.Create
  47. else
  48. Writer := TFPWriterPNG.Create;
  49. img := TFPMemoryImage.Create(1,1);
  50. end;
  51. procedure ReadImage;
  52. {$ifndef UseFile}var str : TStream;{$endif}
  53. begin
  54. if assigned (reader) then
  55. img.LoadFromFile (ReadFile, Reader)
  56. else
  57. {$ifdef UseFile}
  58. img.LoadFromFile (ReadFile);
  59. {$else}
  60. if fileexists (ReadFile) then
  61. begin
  62. str := TFileStream.create (ReadFile,fmOpenRead);
  63. try
  64. img.loadFromStream (str);
  65. finally
  66. str.Free;
  67. end;
  68. end
  69. else
  70. writeln ('File ',readfile,' doesn''t exists!');
  71. {$endif}
  72. end;
  73. procedure WriteImage;
  74. var t : string;
  75. begin
  76. t := WriteOptions;
  77. writeln (' WriteImage, options=',t);
  78. if (t[1] = 'P') then
  79. with (Writer as TFPWriterPNG) do
  80. begin
  81. Grayscale := pos ('G', t) > 0;
  82. Indexed := pos ('I', t) > 0;
  83. WordSized := pos('W', t) > 0;
  84. UseAlpha := pos ('A', t) > 0;
  85. writeln ('Grayscale ',Grayscale, ' - Indexed ',Indexed,
  86. ' - WordSized ',WordSized,' - UseAlpha ',UseAlpha);
  87. end
  88. else if (t[1] = 'X') then
  89. begin
  90. if length(t) > 1 then
  91. with (Writer as TFPWriterXPM) do
  92. begin
  93. ColorCharSize := ord(t[2]) - ord('0');
  94. end;
  95. end;
  96. writeln ('Options checked, now writing...');
  97. img.SaveToFile (WriteFile, Writer);
  98. end;
  99. procedure Clean;
  100. begin
  101. Reader.Free;
  102. Writer.Free;
  103. Img.Free;
  104. end;
  105. begin
  106. if (paramcount <> 4) and (paramcount <> 3) then
  107. begin
  108. writeln ('Give filename to read and to write, preceded by filetype:');
  109. writeln ('X for XPM, P for PNG');
  110. writeln ('example: imgconv X hello.xpm P hello.png');
  111. writeln ('example: imgconv hello.xpm P hello.png');
  112. writeln ('Options for');
  113. writeln (' PNG : G : grayscale, A : use alpha, ');
  114. writeln (' I : Indexed in palette, W : Word sized.');
  115. writeln (' XPM : Number of chars to use for 1 pixel');
  116. writeln (' The color size of an XPM can be set after the X as 1,2,3 or 4');
  117. writeln ('example: imgconv hello.xpm PIA hello.png');
  118. writeln ('example: imgconv hello.png X2 hello.xpm');
  119. end
  120. else
  121. try
  122. writeln ('Initing');
  123. Init;
  124. writeln ('Reading image');
  125. ReadImage;
  126. writeln ('Writeing image');
  127. WriteImage;
  128. writeln ('Clean up');
  129. Clean;
  130. except
  131. on e : exception do
  132. writeln ('Error: ',e.message);
  133. end;
  134. end.