imgconv.pp 4.6 KB

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