imgconv.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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,
  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 = 'P' then
  36. Reader := TFPReaderPNG.Create
  37. else if T = 'T' then
  38. Reader := TFPReaderTarga.Create
  39. else if T = 'F' then
  40. Reader := TFPReaderTiff.Create
  41. else if T = 'N' then
  42. Reader := TFPReaderPNM.Create
  43. else
  44. begin
  45. Writeln('Unknown file format : ',T);
  46. Halt(1);
  47. end;
  48. ReadFile := paramstr(2);
  49. WriteOptions := paramstr(3);
  50. WriteFile := paramstr(4);
  51. end
  52. else
  53. begin
  54. Reader := nil;
  55. ReadFile := paramstr(1);
  56. WriteOptions := paramstr(2);
  57. WriteFile := paramstr(3);
  58. end;
  59. WriteOptions := uppercase (writeoptions);
  60. T := WriteOptions[1];
  61. if T = 'X' then
  62. Writer := TFPWriterXPM.Create
  63. else if T = 'B' then
  64. begin
  65. Writer := TFPWriterBMP.Create;
  66. TFPWriterBMP(Writer).BitsPerPixel:=32;
  67. end
  68. else if T = 'J' then
  69. Writer := TFPWriterJPEG.Create
  70. else if T = 'P' then
  71. Writer := TFPWriterPNG.Create
  72. else if T = 'T' then
  73. Writer := TFPWriterTARGA.Create
  74. else if T = 'F' then
  75. Writer := TFPWriterTiff.Create
  76. else if T = 'N' then
  77. Writer := TFPWriterPNM.Create
  78. else
  79. begin
  80. Writeln('Unknown file format : ',T);
  81. Halt(1);
  82. end;
  83. img := TFPMemoryImage.Create(0,0);
  84. img.UsePalette:=false;
  85. end;
  86. procedure ReadImage;
  87. {$ifndef UseFile}var str : TStream;{$endif}
  88. begin
  89. if assigned (reader) then
  90. img.LoadFromFile (ReadFile, Reader)
  91. else
  92. {$ifdef UseFile}
  93. img.LoadFromFile (ReadFile);
  94. {$else}
  95. if fileexists (ReadFile) then
  96. begin
  97. str := TFileStream.create (ReadFile,fmOpenRead);
  98. try
  99. img.loadFromStream (str);
  100. finally
  101. str.Free;
  102. end;
  103. end
  104. else
  105. writeln ('File ',readfile,' doesn''t exists!');
  106. {$endif}
  107. end;
  108. procedure WriteImage;
  109. var t : string;
  110. begin
  111. t := WriteOptions;
  112. writeln (' WriteImage, options=',t);
  113. if (t[1] = 'P') then
  114. with (Writer as TFPWriterPNG) do
  115. begin
  116. Grayscale := pos ('G', t) > 0;
  117. Indexed := pos ('I', t) > 0;
  118. WordSized := pos('W', t) > 0;
  119. UseAlpha := pos ('A', t) > 0;
  120. writeln ('Grayscale ',Grayscale, ' - Indexed ',Indexed,
  121. ' - WordSized ',WordSized,' - UseAlpha ',UseAlpha);
  122. end
  123. else if (t[1] = 'X') then
  124. begin
  125. if length(t) > 1 then
  126. with (Writer as TFPWriterXPM) do
  127. begin
  128. ColorCharSize := ord(t[2]) - ord('0');
  129. end;
  130. end;
  131. writeln ('Options checked, now writing...');
  132. img.SaveToFile (WriteFile, Writer);
  133. end;
  134. procedure Clean;
  135. begin
  136. Reader.Free;
  137. Writer.Free;
  138. Img.Free;
  139. end;
  140. begin
  141. if (paramcount <> 4) and (paramcount <> 3) then
  142. begin
  143. writeln ('Give filename to read and to write, preceded by filetype:');
  144. writeln ('X for XPM, P for PNG, B for BMP, J for JPEG, T for TGA,');
  145. writeln ('N for PNM (read only), F for TIFF');
  146. writeln ('example: imgconv X hello.xpm P hello.png');
  147. writeln ('example: imgconv hello.xpm P hello.png');
  148. writeln ('Options for');
  149. writeln (' PNG : G : grayscale, A : use alpha, ');
  150. writeln (' I : Indexed in palette, W : Word sized.');
  151. writeln (' XPM : Number of chars to use for 1 pixel');
  152. writeln (' The color size of an XPM can be set after the X as 1,2,3 or 4');
  153. writeln ('example: imgconv hello.xpm PIA hello.png');
  154. writeln ('example: imgconv hello.png X2 hello.xpm');
  155. end
  156. else
  157. try
  158. writeln ('Initing');
  159. Init;
  160. writeln ('Reading image');
  161. ReadImage;
  162. writeln ('Writing image');
  163. WriteImage;
  164. writeln ('Clean up');
  165. Clean;
  166. except
  167. on e : exception do
  168. writeln ('Error: ',e.message);
  169. end;
  170. end.