GIF is a common image storage format, usable for a limited color palette - a GIF image can only contain as most 256 colors - and animations.
Simple encoding: encode, encode_trans
Advanced stuff: render_block, header_block, end_block, netscape_loop_block
Very advanced stuff: _render_block, _gce_block
"image":the image "alpha":the alpha channel"xsize":int "ysize":int size of image "type":"image/gif" file type information as MIME type
The latter (encode_trans) functions add transparency capabilities.
Example:
img=Image.Image([...]); [...] // make your very-nice image write(Image.GIF.encode(img)); // write it as GIF on stdout
argument(s) | description |
object img | The image which to encode. |
int colors object colortable | These arguments decides what colors the image should be encoded with. If a number is given, a colortable with be created with (at most) that amount of colors. Default is '256' (GIF maximum amount of colors). |
object alpha | Alpha channel image (defining what is transparent); black
color indicates transparency. GIF has only transparent
or nontransparent (no real alpha channel).
You can always dither a transparency channel:
Image.Colortable(my_alpha, ({({0,0,0}),({255,255,255})})) |
int tr_r int tr_g int tr_b | Use this (or the color closest to this) color as transparent pixels. |
int a_r int a_g int a_b | Encode transparent pixels (given by alpha channel image) to have this color. This option is for making GIFs for the decoders that doesn't support transparency. |
int transp_index | Use this color no in the colortable as transparent color. |
Image.GIF.encode_trans(img,colortable,alpha);is equivalent of using
Image.GIF.header_block(img->xsize(),img->ysize(),colortable)+ Image.GIF.render_block(img,colortable,0,0,0,alpha)+ Image.GIF.end_block();and is actually implemented that way.
The result of this function is always ";" or "\x3b", but I recommend using this function anyway for code clearity.
Giving a colortable to this function includes a global palette in the header block.
argument(s) | description |
int xsize int ysize | Size of drawing area. Usually same size as in the first (or only) render block(s). |
int background_color_index | This color in the palette is the background color. Background is visible if the following render block(s) doesn't fill the drawing area or are transparent. Most decoders doesn't use this value, though. |
int gif87a | If set, write 'GIF87a' instead of 'GIF89a' (default 0 == 89a). |
int aspectx int aspecty | Aspect ratio of pixels, ranging from 4:1 to 1:4 in increments of 1/16th. Ignored by most decoders. If any of aspectx or aspecty is zero, aspectratio information is skipped. |
int r int g int b | Add this color as the transparent color. This is the color used as transparency color in case of alpha-channel given as image object. This increases (!) the number of colors by one. |
This GIF encoder doesn't support different size of colors in global palette and color resolution.
argument(s) | description |
int number_of_loops | Number of loops. Max and default is 65535. |
Example:
img1=Image.Image([...]); img2=Image.Image([...]); [...] // make your very-nice images nct=Image.Colortable([...]); // make a nice colortable write(Image.GIF.header_block(xsize,ysize,nct)); // write a GIF header write(Image.GIF.render_block(img1,nct,0,0,0,10)); // write a render block write(Image.GIF.render_block(img2,nct,0,0,0,10)); // write a render block [...] write(Image.GIF.end_block()); // write end block // voila! A GIF animation on stdout.
The above animation is thus created:
object nct=colortable(lena,32,({({0,0,0})})); string s=GIF.header_block(lena->xsize(),lena->ysize(),nct); foreach ( ({lena->xsize(), (int)(lena->xsize()*0.75), (int)(lena->xsize()*0.5), (int)(lena->xsize()*0.25), (int)(1), (int)(lena->xsize()*0.25), (int)(lena->xsize()*0.5), (int)(lena->xsize()*0.75)}),int xsize) { object o=lena->scale(xsize,lena->ysize()); object p=lena->clear(0,0,0); p->paste(o,(lena->xsize()-o->xsize())/2,0); s+=GIF.render_block(p,nct,0,0,0,25); } s+=GIF.netscape_loop_block(200); s+=GIF.end_block(); write(s);
argument(s) | description |
object img | The image. |
object colortable | Colortable with colors to use and to write as palette. |
int x int y | Position of this image. |
int localpalette | If set, writes a local palette. |
object alpha | Alpha channel image; black is transparent. |
int r int g int b | Color of transparent pixels. Not all decoders understands transparency. This is ignored if localpalette isn't set. |
int delay | View this image for this many centiseconds. Default is zero. |
int transp_index | Index of the transparent color in the colortable. -1 indicates no transparency. |
int user_input | If set: wait the delay or until user input. If delay is zero, wait indefinitely for user input. May sound the bell upon decoding. Default is non-set. |
int disposal | Disposal method number;
|
The user_input and disposal method are unsupported in most decoders.
({int xsize,int ysize, // 0: size of image drawing area void|object colortable, // 2: opt. global colortable ({ int aspx, int aspy, // 3 0: aspect ratio or 0, 0 if not set int background }), // 2: index of background colorfollowed by any number these blocks in any order (gce chunks are decoded and incorporated in the render chunks):
({ GIF.RENDER, // 0: block identifier int x, int y, // 1: position of render object image, // 3: render image void|object alpha, // 4: 0 or render alpha channel object colortable, // 5: colortable (may be same as global)and possibly ended with one of these:int interlace, // 6: interlace flag int trans_index, // 7: 0 or transparent color index int delay, // 8: 0 or delay in centiseconds int user_input, // 9: user input flag int disposal}) // 10: disposal method number (0..7)
({ GIF.EXTENSION, // 0: block identifier int extension, // 1: extension number string data }) // 2: extension data
({ GIF.ERROR_PREMATURE_EOD }) // premature end-of-data({ GIF.ERROR_TOO_MUCH_DATA, // data following end marker string data }) // (rest of file)
({ GIF.ERROR_UNKNOWN_DATA, // unknown data string data }) // (rest of file)
The decode method uses this data in a way similar to this program:
import Image;object my_decode_gif(string data) { array a=GIF._decode(data); object img=image(a[0],a[1]); foreach (a[4..],array b) if (b[0]==GIF.RENDER) if (b[4]) img->paste_alpha(b[3],b[4],b[1],b[2]); else img->paste(b[3],b[1],b[2]); return img; }
argument(s) | description |
string gifdata | GIF data (with header and all) |
array __decoded | GIF data as from __decode |
This is in the very advanced sector of the GIF support; please read about how GIF files works.
argument(s) | description |
array data | data as returned from _encode |
argument(s) | description |
int transparency int transparency_index | The following image has transparency, marked with this index. |
int delay | View the following rendering for this many centiseconds (0..65535). |
int user_input | Wait the delay or until user input. If delay is zero, wait indefinitely for user input. May sound the bell upon decoding. |
int disposal | Disposal method number;
|
Most decoders just ignore some or all of these parameters.
argument(s) | description |
int x int y | Position of this image. |
int xsize int ysize | Size of the image. Length if the indices string must be xsize*ysize. |
int bpp | Bits per pixels in the indices. Valid range 1..8. |
string indices | The image indices as an 8bit indices. |
string colortable | Colortable with colors to write as palette. If this argument is zero, no local colortable is written. Colortable string len must be 1<<bpp. |
int interlace | Interlace index data and set interlace bit. The given string should _not_ be pre-interlaced. |
({int xsize,int ysize, // 0: size of image drawing area int numcol, // 2: suggested number of colors void|string colortable, // 3: opt. global colortable ({ int aspx, int aspy, // 4,0: aspect ratio or 0, 0 if not set int background }), // 1: index of background colorfollowed by any number these blocks in any order:
({ GIF.EXTENSION, // 0: block identifier int extension, // 1: extension number string data }) // 2: extension dataand possibly ended with one of these:({ GIF.RENDER, // 0: block identifier int x, int y, // 1: position of render int xsize, int ysize, // 3: size of render int interlace, // 5: interlace flag void|string colortbl, // 6: opt. local colortable int lzwsize, // 7: lzw code size string lzwdata }) // 8: packed lzw data
({ GIF.ERROR_PREMATURE_EOD }) // premature end-of-data({ GIF.ERROR_TOO_MUCH_DATA, // data following end marker string data }) // (rest of file)
({ GIF.ERROR_UNKNOWN_DATA, // unknown data string data }) // (rest of file)
This is in the very advanced sector of the GIF support; please read about how GIF files works.