imgconv.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team
  4. Image conversion example.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}{$h+}
  12. program ImgConv;
  13. {_$define UseFile}
  14. uses FPWriteXPM, FPWritePNG, FPWriteBMP,fpreadgif,
  15. FPReadXPM, FPReadPNG, FPReadBMP, fpreadjpeg,fpwritejpeg,
  16. fpreadtga,fpwritetga,fpreadpnm,fpwritepnm, fpreadtiff, fpwritetiff,
  17. {$ifndef UseFile}classes,{$endif}
  18. FPImage, sysutils;
  19. var img : TFPMemoryImage;
  20. reader : TFPCustomImageReader;
  21. Writer : TFPCustomimageWriter;
  22. ReadFile, WriteFile, WriteOptions : string;
  23. procedure Init;
  24. var t : char;
  25. begin
  26. if paramcount = 4 then
  27. begin
  28. T := upcase (paramstr(1)[1]);
  29. if T = 'X' then
  30. Reader := TFPReaderXPM.Create
  31. else if T = 'B' then
  32. Reader := TFPReaderBMP.Create
  33. else if T = 'J' then
  34. Reader := TFPReaderJPEG.Create
  35. else if T = 'G' then
  36. Reader := TFPReaderGif.Create
  37. else if T = 'P' then
  38. Reader := TFPReaderPNG.Create
  39. else if T = 'T' then
  40. Reader := TFPReaderTarga.Create
  41. else if T = 'F' then
  42. Reader := TFPReaderTiff.Create
  43. else if T = 'N' then
  44. Reader := TFPReaderPNM.Create
  45. else
  46. begin
  47. Writeln('Unknown file format : ',T);
  48. Halt(1);
  49. end;
  50. ReadFile := paramstr(2);
  51. WriteOptions := paramstr(3);
  52. WriteFile := paramstr(4);
  53. end
  54. else
  55. begin
  56. Reader := nil;
  57. ReadFile := paramstr(1);
  58. WriteOptions := paramstr(2);
  59. WriteFile := paramstr(3);
  60. end;
  61. WriteOptions := uppercase (writeoptions);
  62. T := WriteOptions[1];
  63. if T = 'X' then
  64. Writer := TFPWriterXPM.Create
  65. else if T = 'B' then
  66. begin
  67. Writer := TFPWriterBMP.Create;
  68. TFPWriterBMP(Writer).BitsPerPixel:=32;
  69. end
  70. else if T = 'J' then
  71. Writer := TFPWriterJPEG.Create
  72. else if T = 'P' then
  73. Writer := TFPWriterPNG.Create
  74. else if T = 'T' then
  75. Writer := TFPWriterTARGA.Create
  76. else if T = 'F' then
  77. Writer := TFPWriterTiff.Create
  78. else if T = 'N' then
  79. Writer := TFPWriterPNM.Create
  80. else
  81. begin
  82. Writeln('Unknown file format : ',T);
  83. Halt(1);
  84. end;
  85. img := TFPMemoryImage.Create(0,0);
  86. img.UsePalette:=false;
  87. end;
  88. procedure ReadImage;
  89. {$ifndef UseFile}var str : TStream;{$endif}
  90. begin
  91. if assigned (reader) then
  92. img.LoadFromFile (ReadFile, Reader)
  93. else
  94. {$ifdef UseFile}
  95. img.LoadFromFile (ReadFile);
  96. {$else}
  97. if fileexists (ReadFile) then
  98. begin
  99. str := TFileStream.create (ReadFile,fmOpenRead);
  100. try
  101. img.loadFromStream (str);
  102. finally
  103. str.Free;
  104. end;
  105. end
  106. else
  107. writeln ('File ',readfile,' doesn''t exists!');
  108. {$endif}
  109. end;
  110. procedure WriteImage;
  111. var t : string;
  112. begin
  113. t := WriteOptions;
  114. writeln (' WriteImage, options=',t);
  115. if (t[1] = 'P') then
  116. with (Writer as TFPWriterPNG) do
  117. begin
  118. Grayscale := pos ('G', t) > 0;
  119. Indexed := pos ('I', t) > 0;
  120. WordSized := pos('W', t) > 0;
  121. UseAlpha := pos ('A', t) > 0;
  122. writeln ('Grayscale ',Grayscale, ' - Indexed ',Indexed,
  123. ' - WordSized ',WordSized,' - UseAlpha ',UseAlpha);
  124. end
  125. else if (t[1] = 'X') then
  126. begin
  127. if length(t) > 1 then
  128. with (Writer as TFPWriterXPM) do
  129. begin
  130. ColorCharSize := ord(t[2]) - ord('0');
  131. end;
  132. end;
  133. writeln ('Options checked, now writing...');
  134. img.SaveToFile (WriteFile, Writer);
  135. end;
  136. procedure Clean;
  137. begin
  138. Reader.Free;
  139. Writer.Free;
  140. Img.Free;
  141. end;
  142. begin
  143. if (paramcount <> 4) and (paramcount <> 3) then
  144. begin
  145. writeln ('Give filename to read and to write, preceded by filetype:');
  146. writeln ('X for XPM, P for PNG, B for BMP, J for JPEG, T for TGA,');
  147. writeln ('N for PNM (read only), F for TIFF, G for gif (read only)');
  148. writeln ('example: imgconv X hello.xpm P hello.png');
  149. writeln ('example: imgconv hello.xpm P hello.png');
  150. writeln ('Options for');
  151. writeln (' PNG : G : grayscale, A : use alpha, ');
  152. writeln (' I : Indexed in palette, W : Word sized.');
  153. writeln (' XPM : Number of chars to use for 1 pixel');
  154. writeln (' The color size of an XPM can be set after the X as 1,2,3 or 4');
  155. writeln ('example: imgconv hello.xpm PIA hello.png');
  156. writeln ('example: imgconv hello.png X2 hello.xpm');
  157. end
  158. else
  159. try
  160. writeln ('Initing');
  161. Init;
  162. writeln ('Reading image');
  163. ReadImage;
  164. writeln ('Writing image');
  165. WriteImage;
  166. writeln ('Clean up');
  167. Clean;
  168. except
  169. on e : exception do
  170. writeln ('Error: ',e.message);
  171. end;
  172. end.