imgconv.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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,fptiffcmn,
  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 : AnsiChar;
  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] = 'F') then
  126. with (Writer as TFPWriterTiff) do
  127. begin
  128. if pos ('G', t) > 0 then
  129. begin
  130. Img.Extra[TiffPhotoMetric]:='0';
  131. if Pos('8',T)>0 then
  132. Img.Extra[TiffGrayBits]:='8'
  133. else if Pos('16',T)>0 then
  134. Img.Extra[TiffGrayBits]:='16';
  135. Writeln(TiffPhotoMetric,': 0 ',TiffGrayBits,': ',Img.Extra[TiffGrayBits]);
  136. end;
  137. end
  138. else if (t[1] = 'X') then
  139. begin
  140. if length(t) > 1 then
  141. with (Writer as TFPWriterXPM) do
  142. begin
  143. ColorCharSize := ord(t[2]) - ord('0');
  144. end;
  145. end;
  146. writeln ('Options checked, now writing...');
  147. img.SaveToFile (WriteFile, Writer);
  148. end;
  149. procedure Clean;
  150. begin
  151. Reader.Free;
  152. Writer.Free;
  153. Img.Free;
  154. end;
  155. begin
  156. if (paramcount <> 4) and (paramcount <> 3) then
  157. begin
  158. writeln ('Give filename to read and to write, preceded by filetype:');
  159. writeln ('X for XPM, P for PNG, B for BMP, J for JPEG, T for TGA,');
  160. writeln ('N for PNM (read only), F for TIFF, G for gif (read only)');
  161. writeln ('example: imgconv X hello.xpm P hello.png');
  162. writeln ('example: imgconv hello.xpm P hello.png');
  163. writeln ('Options for');
  164. writeln (' PNG : G : grayscale, A : use alpha, ');
  165. writeln (' I : Indexed in palette, W : Word sized.');
  166. writeln (' TIFF : G16 write grayscale 16 bits/pixel');
  167. writeln (' G8 write grayscale 16 bits/pixel');
  168. writeln (' XPM : Number of chars to use for 1 pixel');
  169. writeln (' The color size of an XPM can be set after the X as 1,2,3 or 4');
  170. writeln ('example: imgconv hello.xpm PIA hello.png');
  171. writeln ('example: imgconv hello.png X2 hello.xpm');
  172. end
  173. else
  174. try
  175. writeln ('Initing');
  176. Init;
  177. writeln ('Reading image');
  178. ReadImage;
  179. writeln ('Writing image');
  180. WriteImage;
  181. writeln ('Clean up');
  182. Clean;
  183. except
  184. on e : exception do
  185. writeln ('Error: ',e.message);
  186. end;
  187. end.