70 #include <config_auto.h> 74 #include "allheaders.h" 77 #if HAVE_LIBGIF || HAVE_LIBUNGIF 83 static PIX * gifToPix(GifFileType *gif);
85 static l_int32 pixToGif(
PIX *pix, GifFileType *gif);
88 typedef struct GifReadBuffer
96 static l_int32 gifReadFunc(GifFileType *gif, GifByteType *dest,
99 static l_int32 gifWriteFunc(GifFileType *gif,
const GifByteType *src,
100 l_int32 bytesToWrite);
113 pixReadStreamGif(FILE *fp)
119 PROCNAME(
"pixReadStreamGif");
122 return (
PIX *)ERROR_PTR(
"fp not defined", procName, NULL);
127 return (
PIX *)ERROR_PTR(
"filedata not read", procName, NULL);
130 pix = pixReadMemGif(filedata, filesize);
133 L_ERROR(
"failed to read gif from file data\n", procName);
156 pixReadMemGif(
const l_uint8 *cdata,
160 GifReadBuffer buffer;
162 PROCNAME(
"pixReadMemGif");
165 #if (GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0)) 166 L_ERROR(
"Require giflib-5.1 or later\n", procName);
169 #if GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 1 && GIFLIB_RELEASE == 2 170 L_ERROR(
"Can't use giflib-5.1.2; suggest 5.1.3 or later\n", procName);
175 return (
PIX *)ERROR_PTR(
"cdata not defined", procName, NULL);
177 buffer.cdata = cdata;
180 if ((gif = DGifOpen((
void*)&buffer, gifReadFunc, NULL)) == NULL)
181 return (
PIX *)ERROR_PTR(
"could not open gif stream from memory",
184 return gifToPix(gif);
189 gifReadFunc(GifFileType *gif,
193 GifReadBuffer *buffer;
196 PROCNAME(
"gifReadFunc");
198 if ((buffer = (GifReadBuffer*)gif->UserData) == NULL)
199 return ERROR_INT(
"UserData not set", procName, -1);
201 if(buffer->pos >= buffer->size || bytesToRead > buffer->size)
204 bytesRead = (buffer->pos < buffer->size - bytesToRead)
205 ? bytesToRead : buffer->size - buffer->pos;
206 memcpy(dest, buffer->cdata + buffer->pos, bytesRead);
207 buffer->pos += bytesRead;
226 gifToPix(GifFileType *gif)
228 l_int32 wpl, i, j, w, h, d, cindex, ncolors, valid, nimages;
229 l_int32 rval, gval, bval;
230 l_uint32 *data, *line;
233 ColorMapObject *gif_cmap;
237 PROCNAME(
"gifToPix");
240 if (DGifSlurp(gif) != GIF_OK) {
241 DGifCloseFile(gif, &giferr);
242 return (
PIX *)ERROR_PTR(
"failed to read GIF data", procName, NULL);
245 if (gif->SavedImages == NULL) {
246 DGifCloseFile(gif, &giferr);
247 return (
PIX *)ERROR_PTR(
"no images found in GIF", procName, NULL);
250 nimages = gif->ImageCount;
252 L_WARNING(
"There are %d images in the file; we only read the first\n",
255 si = gif->SavedImages[0];
256 w = si.ImageDesc.Width;
257 h = si.ImageDesc.Height;
258 if (w <= 0 || h <= 0) {
259 DGifCloseFile(gif, &giferr);
260 return (
PIX *)ERROR_PTR(
"invalid image dimensions", procName, NULL);
263 if (si.RasterBits == NULL) {
264 DGifCloseFile(gif, &giferr);
265 return (
PIX *)ERROR_PTR(
"no raster data in GIF", procName, NULL);
268 if (si.ImageDesc.ColorMap) {
270 gif_cmap = si.ImageDesc.ColorMap;
271 }
else if (gif->SColorMap) {
273 gif_cmap = gif->SColorMap;
276 DGifCloseFile(gif, &giferr);
277 return (
PIX *)ERROR_PTR(
"color map is missing", procName, NULL);
280 ncolors = gif_cmap->ColorCount;
281 if (ncolors <= 0 || ncolors > 256) {
282 DGifCloseFile(gif, &giferr);
283 return (
PIX *)ERROR_PTR(
"ncolors is invalid", procName, NULL);
287 else if (ncolors <= 4)
289 else if (ncolors <= 16)
294 for (cindex = 0; cindex < ncolors; cindex++) {
295 rval = gif_cmap->Colors[cindex].Red;
296 gval = gif_cmap->Colors[cindex].Green;
297 bval = gif_cmap->Colors[cindex].Blue;
301 if ((pixd =
pixCreate(w, h, d)) == NULL) {
302 DGifCloseFile(gif, &giferr);
304 return (
PIX *)ERROR_PTR(
"failed to allocate pixd", procName, NULL);
306 pixSetInputFormat(pixd, IFF_GIF);
310 DGifCloseFile(gif, &giferr);
313 return (
PIX *)ERROR_PTR(
"colormap is invalid", procName, NULL);
316 wpl = pixGetWpl(pixd);
318 for (i = 0; i < h; i++) {
319 line = data + i * wpl;
321 for (j = 0; j < w; j++) {
322 if (si.RasterBits[i * w + j])
326 for (j = 0; j < w; j++)
329 for (j = 0; j < w; j++)
332 for (j = 0; j < w; j++)
346 DGifCloseFile(gif, &giferr);
369 pixWriteStreamGif(FILE *fp,
373 size_t filebytes, nbytes;
375 PROCNAME(
"pixWriteStreamGif");
378 return ERROR_INT(
"stream not open", procName, 1);
380 return ERROR_INT(
"pix not defined", procName, 1);
383 if (pixWriteMemGif(&filedata, &filebytes, pix) != 0) {
385 return ERROR_INT(
"failure to gif encode pix", procName, 1);
389 nbytes = fwrite(filedata, 1, filebytes, fp);
391 if (nbytes != filebytes)
392 return ERROR_INT(
"write error", procName, 1);
411 pixWriteMemGif(l_uint8 **pdata,
420 PROCNAME(
"pixWriteMemGif");
423 #if (GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0)) 424 L_ERROR(
"Require giflib-5.1 or later\n", procName);
427 #if GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 1 && GIFLIB_RELEASE == 2 428 L_ERROR(
"Can't use giflib-5.1.2; suggest 5.1.3 or later\n", procName);
433 return ERROR_INT(
"&data not defined", procName, 1 );
436 return ERROR_INT(
"&size not defined", procName, 1 );
439 return ERROR_INT(
"&pix not defined", procName, 1 );
442 return ERROR_INT(
"failed to create buffer", procName, 1);
444 if ((gif = EGifOpen((
void*)buffer, gifWriteFunc, NULL)) == NULL) {
446 return ERROR_INT(
"failed to create GIF image handle", procName, 1);
449 result = pixToGif(pix, gif);
450 EGifCloseFile(gif, &giferr);
462 gifWriteFunc(GifFileType *gif,
463 const GifByteType *src,
464 l_int32 bytesToWrite)
468 PROCNAME(
"gifWriteFunc");
470 if ((buffer = (
L_BBUFFER*)gif->UserData) == NULL)
471 return ERROR_INT(
"UserData not set", procName, -1);
473 if(
bbufferRead(buffer, (l_uint8*)src, bytesToWrite) == 0)
498 l_int32 wpl, i, j, w, h, d, ncolor, rval, gval, bval, valid;
499 l_int32 gif_ncolor = 0;
500 l_uint32 *data, *line;
503 ColorMapObject *gif_cmap;
504 GifByteType *gif_line;
506 PROCNAME(
"pixToGif");
509 return ERROR_INT(
"pix not defined", procName, 1);
511 return ERROR_INT(
"gif not defined", procName, 1);
513 d = pixGetDepth(pix);
520 if (!pixGetColormap(pixd)) {
529 return ERROR_INT(
"failed to convert to colormapped pix", procName, 1);
530 d = pixGetDepth(pixd);
531 cmap = pixGetColormap(pixd);
534 return ERROR_INT(
"cmap is missing", procName, 1);
539 return ERROR_INT(
"colormap is not valid", procName, 1);
544 for (i = 0; i <= 8; i++) {
545 if ((1 << i) >= ncolor) {
546 gif_ncolor = (1 << i);
550 if (gif_ncolor < 1) {
552 return ERROR_INT(
"number of colors is invalid", procName, 1);
556 if ((gif_cmap = GifMakeMapObject(gif_ncolor, NULL)) == NULL) {
558 return ERROR_INT(
"failed to create GIF color map", procName, 1);
560 for (i = 0; i < gif_ncolor; i++) {
561 rval = gval = bval = 0;
565 GifFreeMapObject(gif_cmap);
566 return ERROR_INT(
"failed to get color from color map",
571 gif_cmap->Colors[i].Red = rval;
572 gif_cmap->Colors[i].Green = gval;
573 gif_cmap->Colors[i].Blue = bval;
577 if (EGifPutScreenDesc(gif, w, h, gif_cmap->BitsPerPixel, 0, gif_cmap)
580 GifFreeMapObject(gif_cmap);
581 return ERROR_INT(
"failed to write screen description", procName, 1);
583 GifFreeMapObject(gif_cmap);
585 if (EGifPutImageDesc(gif, 0, 0, w, h, FALSE, NULL) != GIF_OK) {
587 return ERROR_INT(
"failed to image screen description", procName, 1);
591 wpl = pixGetWpl(pixd);
592 if (d != 1 && d != 2 && d != 4 && d != 8) {
594 return ERROR_INT(
"image depth is not in {1, 2, 4, 8}", procName, 1);
597 if ((gif_line = (GifByteType *)LEPT_CALLOC(
sizeof(GifByteType), w))
600 return ERROR_INT(
"mem alloc fail for data line", procName, 1);
603 for (i = 0; i < h; i++) {
604 line = data + i * wpl;
606 for (j = 0; j < w; j++) {
625 if (EGifPutLine(gif, gif_line, w) != GIF_OK) {
628 return ERROR_INT(
"failed to write data line into GIF", procName, 1);
637 if (EGifPutComment(gif, text) != GIF_OK)
638 L_WARNING(
"gif comment not written\n", procName);
656 static const l_int32 InterlacedOffset[] = {0, 4, 2, 1};
657 static const l_int32 InterlacedJumps[] = {8, 8, 4, 2};
660 pixUninterlaceGIF(
PIX *pixs)
662 l_int32 w, h, d, wpl, j, k, srow, drow;
663 l_uint32 *datas, *datad, *lines, *lined;
666 PROCNAME(
"pixUninterlaceGIF");
669 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
672 wpl = pixGetWpl(pixs);
676 for (k = 0, srow = 0; k < 4; k++) {
677 for (drow = InterlacedOffset[k]; drow < h;
678 drow += InterlacedJumps[k], srow++) {
679 lines = datas + srow * wpl;
680 lined = datad + drow * wpl;
681 for (j = 0; j < w; j++)
682 memcpy(lined, lines, 4 * wpl);
L_BBUFFER * bbufferCreate(const l_uint8 *indata, l_int32 nalloc)
bbufferCreate()
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
#define SET_DATA_QBIT(pdata, n, val)
void pixcmapDestroy(PIXCMAP **pcmap)
pixcmapDestroy()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
#define GET_DATA_BIT(pdata, n)
l_ok pixSetColormap(PIX *pix, PIXCMAP *colormap)
pixSetColormap()
l_uint8 * bbufferDestroyAndSaveData(L_BBUFFER **pbb, size_t *pnbytes)
bbufferDestroyAndSaveData()
#define SET_DATA_DIBIT(pdata, n, val)
PIXCMAP * pixcmapCreate(l_int32 depth)
pixcmapCreate()
l_ok pixcmapGetColor(PIXCMAP *cmap, l_int32 index, l_int32 *prval, l_int32 *pgval, l_int32 *pbval)
pixcmapGetColor()
#define SET_DATA_BYTE(pdata, n, val)
l_ok pixSetPadBits(PIX *pix, l_int32 val)
pixSetPadBits()
#define GET_DATA_QBIT(pdata, n)
#define GET_DATA_BYTE(pdata, n)
PIX * pixClone(PIX *pixs)
pixClone()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixcmapIsValid(const PIXCMAP *cmap, PIX *pix, l_int32 *pvalid)
pixcmapIsValid()
void bbufferDestroy(L_BBUFFER **pbb)
bbufferDestroy()
l_ok bbufferRead(L_BBUFFER *bb, l_uint8 *src, l_int32 nbytes)
bbufferRead()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
char * pixGetText(PIX *pix)
pixGetText()
#define GET_DATA_DIBIT(pdata, n)
PIX * pixConvertRGBToColormap(PIX *pixs, l_int32 ditherflag)
pixConvertRGBToColormap()
l_int32 pixcmapGetCount(const PIXCMAP *cmap)
pixcmapGetCount()
l_ok pixcmapAddColor(PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval)
pixcmapAddColor()
#define SET_DATA_BIT(pdata, n)