| 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 |
#include <fontforge-config.h> |
| 30 |
|
| 31 |
#ifdef _NO_LIBUNGIF |
| 32 |
|
| 33 |
static int a_file_must_define_something=0; |
| 34 |
|
| 35 |
#else |
| 36 |
|
| 37 |
#include "basics.h" |
| 38 |
#include "gimage.h" |
| 39 |
|
| 40 |
#include <gif_lib.h> |
| 41 |
|
| 42 |
#if defined(GIFLIB_MAJOR) && defined(GIFLIB_MINOR) && ((GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1) || GIFLIB_MAJOR > 5) |
| 43 |
#define _GIFLIB_51PLUS |
| 44 |
#endif |
| 45 |
|
| 46 |
static GImage *ProcessSavedImage(GifFileType *gif,struct SavedImage *si,int il) { |
| 47 |
|
| 48 |
GImage *ret; |
| 49 |
struct _GImage *base; |
| 50 |
ColorMapObject *m = gif->SColorMap; |
| 51 |
int i,j,k,*id = NULL; |
| 52 |
long l; |
| 53 |
uint8_t *d,*iv = NULL; |
| 54 |
|
| 55 |
|
| 56 |
if ( si->ImageDesc.ColorMap!=NULL ) m=si->ImageDesc.ColorMap; |
| 57 |
if ( m==NULL ) |
| 58 |
return( NULL ); |
| 59 |
if ( m->BitsPerPixel==1 ) { |
| 60 |
if ( (ret=GImageCreate(it_bitmap,si->ImageDesc.Width,si->ImageDesc.Height))==NULL ) |
| 61 |
return( NULL ); |
| 62 |
if ( m->ColorCount==2 && |
| 63 |
m->Colors[0].Red==0 && m->Colors[0].Green==0 && m->Colors[0].Blue==0 && |
| 64 |
m->Colors[1].Red==255 && m->Colors[1].Green==255 && m->Colors[1].Blue==255 ) |
| 65 |
; |
| 66 |
else |
| 67 |
if ( (ret->u.image->clut = (GClut *) calloc(1,sizeof(GClut)))==NULL ) { |
| 68 |
free(ret); |
| 69 |
NoMoreMemMessage(); |
| 70 |
return( NULL ); |
| 71 |
} |
| 72 |
} else |
| 73 |
if ( (ret=GImageCreate(it_index,si->ImageDesc.Width,si->ImageDesc.Height))==NULL ) |
| 74 |
return( NULL ); |
| 75 |
if ( il && ((id=(int *) malloc(si->ImageDesc.Height*sizeof(int)))==NULL || \ |
| 76 |
(iv=(uint8_t *) malloc(si->ImageDesc.Height*sizeof(uint8_t)))==NULL) ) { |
| 77 |
free(ret->u.image->clut); |
| 78 |
free(ret); |
| 79 |
free(id); |
| 80 |
free(iv); |
| 81 |
NoMoreMemMessage(); |
| 82 |
return( NULL ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
base = ret->u.image; |
| 87 |
if ( base->clut!=NULL ) { |
| 88 |
base->clut->clut_len = m->ColorCount; |
| 89 |
for ( i=0; i<m->ColorCount; ++i ) |
| 90 |
base->clut->clut[i] = COLOR_CREATE(m->Colors[i].Red,m->Colors[i].Green,m->Colors[i].Blue); |
| 91 |
} |
| 92 |
if ( m->BitsPerPixel!=1 ) |
| 93 |
memcpy(base->data,si->RasterBits,base->width*base->height); |
| 94 |
else if ( m->BitsPerPixel==1 ) { |
| 95 |
l=0; |
| 96 |
for ( i=0; i<base->height; ++i ) { |
| 97 |
d = (base->data + i*base->bytes_per_line); |
| 98 |
memset(d,'\0',base->bytes_per_line); |
| 99 |
for ( j=0; j<base->width; ++j ) { |
| 100 |
if ( si->RasterBits[l] ) |
| 101 |
d[j>>3] |= (1<<(7-(j&7))); |
| 102 |
++l; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
if ( il ) { |
| 107 |
|
| 108 |
j=0; k=0; |
| 109 |
for ( i=0; i<base->height; ++i ) { |
| 110 |
id[i]=k; |
| 111 |
if ( j==0 ) { |
| 112 |
k += 8; |
| 113 |
if ( k>=base->height ) { |
| 114 |
j++; k=4; |
| 115 |
} |
| 116 |
} else if ( j==1 ) { |
| 117 |
k += 8; |
| 118 |
if ( k>=base->height ) { |
| 119 |
j++; k=2; |
| 120 |
} |
| 121 |
} else if ( j==2 ) { |
| 122 |
k += 4; |
| 123 |
if ( k>=base->height ) { |
| 124 |
j++; k=1; |
| 125 |
} |
| 126 |
} else |
| 127 |
k += 2; |
| 128 |
} |
| 129 |
for ( j=0; j<base->bytes_per_line; ++j ) { |
| 130 |
for ( i=1; i<base->height; ++i ) |
| 131 |
iv[id[i]]=base->data[i*base->bytes_per_line+j]; |
| 132 |
for ( i=1; i<base->height; ++i ) |
| 133 |
base->data[i*base->bytes_per_line+j]=iv[i]; |
| 134 |
} |
| 135 |
free(id); |
| 136 |
free(iv); |
| 137 |
} |
| 138 |
for ( i=0; i<si->ExtensionBlockCount; ++i ) { |
| 139 |
if ( si->ExtensionBlocks[i].Function==0xf9 && |
| 140 |
si->ExtensionBlocks[i].ByteCount>=4 ) { |
| 141 |
base->delay = (si->ExtensionBlocks[i].Bytes[2]<<8) | |
| 142 |
(si->ExtensionBlocks[i].Bytes[2]&0xff); |
| 143 |
if ( si->ExtensionBlocks[i].Bytes[0]&1 ) { |
| 144 |
base->trans = (unsigned char) si->ExtensionBlocks[i].Bytes[3]; |
| 145 |
if ( base->clut!=NULL ) |
| 146 |
base->clut->trans_index = base->trans; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
return( ret ); |
| 151 |
} |
| 152 |
|
| 153 |
GImage *GImageReadGif(char *filename) { |
| 154 |
|
| 155 |
GImage *ret, **images; |
| 156 |
GifFileType *gif; |
| 157 |
int i,il; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 || defined(_GIFLIB_5PLUS) |
| 167 |
if ( (gif=DGifOpenFileName(filename,NULL))==NULL ) { |
| 168 |
#else |
| 169 |
if ( (gif=DGifOpenFileName(filename))==NULL ) { |
| 170 |
#endif |
| 171 |
fprintf( stderr,"Can't open \"%s\"\n",filename ); |
| 172 |
return( NULL ); |
| 173 |
} |
| 174 |
|
| 175 |
if ( DGifSlurp(gif)!=GIF_OK ) { |
| 176 |
fprintf(stderr,"Bad input file \"%s\"\n",filename ); |
| 177 |
#ifdef _GIFLIB_51PLUS |
| 178 |
DGifCloseFile(gif, NULL); |
| 179 |
#else |
| 180 |
DGifCloseFile(gif); |
| 181 |
#endif |
| 182 |
return( NULL ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
if ( (images=(GImage **) malloc(gif->ImageCount*sizeof(GImage *)))==NULL ) { |
| 187 |
#ifdef _GIFLIB_51PLUS |
| 188 |
DGifCloseFile(gif, NULL); |
| 189 |
#else |
| 190 |
DGifCloseFile(gif); |
| 191 |
#endif |
| 192 |
NoMoreMemMessage(); |
| 193 |
return( NULL ); |
| 194 |
} |
| 195 |
il=gif->SavedImages[0].ImageDesc.Interlace; |
| 196 |
for ( i=0; i<gif->ImageCount; ++i ) { |
| 197 |
if ( (images[i]=ProcessSavedImage(gif,&gif->SavedImages[i],il))==NULL ) { |
| 198 |
while ( --i>=0 ) free(images[i]); |
| 199 |
free(images); |
| 200 |
#ifdef _GIFLIB_51PLUS |
| 201 |
DGifCloseFile(gif, NULL); |
| 202 |
#else |
| 203 |
DGifCloseFile(gif); |
| 204 |
#endif |
| 205 |
return( NULL ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
if ( gif->ImageCount==1 ) |
| 211 |
ret = images[0]; |
| 212 |
else |
| 213 |
ret = GImageCreateAnimation(images,gif->ImageCount); |
| 214 |
#ifdef _GIFLIB_51PLUS |
| 215 |
DGifCloseFile(gif, NULL); |
| 216 |
#else |
| 217 |
DGifCloseFile(gif); |
| 218 |
#endif |
| 219 |
free(images); |
| 220 |
return( ret ); |
| 221 |
} |
| 222 |
|
| 223 |
#endif |