Ejemplo de conversion de imagen

¿Has encontrado un error? ¿Tienes la solución? Deja tu correción ;-)

Antes de comentar: Gran parte de los ejercicios propuestos no tienen librerías debido a que Wordpress las eliminó al verlas como etiquetas HTML. Si sabes/tienes/conoces las librerías que hacen falta, déjalo en los comentarios. Y lo mas importante: Todos los ejemplos fueron realizados por estudiante con únicamente conocimiento básico del lenguaje, no de programación.

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

Punto importante: Si vas a sugerir un segmento de código en algún lenguaje debes hacerlo así:

  • Si es lenguaje C <code lang="c">Código en C</code>
  • Si es lenguaje Pascal <code lang="pascal">Aquí dentro el código de Pascal</code>.

De esta manera el código coloreas el código.

Deja un comentario

Suscribirse a los comentarios.