1 /* 2 * Copyright(c) 2012-2012, John Forkosh Associates, Inc. All rights reserved. 3 * http://www.forkosh.com mailto: john@forkosh.com 4 * Copyright (C) 2014 haru <uobikiemukot at gmail dot com> 5 * Copyright (C) 2014 Hayaki Saito <user@zuse.jp> 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 /* --- program id --- */ 22 #define PROGRAMID "gifsave89" /* program name */ 23 #define GIFSAVE_VERSION "1.00" /* version number */ 24 #define REVISIONDATE " 10 Oct 2012 " /* date of most recent revision */ 25 #define COPYRIGHTTEXT "Copyright(c) 2012-2012, John Forkosh Associates, Inc" 26 /* --- program messages --- */ 27 static char *copyright = /* gnu/gpl copyright notice */ 28 "+-----------------------------------------------------------------------+\n" 29 "|" PROGRAMID " ver" GIFSAVE_VERSION ", " COPYRIGHTTEXT "|\n" 30 "| ( current revision:" REVISIONDATE 31 ") |\n" 32 "+-----------------------------------------------------------------------+\n" 33 "|gifsave89 is free software, licensed to you under terms of the GNU/GPL,|\n" 34 "| and comes with absolutely no warranty whatsoever. |\n" 35 "| See http://www.forkosh.com/gifsave89.html for further details. |\n" 36 "+-----------------------------------------------------------------------+"; 37 38 /* ------------------------------------------------------------------------- 39 url of cgi to pixelize plaintext expressions (see plainmimetext() below)... 40 -------------------------------------------------------------------------- */ 41 /* --- pixelizing cgi --- */ 42 #if !defined(MIMETEX) 43 #define MIMETEX "http://www.forkosh.com/mimetex.cgi" 44 #endif 45 /* --- local /path/to/wget to run the cgi (-DWGET to supply path/to) --- */ 46 #if !defined(WGET) 47 #define WGET "wget" 48 #endif 49 50 /* ------------------------------------------------------------------------- 51 gifsave89 data and structures... 52 -------------------------------------------------------------------------- */ 53 /* --- 54 * gifsave89 datatypes 55 * ---------------------- */ 56 typedef uint8_t BYTE; /* one byte (8 bits) */ 57 typedef uint16_t WORD; /* used for two byte integers */ 58 59 /* --- 60 * gif image buffer (note: ptr to caller's buffer address is BYTE **block) 61 * ----------------------------------------------------------------------- */ 62 #define BLOCK struct _Block 63 #define BK BLOCK /* shorthand for funcs and args */ 64 #define MAXBLKBYTES (1024) /* initial gif buffer allocation */ 65 BLOCK { 66 BYTE **block; /* block buffer */ 67 int nblkbytes, /* #bytes already in our block */ 68 maxblkbytes; /* #bytes currently (re)allocated */ 69 }; /* --- end-of-struct BLOCK --- */ 70 71 /* --- 72 * data subblock (as per gif standard) 73 * -------------------------------------- */ 74 #define SUBBLOCK struct _SubBlock 75 #define SB SUBBLOCK /* shorthand for funcs and args */ 76 #define SUBBLOCKSIZE (255) /* maximum gif subblock size */ 77 #define BITSPERBYTE (8) /* #bits per byte */ 78 SUBBLOCK { 79 BYTE subblock[SUBBLOCKSIZE + 1]; /* subblock buffer */ 80 BLOCK *block; /* parent block to flush subblock */ 81 int nsubbytes, /* #bytes already in our subblock */ 82 nsubbits; /* #bits already in current byte */ 83 int index; /* >=0 writes subblock index# byte */ 84 }; /* --- end-of-struct SUBBLOCK --- */ 85 86 /* --- 87 * LZW parameters 88 * ----------------- */ 89 #define POW2(n) ((int)(1<<(n))) /* 2**n */ 90 #define RESCODES (2) 91 #define HASHFREE (0xFFFF) 92 #define NEXTFIRST (0xFFFF) 93 #define CODEBITS (12) 94 #define NSTRINGS POW2((CODEBITS)) 95 #define HASHSIZE (9973) 96 #define HASHSTEP (2039) 97 #define HASH(index,byte) ( (((byte) << 8) ^ (index)) % (HASHSIZE) ) 98 99 /* --- 100 * "global" gifsave89 data 101 * Note: gifsave89 is reentrant by keeping all variables that need to 102 * be maintained from call to call in this struct, i.e., no globals. 103 * --------------------------------------------------------------------- */ 104 #define GIFSAVE89 struct _GifSave89 105 #define GS GIFSAVE89 /* shorthand for funcs and args */ 106 GIFSAVE89 { 107 /* --- 108 * gif image block and subblock buffers 109 * --------------------------------------- */ 110 BLOCK gifimage; /* buffer for gif image block */ 111 SUBBLOCK gifsubblock; /* buffer for gifimage subblock */ 112 /* --- 113 * LZW string table 114 * ------------------- */ 115 BYTE strbyte[NSTRINGS]; 116 int strnext[NSTRINGS], strhash[HASHSIZE], nstrings; 117 /* --- 118 * additional control data 119 * -------------------------- */ 120 int version; /* 87=GIF87a, 89=GIF89a */ 121 int width, height; /* #row, #col pixels for screen */ 122 int ncolorbits_gct, /* #bits/index (global color table) */ 123 ncolorbits; /* local(if given) or global #bits */ 124 int bgindex; /* global&local colortable bgindex */ 125 int npixels; /* width*height of current image */ 126 int ncontrol; /* #controlgif calls before putgif */ 127 int isanimated, /* true if animategif() called */ 128 delay, tcolor, disposal; /* animation frame defaults */ 129 /* --- 130 * plaintext control data 131 * ------------------------- */ 132 int isplaintext; /* plaintext flag, 1or2 if present */ 133 int pt_left, pt_top; /* col,row of top-left corner */ 134 int pt_bg, pt_fg; /* bg,fg colortable indexes */ 135 int pt_width, pt_height; /* width,height of pixelized data */ 136 char pt_data[1024]; /* local copy of user text data */ 137 BYTE *pt_pixels; /* pixelized data (kept if flag=2) */ 138 /* --- 139 * debugging control data 140 * ------------------------- */ 141 int msglevel; /* desired verbosity (0=none) */ 142 void *msgfp; /* debug file ptr */ 143 }; /* --- end-of-struct GIFSAVE89 --- */ 144 145 146 void *newgif(void **gifimage, int width, int height, int *colors, int bgindex); 147 int controlgif(GS * gs, int tcolor, int delay, int userinput, int disposal); 148 int animategif(GS * gs, int nrepetitions, int delay, int tcolor, int disposal); 149 int putgif(GS * gs, void *pixels); 150 int endgif(GS * gs); 151