KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > graphics > GifEncoder


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util.graphics;
25
26 import java.awt.Color JavaDoc;
27 import java.awt.image.BufferedImage JavaDoc;
28 import java.io.DataOutputStream JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.TreeSet JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 public class GifEncoder
35 {
36
37     String JavaDoc b;
38     int g;
39     int a;
40     int h;
41     int f;
42     int c[];
43     int i[];
44     int e[];
45     TreeSet JavaDoc d;
46
47     private void a(int ai[], int j, DataOutputStream JavaDoc dataoutputstream) throws Exception JavaDoc
48     {
49         try
50         {
51             boolean flag2 = false;
52             int l;
53             int j3 = (1 << (l = j + 1)) - 1;
54             int i2 = (1 << j) + 2;
55             byte abyte0[] = new byte[255];
56             int ai1[] = new int[4096];
57             int ai2[] = new int[4096];
58             int ai3[] = new int[4096];
59             int ai4[] = new int[i2];
60             int k;
61             for(k = 0; k < i2; k++)
62             {
63                 ai4[k] = 0xffffffff | k;
64                 ai3[k] = -1;
65             }
66
67             for(; k < 4096; k++)
68             {
69                 ai3[k] = -1;
70             }
71
72             System.arraycopy(ai3, 0, ai1, 0, 4096);
73             System.arraycopy(ai3, 0, ai2, 0, 4096);
74             System.arraycopy(ai4, 0, ai1, 0, i2);
75             int j1 = ai[0];
76             k = 1;
77             boolean flag1 = false;
78             int j2 = 0;
79             int k2 = (1 << l) - 1;
80             boolean flag = true;
81             int i3 = 0;
82             int i1 = 0;
83             j2 |= 1 << j + i3;
84             for(i3 += l; i3 >= 8;)
85             {
86                 try
87                 {
88                     abyte0[i1++] = (byte)j2;
89                 }
90                 catch(ArrayIndexOutOfBoundsException JavaDoc arrayindexoutofboundsexception)
91                 {
92                     dataoutputstream.writeByte(255);
93                     dataoutputstream.write(abyte0);
94                     abyte0[i1 = 0] = (byte)j2;
95                     i1++;
96                 }
97                 i3 -= 8;
98                 j2 >>= 8;
99             }
100
101             try
102             {
103                 do
104                 {
105                     int k1;
106                     int l1 = j1 << 16 | (k1 = ai[k++]);
107                     int k3;
108                     for(k3 = j1; ai1[k3] != l1 && ai2[k3] >= 0; k3 = ai2[k3]) { }
109                     if(ai1[k3] != l1)
110                     {
111                         j2 |= j1 << i3;
112                         for(i3 += l; i3 >= 8;)
113                         {
114                             try
115                             {
116                                 abyte0[i1++] = (byte)j2;
117                             }
118                             catch(ArrayIndexOutOfBoundsException JavaDoc arrayindexoutofboundsexception1)
119                             {
120                                 dataoutputstream.writeByte(255);
121                                 dataoutputstream.write(abyte0);
122                                 abyte0[i1 = 0] = (byte)j2;
123                                 i1++;
124                             }
125                             i3 -= 8;
126                             j2 >>= 8;
127                         }
128
129                         if(i2 > j3)
130                         {
131                             l++;
132                             j3 = (j3 << 1) + 1;
133                         }
134                         try
135                         {
136                             ai2[k3] = i2;
137                             ai1[i2++] = j1 << 16 | k1;
138                             j1 = k1;
139                         }
140                         catch(ArrayIndexOutOfBoundsException JavaDoc arrayindexoutofboundsexception2)
141                         {
142                             j1 = k1;
143                             l--;
144                             j2 |= 1 << j + i3;
145                             for(i3 += l; i3 >= 8;)
146                             {
147                                 try
148                                 {
149                                     abyte0[i1++] = (byte)j2;
150                                 }
151                                 catch(ArrayIndexOutOfBoundsException JavaDoc arrayindexoutofboundsexception5)
152                                 {
153                                     dataoutputstream.writeByte(255);
154                                     dataoutputstream.write(abyte0);
155                                     abyte0[i1 = 0] = (byte)j2;
156                                     i1++;
157                                 }
158                                 i3 -= 8;
159                                 j2 >>= 8;
160                             }
161
162                             j3 = (1 << (l = j + 1)) - 1;
163                             i2 = (1 << j) + 2;
164                             int l2 = (1 << l) - 1;
165                             System.arraycopy(ai3, 0, ai1, 0, 4096);
166                             System.arraycopy(ai3, 0, ai2, 0, 4096);
167                             System.arraycopy(ai4, 0, ai1, 0, i2);
168                         }
169                     }
170                     else
171                     {
172                         j1 = k3;
173                     }
174                 }
175                 while(true);
176             }
177             catch(Exception JavaDoc exception)
178             {
179                 j2 |= j1 << i3;
180             }
181             for(i3 += l; i3 >= 8;)
182             {
183                 try
184                 {
185                     abyte0[i1++] = (byte)j2;
186                 }
187                 catch(ArrayIndexOutOfBoundsException JavaDoc arrayindexoutofboundsexception3)
188                 {
189                     dataoutputstream.writeByte(255);
190                     dataoutputstream.write(abyte0);
191                     abyte0[i1 = 0] = (byte)j2;
192                     i1++;
193                 }
194                 i3 -= 8;
195                 j2 >>= 8;
196             }
197
198             j2 |= (1 << j) + 1 << i3;
199             for(i3 += l; i3 > 0;)
200             {
201                 try
202                 {
203                     abyte0[i1++] = (byte)j2;
204                 }
205                 catch(ArrayIndexOutOfBoundsException JavaDoc arrayindexoutofboundsexception4)
206                 {
207                     dataoutputstream.writeByte(255);
208                     dataoutputstream.write(abyte0);
209                     abyte0[i1 = 0] = (byte)j2;
210                     i1++;
211                 }
212                 i3 -= 8;
213                 j2 >>= 8;
214             }
215
216             dataoutputstream.writeByte(i1);
217             dataoutputstream.write(abyte0, 0, i1);
218             dataoutputstream.writeByte(0);
219             return;
220         }
221         catch(Exception JavaDoc e) { }
222     }
223
224     public void addTransparentColor(Color JavaDoc color)
225     {
226         try
227         {
228             if(f < 256)
229             {
230                 c[f++] = color.getRGB();
231             }
232             return;
233         }
234         catch(Exception JavaDoc e) { }
235     }
236
237     public void setTransparentColors(Vector JavaDoc vector)
238     {
239         try
240         {
241             Iterator JavaDoc iterator = vector.iterator();
242             while(iterator.hasNext())
243             {
244                 Color JavaDoc color = (Color JavaDoc)iterator.next();
245                 addTransparentColor(color);
246             }
247
248             return;
249         }
250         catch(Exception JavaDoc e) { }
251     }
252
253     public void encode(BufferedImage JavaDoc bufferedimage, DataOutputStream JavaDoc dataoutputstream, Hashtable JavaDoc hashtable) throws Exception JavaDoc
254     {
255         try
256         {
257             a = bufferedimage.getWidth();
258             g = bufferedimage.getHeight();
259             e = bufferedimage.getRGB(0, 0, a, g, null, 0, a);
260             int i4 = 0;
261             b = hashtable.get("encoding").toString();
262             if(b.equals("websafe"))
263             {
264                 int ai[] = new int[256];
265                 i = new int[256];
266                 h = 8;
267                 int k1 = 0;
268                 int j;
269                 int j1 = j = 0;
270                 for(; j <= 255; j += 51)
271                 {
272                     for(int l = 0; l <= 255; l += 51)
273                     {
274                         for(int i1 = 0; i1 <= 255;)
275                         {
276                             i[j1] = (j << 16) + (l << 8) + i1;
277                             ai[k1++] = j1;
278                             i1 += 51;
279                             j1++;
280                         }
281
282                     }
283
284                 }
285
286                 if(f > 0)
287                 {
288                     int j4 = c[0];
289                     int l1 = ((c[0] >> 16 & 0xff) + 25) / 51;
290                     int k2 = ((c[0] >> 8 & 0xff) + 25) / 51;
291                     int j3 = ((c[0] & 0xff) + 25) / 51;
292                     i4 = l1 * 36 + k2 * 6 + j3;
293                     for(j = 1; j < f; j++)
294                     {
295                         int i2 = ((c[j] >> 16 & 0xff) + 25) / 51;
296                         int l2 = ((c[j] >> 8 & 0xff) + 25) / 51;
297                         int k3 = ((c[j] & 0xff) + 25) / 51;
298                         ai[i2 * 36 + l2 * 6 + k3] = i4;
299                     }
300
301                 }
302                 j = 0;
303                 try
304                 {
305                     do
306                     {
307                         int i5 = e[j];
308                         int j2 = ((i5 >> 16 & 0xff) + 25) / 51;
309                         int i3 = ((i5 >> 8 & 0xff) + 25) / 51;
310                         int l3 = ((i5 & 0xff) + 25) / 51;
311                         e[j++] = ai[j2 * 36 + i3 * 6 + l3];
312                     }
313                     while(true);
314                 }
315                 catch(Exception JavaDoc exception1) { }
316             }
317             /*else
318             if(b.equals("optimized"))
319             {
320                 try
321                 {
322                     int k4 = Integer.parseInt(hashtable.get("colors").toString());
323                     for(h = 1; k4 - 1 >> h > 0; h++) { }
324                     i = new int[1 << h];
325                     CSelectiveQuant cselectivequant = new CSelectiveQuant();
326                     for(int j5 = 0; j5 < e.length; j5++)
327                     {
328                         cselectivequant.addPixel(e[j5]);
329                     }
330
331                     boolean flag = f > 0;
332                     int k5 = flag ? 1 : 0;
333                     int ai1[] = cselectivequant.createPalette(k4 - k5);
334                     for(int l5 = 0; l5 < i.length; l5++)
335                     {
336                         try
337                         {
338                             i[l5] = ai1[l5 - k5];
339                         }
340                         catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception)
341                         {
342                             i[l5] = 0;
343                         }
344                     }
345
346                     if(flag)
347                     {
348                         i4 = 0;
349                         for(int i6 = 0; i6 < f; i6++)
350                         {
351                             cselectivequant.setIndex(c[i6], -1);
352                         }
353
354                     }
355                     for(int j6 = 0; j6 < e.length; j6++)
356                     {
357                         e[j6] = cselectivequant.getIndex(e[j6]) + k5;
358                     }
359
360                 }
361                 catch(NumberFormatException numberformatexception)
362                 {
363                     logger.info("Parameter: 'colors' is malformated...");
364                     return;
365                 }
366             }
367             */

368             dataoutputstream.write("GIF89a".getBytes());
369             dataoutputstream.writeByte(a);
370             dataoutputstream.writeByte(a >> 8);
371             dataoutputstream.writeByte(g);
372             dataoutputstream.writeByte(g >> 8);
373             dataoutputstream.writeByte(0xf0 | h - 1);
374             dataoutputstream.writeByte(0);
375             dataoutputstream.writeByte(0);
376             int k = 0;
377             try
378             {
379                 do
380                 {
381                     int l4 = i[k++];
382                     dataoutputstream.writeByte(l4 >> 16 & 0xff);
383                     dataoutputstream.writeByte(l4 >> 8 & 0xff);
384                     dataoutputstream.writeByte(l4 & 0xff);
385                 }
386                 while(true);
387             }
388             catch(Exception JavaDoc exception) { }
389             if(f > 0)
390             {
391                 dataoutputstream.writeByte(33);
392                 dataoutputstream.writeByte(249);
393                 dataoutputstream.writeByte(4);
394                 dataoutputstream.writeByte(1);
395                 dataoutputstream.writeByte(0);
396                 dataoutputstream.writeByte(0);
397                 dataoutputstream.writeByte(i4);
398                 dataoutputstream.writeByte(0);
399             }
400             dataoutputstream.writeByte(44);
401             dataoutputstream.writeByte(0);
402             dataoutputstream.writeByte(0);
403             dataoutputstream.writeByte(0);
404             dataoutputstream.writeByte(0);
405             dataoutputstream.writeByte(a);
406             dataoutputstream.writeByte(a >> 8);
407             dataoutputstream.writeByte(g);
408             dataoutputstream.writeByte(g >> 8);
409             dataoutputstream.writeByte(0);
410             dataoutputstream.writeByte(h);
411             a(e, h, dataoutputstream);
412             dataoutputstream.writeByte(59);
413             dataoutputstream.flush();
414             return;
415         }
416         catch(Exception JavaDoc e) { }
417     }
418
419     public GifEncoder()
420     {
421         f = 0;
422         c = new int[256];
423     }
424 }
425
Popular Tags