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;
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 si = gif->SavedImages[0];
251 w = si.ImageDesc.Width;
252 h = si.ImageDesc.Height;
253 if (w <= 0 || h <= 0) {
254 DGifCloseFile(gif, &giferr);
255 return (
PIX *)ERROR_PTR(
"invalid image dimensions", procName, NULL);
258 if (si.RasterBits == NULL) {
259 DGifCloseFile(gif, &giferr);
260 return (
PIX *)ERROR_PTR(
"no raster data in GIF", procName, NULL);
263 if (si.ImageDesc.ColorMap) {
265 gif_cmap = si.ImageDesc.ColorMap;
266 }
else if (gif->SColorMap) {
268 gif_cmap = gif->SColorMap;
271 DGifCloseFile(gif, &giferr);
272 return (
PIX *)ERROR_PTR(
"color map is missing", procName, NULL);
275 ncolors = gif_cmap->ColorCount;
278 else if (ncolors <= 4)
280 else if (ncolors <= 16)
285 DGifCloseFile(gif, &giferr);
286 return (
PIX *)ERROR_PTR(
"cmap creation failed", procName, NULL);
289 for (cindex = 0; cindex < ncolors; cindex++) {
290 rval = gif_cmap->Colors[cindex].Red;
291 gval = gif_cmap->Colors[cindex].Green;
292 bval = gif_cmap->Colors[cindex].Blue;
296 if ((pixd =
pixCreate(w, h, d)) == NULL) {
297 DGifCloseFile(gif, &giferr);
299 return (
PIX *)ERROR_PTR(
"failed to allocate pixd", procName, NULL);
301 pixSetInputFormat(pixd, IFF_GIF);
304 wpl = pixGetWpl(pixd);
306 for (i = 0; i < h; i++) {
307 line = data + i * wpl;
309 for (j = 0; j < w; j++) {
310 if (si.RasterBits[i * w + j])
314 for (j = 0; j < w; j++)
317 for (j = 0; j < w; j++)
320 for (j = 0; j < w; j++)
334 DGifCloseFile(gif, &giferr);
357 pixWriteStreamGif(FILE *fp,
361 size_t filebytes, nbytes;
363 PROCNAME(
"pixWriteStreamGif");
366 return ERROR_INT(
"stream not open", procName, 1);
368 return ERROR_INT(
"pix not defined", procName, 1);
371 if (pixWriteMemGif(&filedata, &filebytes, pix) != 0) {
373 return ERROR_INT(
"failure to gif encode pix", procName, 1);
377 nbytes = fwrite(filedata, 1, filebytes, fp);
379 if (nbytes != filebytes)
380 return ERROR_INT(
"write error", procName, 1);
399 pixWriteMemGif(l_uint8 **pdata,
408 PROCNAME(
"pixWriteMemGif");
411 #if (GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0)) 412 L_ERROR(
"Require giflib-5.1 or later\n", procName);
415 #if GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 1 && GIFLIB_RELEASE == 2 416 L_ERROR(
"Can't use giflib-5.1.2; suggest 5.1.3 or later\n", procName);
421 return ERROR_INT(
"&data not defined", procName, 1 );
424 return ERROR_INT(
"&size not defined", procName, 1 );
427 return ERROR_INT(
"&pix not defined", procName, 1 );
430 return ERROR_INT(
"failed to create buffer", procName, 1);
432 if ((gif = EGifOpen((
void*)buffer, gifWriteFunc, NULL)) == NULL) {
434 return ERROR_INT(
"failed to create GIF image handle", procName, 1);
437 result = pixToGif(pix, gif);
438 EGifCloseFile(gif, &giferr);
450 gifWriteFunc(GifFileType *gif,
451 const GifByteType *src,
452 l_int32 bytesToWrite)
456 PROCNAME(
"gifWriteFunc");
458 if ((buffer = (
L_BBUFFER*)gif->UserData) == NULL)
459 return ERROR_INT(
"UserData not set", procName, -1);
461 if(
bbufferRead(buffer, (l_uint8*)src, bytesToWrite) == 0)
486 l_int32 wpl, i, j, w, h, d, ncolor, rval, gval, bval;
487 l_int32 gif_ncolor = 0;
488 l_uint32 *data, *line;
491 ColorMapObject *gif_cmap;
492 GifByteType *gif_line;
494 PROCNAME(
"pixToGif");
497 return ERROR_INT(
"pix not defined", procName, 1);
499 return ERROR_INT(
"gif not defined", procName, 1);
501 d = pixGetDepth(pix);
508 if (!pixGetColormap(pixd)) {
517 return ERROR_INT(
"failed to convert image to indexed", procName, 1);
518 d = pixGetDepth(pixd);
520 if ((cmap = pixGetColormap(pixd)) == NULL) {
522 return ERROR_INT(
"cmap is missing", procName, 1);
527 for (i = 0; i <= 8; i++) {
528 if ((1 << i) >= ncolor) {
529 gif_ncolor = (1 << i);
533 if (gif_ncolor < 1) {
535 return ERROR_INT(
"number of colors is invalid", procName, 1);
539 if ((gif_cmap = GifMakeMapObject(gif_ncolor, NULL)) == NULL) {
541 return ERROR_INT(
"failed to create GIF color map", procName, 1);
543 for (i = 0; i < gif_ncolor; i++) {
544 rval = gval = bval = 0;
548 GifFreeMapObject(gif_cmap);
549 return ERROR_INT(
"failed to get color from color map",
554 gif_cmap->Colors[i].Red = rval;
555 gif_cmap->Colors[i].Green = gval;
556 gif_cmap->Colors[i].Blue = bval;
560 if (EGifPutScreenDesc(gif, w, h, gif_cmap->BitsPerPixel, 0, gif_cmap)
563 GifFreeMapObject(gif_cmap);
564 return ERROR_INT(
"failed to write screen description", procName, 1);
566 GifFreeMapObject(gif_cmap);
568 if (EGifPutImageDesc(gif, 0, 0, w, h, FALSE, NULL) != GIF_OK) {
570 return ERROR_INT(
"failed to image screen description", procName, 1);
574 wpl = pixGetWpl(pixd);
575 if (d != 1 && d != 2 && d != 4 && d != 8) {
577 return ERROR_INT(
"image depth is not in {1, 2, 4, 8}", procName, 1);
580 if ((gif_line = (GifByteType *)LEPT_CALLOC(
sizeof(GifByteType), w))
583 return ERROR_INT(
"mem alloc fail for data line", procName, 1);
586 for (i = 0; i < h; i++) {
587 line = data + i * wpl;
589 for (j = 0; j < w; j++) {
608 if (EGifPutLine(gif, gif_line, w) != GIF_OK) {
611 return ERROR_INT(
"failed to write data line into GIF", procName, 1);
620 if (EGifPutComment(gif, text) != GIF_OK)
621 L_WARNING(
"gif comment not written\n", procName);
639 static const l_int32 InterlacedOffset[] = {0, 4, 2, 1};
640 static const l_int32 InterlacedJumps[] = {8, 8, 4, 2};
643 pixUninterlaceGIF(
PIX *pixs)
645 l_int32 w, h, d, wpl, j, k, srow, drow;
646 l_uint32 *datas, *datad, *lines, *lined;
649 PROCNAME(
"pixUninterlaceGIF");
652 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
655 wpl = pixGetWpl(pixs);
659 for (k = 0, srow = 0; k < 4; k++) {
660 for (drow = InterlacedOffset[k]; drow < h;
661 drow += InterlacedJumps[k], srow++) {
662 lines = datas + srow * wpl;
663 lined = datad + drow * wpl;
664 for (j = 0; j < w; j++)
665 memcpy(lined, lines, 4 * wpl);
L_BBUFFER * bbufferCreate(const l_uint8 *indata, l_int32 nalloc)
bbufferCreate()
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)
PIX * pixCreateTemplate(PIX *pixs)
pixCreateTemplate()
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()
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(PIXCMAP *cmap)
pixcmapGetCount()
l_ok pixcmapAddColor(PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval)
pixcmapAddColor()
#define SET_DATA_BIT(pdata, n)