BOOL CReadBitmap::GIFRead() /************************************************************************/ { GIFHDR hdr; GIFDESC ImDesc; GIFMAP GlobalMap; GIFMAP LocalMap; BYTE cTemp; LPTR lpFileLine, lpLineBuffer, lpOut; LPFRAME lpFrame; FILEBUF ifd; /* file descriptor (buffered) */ BOOL graymap; int i; int sy; int xres; /* pixels per inch */ int npix; /* image width (pixels) */ int nlin; /* image height (pixels) */ BYTE codeSize; int iCodeSize; int iRowMapIndex; BOOL compressInit; LPLZW_STUFF lpLZW; FRMTYPEINFO inType, outType; LPCOLORMAP lpColorMap; CFrameTypeConvert TypeConvert; CFile theFile; CFile* pTheFile; BOOL fRet = FALSE; ProgressBegin(1); if ((pTheFile = OpenFile()) == NULL) { ProgressEnd(); return(FALSE); } TRY { lpFileLine = NULL; lpFrame = NULL; lpLineBuffer = NULL; compressInit = NO; lpColorMap = NULL; if (!(lpLineBuffer = Alloc (BUF_SIZE))) goto Exit; FileFDOpenRdr (&ifd, pTheFile, lpLineBuffer, BUF_SIZE); /* initialize the Global and local color maps */ gifInitColorMap (&GlobalMap); gifInitColorMap (&LocalMap); /* read gif file header */ if (gifReadHeader (&ifd, &hdr)) goto BadRead; /* get global color map, if any */ if (hdr.GlobalMap) { if (gifReadColorMap (&ifd, hdr.bpp, &GlobalMap)) goto BadRead; } /* look for start of image */ while (1) { FileFDRead (&ifd, (LPTR) &cTemp, 1); if (ifd.err) goto BadRead; /* test for image separator character */ if (cTemp == GIFImSep) break; /* test for terminator character (no image blocks in file?) */ if (cTemp == GIFImSep) goto BadRead; /* test for extension block character */ if (cTemp == GIFExtBlk) { /* Skip over the extension block */ /* read function code */ FileFDRead (&ifd, (LPTR) &cTemp, 1); do { /* read byte count */ FileFDRead (&ifd, (LPTR) &cTemp, 1); /* skip data bytes */ if (cTemp) FileFDSeek (&ifd, (long) cTemp, 1); } while (cTemp); } } /* now at the start of the first image */ if (gifReadImDesc (&ifd, &ImDesc)) goto BadRead; /* read local color map, if any */ if (ImDesc.LocalMap) { if (gifReadColorMap (&ifd, ImDesc.bpp, &LocalMap)) goto BadRead; } else { LocalMap = GlobalMap; ImDesc.bpp = hdr.bpp; } /* check for gray map */ graymap = TRUE; for (i = 0; (i < LocalMap.Length) && graymap; i++) graymap = (LocalMap.Map[i].red == LocalMap.Map[i].green) && (LocalMap.Map[i].red == LocalMap.Map[i].blue); lpColorMap = FrameCreateColorMap(); if (!lpColorMap) { SetError(BEC_errMemory); goto Exit; } lpColorMap->NumEntries = LocalMap.Length; for (i = 0; i < LocalMap.Length; ++i) lpColorMap->RGBData[i] = LocalMap.Map[i]; /* get width of image in pixels */ npix = ImDesc.ImWidth; nlin = ImDesc.ImHeight; xres = 75; if (hdr.bpp == 1) FrameSetTypeInfo(&inType, FDT_LINEART); else FrameSetTypeInfo(&inType, FDT_PALETTECOLOR, lpColorMap); if (!SetupTypes(&inType, &outType, graymap)) goto Exit; FrameSetTypeInfo(&inType, FDT_PALETTECOLOR, lpColorMap); if (!TypeConvert.Init(inType, outType, npix, m_DitherType)) { SetError(BEC_errMemory); goto Exit; } /* allocate space for one line of the image (file) */ if ( !AllocLines (&lpFileLine, 1, npix, 1)) { SetError(BEC_errMemory); goto BadWrite; } /* Create the image frame store */ lpFrame = FrameOpen(outType, npix, nlin, xres); if ( !lpFrame ) { SetError(BEC_errFrameOpen); goto Exit; } /* convert the image */ if (FileFDRead (&ifd, &codeSize, 1) == -1) goto BadRead; iCodeSize = codeSize; if (FileFDSeek (&ifd, 0L, 1) == -1) goto BadRead; if ( !( lpLZW = DecompressLZW_GIF (ifd.pFile, NULL, 0, iCodeSize, NULL))) goto BadRead; compressInit = YES; if (ImDesc.Interlaced) iRowMapIndex = 1; else iRowMapIndex = 0; sy = gifRowMap [iRowMapIndex].first; for (i = 0; i < nlin; i++) { if (Progress (i, nlin, YES)) goto Exit; if (!(DecompressLZW_GIF (ifd.pFile, lpFileLine, npix, iCodeSize, lpLZW))) goto BadRead; if ( !(lpOut = FramePointerRaw(lpFrame, 0, sy, YES)) ) goto BadWrite; TypeConvert.ConvertData(lpFileLine, lpOut, sy, npix); sy += gifRowMap [iRowMapIndex].step; if (sy >= ImDesc.ImHeight) { iRowMapIndex++; sy = gifRowMap [iRowMapIndex].first; } } m_iWidth = npix; m_iHeight = nlin; m_iRes = xres; m_lpFrame = lpFrame; fRet = TRUE; goto Exit; } CATCH_ALL(e) { goto BadRead; } END_CATCH_ALL ProgressEnd(); return (TRUE); BadRead: SetError(BEC_errFileRead); goto Exit; BadWrite: SetError(BEC_errFrameRead); Exit: if (compressInit) DecompressLZW_GIF (ifd.pFile, NULL, 0, iCodeSize, lpLZW); compressInit = NO; if ( lpFileLine ) FreeUp( lpFileLine ); if ( lpLineBuffer ) FreeUp (lpLineBuffer); if ( lpColorMap ) FrameDestroyColorMap(lpColorMap); CloseFile(pTheFile); if (!fRet && lpFrame) FrameClose(lpFrame); ProgressEnd(); return(fRet); }
BOOL CReadBitmap::CTRead() { LPCTHEADER lpTH; LPFRAME lpFrame; BOOL fConvert = FALSE, fRet = FALSE; CFile theFile; CFile* pTheFile; FRMTYPEINFO inType, outType; LPTR lpIn, lpIn2,lpOut, lpFrmLine; int x,y,j; UINT bytes_read; BOOL GrayScale; BITMAP_ERROR_CODE err; CFrameTypeConvert Convert; ProgressBegin(1); if ((pTheFile = OpenFile()) == NULL) { ProgressEnd(); return(FALSE); } lpFrame = NULL; if ( !(lpTH = ReadCTHeader(pTheFile, m_lpFileName, &err))) { SetError(err); goto Exit; } GrayScale = lpTH->ColSepMask == 8 && lpTH->NumColSep == 1; if (GrayScale) FrameSetTypeInfo(&inType, FDT_GRAYSCALE, NULL); else FrameSetTypeInfo(&inType, FDT_CMYKCOLOR, NULL); if (!SetupTypes(&inType, &outType)) goto Exit; fConvert = !FrameTypeInfoEqual( inType, outType ); if( fConvert ) { if( !Convert.Init( inType, outType, lpTH->Pixels ) ) { SetError(BEC_errMemory); goto Exit; } } /* Create the image frame store */ if ( !(lpFrame = FrameOpen(outType, lpTH->Pixels, lpTH->Lines, lpTH->Resolution))) { SetError(BEC_errFrameOpen); goto Exit; } pTheFile->Seek (2048, CFile::begin); for (y=0; y < lpTH->Lines; y++) { if ( Progress(y, lpTH->Lines, YES)) goto Exit; TRY { bytes_read = pTheFile->Read(lpTH->lpCTLine, lpTH->LineSize); } CATCH_ALL(e) { goto BadRead; } END_CATCH_ALL if (bytes_read != lpTH->LineSize) goto BadRead; if ( !(lpFrmLine = FramePointerRaw( lpFrame, 0, y, YES))) goto BadWrite; lpIn = lpTH->lpCTLine; lpOut = lpFrmLine; lpIn2 = lpIn; for (x=0; x < lpTH->Pixels; x++) { j = 0; if (!GrayScale) { if (lpTH->ColSepMask & 0x1) // cyan { j += lpTH->ChannelSize; *lpOut++ = 255 - *lpIn2; } else *lpOut++ = 0; if (lpTH->ColSepMask & 0x2) // magenta { *lpOut++ = 255 - *(lpIn2 + j); j += lpTH->ChannelSize; } else *lpOut++ = 0; if (lpTH->ColSepMask & 0x4) // yellow { *lpOut++ = 255 - *(lpIn2 + j); j += lpTH->ChannelSize; } else *lpOut++ = 0; } if (lpTH->ColSepMask & 0x8) // black *lpOut++ = 255 - *(lpIn2 + j); else *lpOut++ = 0; lpIn2++; } if( fConvert ) Convert.ConvertData( lpFrmLine, lpFrmLine, y, lpTH->Pixels ); } m_iWidth = lpTH->Pixels; m_iHeight = lpTH->Lines; m_iRes = (int)lpTH->Resolution; m_lpFrame = lpFrame; fRet = TRUE; goto Exit; BadRead: SetError(BEC_errFileRead); goto Exit; BadWrite: SetError(BEC_errFrameRead); Exit: FreeCTHeader(lpTH); CloseFile(pTheFile); if (!fRet && lpFrame) FrameClose(lpFrame); ProgressEnd(); return( fRet ); }