1 /*
2  * This software is copyrighted as noted below.  It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is".  Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * fadesec.c - fades from one sequence to another one.
20  *
21  * Author:      Raul Rivero
22  *              Mathematics Dept.
23  *              University of Oviedo
24  * Date:        Wed Nov 20 1991
25  * Copyright (c) 1991, Raul Rivero
26  *
27  */
28 /*
29  * Modifications:
30  *
31  * --
32  *
33  * Author:      Raul Rivero
34  *              Vicerrectorado de Estudiantes
35  *              University of Oviedo
36  * Date:        Mon Dec 27 1993
37  *
38  *		This code was written before LUG, so it was c-h-a-n-g-e-d
39  *              to use LUG. It appears works fine but be careful ( i don't
40  *              have too much time to check it :-( ).
41  *
42  * --
43  */
44 
45 #include <lug.h>
46 #include <lugfnts.h>
47 
48 long tam_fichero();
49 byte *lee_fichero();
50 
51 byte *rbuf, *gbuf, *bbuf;
52 
53 extern char *MY_NAME;
54 extern int LUGverbose;
55 
main(argc,argv)56 main(argc, argv)
57 int argc;
58 char **argv;
59 {
60   int numero;
61   int ini1, ini2;
62 
63   MY_NAME = argv[0];
64 
65   if ( argc > 1 )
66     if (!strcmp("-v", argv[1])) {
67       /*
68        * Activamos la opcion 'verbose' y desplazamos los argumentos.
69        */
70     LUGverbose= 1;
71     argc--;
72     argv++;
73   }
74 
75   if (argc < 5)
76     old_error(0);
77 
78   if ( !(ini1= atoi(argv[2])) || !(ini2= atoi(argv[4])) ||
79        !(numero= atoi(argv[6])) )
80     old_error(1);
81 
82   trata(argv[1], ini1, argv[3], ini2, argv[5], numero);
83   VPRINTF(stdout, "\rFundido concluido.                                  \n");
84   return(0);
85 }
86 
87 /*
88  * Se encarga de llamar al fundido con los fichero correspondientes y de
89  * copiar los dos frames que quedan iguales.
90  */
trata(nombre1,ini1,nombre2,ini2,nombre,veces)91 trata(nombre1, ini1, nombre2, ini2, nombre, veces)
92 char *nombre1, *nombre2, *nombre;
93 int ini1, ini2, veces;
94 {
95   register int i;
96   char aux1[80], aux2[80];
97   char destino[80];
98   float incremento;
99   float grado;
100 
101   /*
102    * Como el primer y ultimo frame son iguales ( estamos en un fundido ),
103    * nos limitamos a copiarlos ( ley del minimo esfuerzo ).
104    */
105   VPRINTF(stdout, "Copiando frames"), fflush(stdout);
106   sprintf(aux1, "%s.%d", nombre1, ini1++);
107   sprintf(destino, "%s.1", nombre);
108   copia(aux1, destino);    /* el primero */
109   sprintf(aux2, "%s.%d", nombre2, ini2+veces-1);
110   sprintf(destino, "%s.%d", nombre, veces);
111   copia(aux2, destino);    /* el ultimo */
112   ini2++;
113 
114   /*
115    * Vamos a hacer <veces> fundidos de una pareja de imagenes. Para ello
116    * utilizamos una variable que nos indicara el % de la imagen segunda
117    * que debemos poner.
118    */
119   incremento = 1.0 / ((float) (veces-1));
120   grado= incremento;
121 
122   /*
123    * Ahora nos limitamos a llamar a fade para que funda dos imagenes y lo
124    * almacene en otra.
125    */
126   for (i= 2; i< veces; i++, grado+= incremento) {
127     sprintf(aux1, "%s.%d", nombre1, ini1++);
128     sprintf(aux2, "%s.%d", nombre2, ini2++);
129     sprintf(destino, "%s.%d", nombre, i);
130     VPRINTF(stdout, "\r Paso %d: %s & %s -> %s", i, aux1, aux2, destino), fflush(stdout);
131     fade(aux1, aux2, destino, grado);
132   }
133 }
134 
135 #define ASIGNA     {  rptr1= img_base.r;      \
136                       gptr1= img_base.g;      \
137                       bptr1= img_base.b;      \
138                       rptr2= img_destino.r;   \
139                       gptr2= img_destino.g;   \
140                       bptr2= img_destino.b;   \
141                       dif_ptr= dif; }
142 /*
143  * Aqui esta el tomate y seria muy largo explicarlo. Si no entiendes algo
144  * puedes preguntarme ( pero no opino ni de politica, ni de religion, ni
145  * de deporte, ni de sexo, ni de ciencia, ni de arte. Para cualquier otro
146  * tema, ya sabes, estoy disponible ).
147  */
fade(nombre1,nombre2,destino,grado)148 fade(nombre1, nombre2, destino, grado)
149 char *nombre1, *nombre2, *destino;
150 float grado;
151 {
152   register int i, j;
153   bitmap_hdr img_base;
154   bitmap_hdr img_destino;
155   byte *rptr1, *gptr1, *bptr1;
156   byte *rptr2, *gptr2, *bptr2;
157   byte *buffer;
158   short *dif, *dif_ptr;
159   long total_size;
160 
161   /* Leemos la imagen base */
162   read_lug_file( nombre1, &img_base );
163 
164   /* Ahora la destino */
165   read_lug_file( nombre2, &img_destino );
166 
167   if ( img_base.xsize != img_destino.xsize ||  /* deben ser iguales */
168        img_base.ysize != img_destino.ysize )
169     old_error(2);
170 
171   total_size= img_base.xsize * img_base.ysize;
172   if ((dif= (short *) malloc(3 * total_size * sizeof(short))) == NULL)
173     old_error(3);
174 
175   /*
176    * Para pasar de una imagen a otra hay que ir incrementando/disminuyendo
177    * sus respectivos colores, ahora vamos a calcular esos incrementos/decrementos.
178    */
179   ASIGNA;
180   for (i= 0; i< total_size; i++) {
181     *dif_ptr++ = ((short) *rptr2++ - (short) *rptr1++);
182     *dif_ptr++ = ((short) *gptr2++ - (short) *gptr1++);
183     *dif_ptr++ = ((short) *bptr2++ - (short) *bptr1++);
184   }
185 
186   /*
187    * Tenemos que escribir imagenes que vayan desde la primera animacion hasta
188    * la segunda. Asi que lo que escribiremos sera la primera imagen mas
189    * el incremento que corresponda por el grado de solapacion de la segunda.
190    * En otras palabras ( ya, ya se que me lo agradeces ), lo que hacemos
191    * es calcular que diferencia hay de la primera imagen a la segunda y
192    * segun donde estemos, el grado de fundido, metemos mas o menos de
193    * este incremento.
194    */
195   ASIGNA;
196   for (j= 0; j< total_size; j++) {
197     *rptr2++ = *rptr1++ + (byte)(grado * ((float) *dif_ptr++));
198     *gptr2++ = *gptr1++ + (byte)(grado * ((float) *dif_ptr++));
199     *bptr2++ = *bptr1++ + (byte)(grado * ((float) *dif_ptr++));
200   }
201   write_lug_file( destino, &img_destino );
202 
203   /*
204    * Liberamos los buffers utilizados ( you are freeeee !!! ).
205    */
206   free(dif);
207   freebitmap( &img_base );
208   freebitmap( &img_destino );
209 }
210 
copia(fuente,destino)211 copia(fuente, destino)
212 char *fuente;
213 char *destino;
214 {
215   bitmap_hdr aux;
216 
217   /* Leemos, ... */
218   read_lug_file( fuente, &aux );
219   /* escribimos, ... */
220   write_lug_file( destino, &aux );
221   /* ... y ADIOS! :-) */
222   freebitmap( &aux );
223 }
224 
225 /*
226  * This program had an error subroutine but LUG has another. So, i
227  * renamed this on.
228  */
old_error(tipo)229 old_error( tipo )
230 int tipo;
231 {
232   VPRINTF(stdout, "\n");
233   fprintf(stderr, "%s: ", MY_NAME);
234   switch( tipo ) {
235     case  0: fprintf(stderr, "Uso:   %s <source> <sframe> <target> <tframe> <name> <no_frames> \n\n", MY_NAME);
236 	     fprintf(stdout, "Crea una secuencia que es un fundido de la animacion <source>\n");
237              fprintf(stdout, "a la <target>. El fundido comenzara en el frame <sframe> para la\n");
238              fprintf(stdout, "primera, en <tframe> para la ultima y se realizara en <no_frames>.\n");
239              fprintf(stdout, "Como resultado se obtendra una nueva animacion, de <no_frames>, y\n");
240              fprintf(stdout, "llamada <name>.\n");
241 	     fprintf(stdout, "El formato de los ficheros es ALIAS ( 24 planos ).\n");
242              break;
243     case  1: fprintf(stderr, "Error en introduccion de los numeros\n");
244              fprintf(stderr, "Uso:   %s <source> <sframe> <target> <tframe> <name> <no_frames> \n\n", MY_NAME);
245              break;
246     case  2: fprintf(stderr, "Imaganes con distinto taman~o\n");
247              break;
248     case  3: fprintf(stderr, "No hay memoria\n");
249              break;
250     case  4: fprintf(stderr, "Las imagenes deben ser de 24 bits\n");
251              break;
252     case  5: fprintf(stderr, "Error en apertura de ficheros\n");
253              break;
254     case  6: fprintf(stderr, "Error durante copia de ficheros\n");
255              break;
256     default: fprintf(stderr, "Error, codigo desconocido (%d)\n", tipo);
257              break;
258   }
259   exit(tipo);
260 }
261 
262