imgconv.pp 4.5 KB

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