LOCAL LPFRAME ConvertFrame(LPFRAME lpSrcFrame, FRMTYPEINFO OutTypeInfo, CFrameTypeConvert *pTypeConvert) /***********************************************************************/ { int y, Xsize, Ysize; LPFRAME lpDstFrame; FRMTYPEINFO InTypeInfo; LPTR lpSrc, lpDst; CFrameTypeConvert TypeConvert; Xsize = FrameXSize(lpSrcFrame); Ysize = FrameYSize(lpSrcFrame); FrameGetTypeInfo(lpSrcFrame, &InTypeInfo); if (!pTypeConvert) { if (!TypeConvert.Init(InTypeInfo, OutTypeInfo, Xsize)) { Message(IDS_EMEMALLOC); return(NULL); } pTypeConvert = &TypeConvert; } lpDstFrame = FrameOpen(OutTypeInfo, Xsize, Ysize, FrameResolution(lpSrcFrame)); if (!lpDstFrame) { FrameError(IDS_EPROCESSOPEN); return(NULL); } ProgressBegin(1, 0); for (y = 0; y < Ysize; ++y) { if (AstralClockCursor(y, Ysize, YES)) { FrameClose(lpDstFrame); goto ExitError; } if (!(lpSrc = FramePointerRaw(lpSrcFrame, 0, y, NO))) continue; if (!(lpDst = FramePointerRaw(lpDstFrame, 0, y, YES))) continue; pTypeConvert->ConvertData(lpSrc, lpDst, y, Xsize); } ProgressEnd(); return(lpDstFrame); ExitError: ProgressEnd(); return(NULL); }
LPTR EXPORT grabber_frame_ptr( int Depth, int x, int y, BOOL bModify ) /************************************************************************/ { if (Depth == -1) return(FramePointerRaw(frame_set(NULL), x, y, bModify)); else return(FramePointer(frame_set(NULL), x, y, bModify)); }
/************************************************************************** Function: StoreImageRect **************************************************************************/ void StoreImageRect( LPFRAME lpFrame, TW_IMAGEINFO imageInfo, TW_IMAGEMEMXFER imageData, FRMDATATYPE Type, int expand ) { register int i, j; BYTE Lut4to8[] = { 0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255 }; LPTR pSrc, pDst; pSrc = ( LPTR )imageData.Memory.TheMem; if( imageData.BytesWritten <= 0 ) return; for( i = 0; i < imageData.Rows; i++ ) { pDst = FramePointerRaw( lpFrame, ( int )( 0 ), ( int )( i+imageData.YOffset ), ( BOOL )( YES )); switch( Type ) { case FDT_LINEART : // Bi-Level Data pDst += ( imageData.XOffset + 7 ) / 8; j = imageData.BytesPerRow; if( fInvert ) { while( j-- ) { *pDst++ = ( *pSrc++ ) ^ 0xFF; } } else { copy( pSrc, pDst,( int )( imageData.BytesPerRow )); pSrc += imageData.BytesPerRow; } break; case FDT_PALETTECOLOR : // Palette Color Data case FDT_GRAYSCALE : // Gray Scale Data if( expand ) { pDst += imageData.XOffset; /* la2con( pSrc,( int )( imageData.Columns ), pDst, ( BOOL )(( twainPixelFlavor == TWPF_CHOCOLATE ) ? YES : NO )); */ la2con( pSrc,( int )( imageData.Columns ), pDst, ( BOOL )( fInvert ? NO : YES )); pSrc += imageData.BytesPerRow; } else { switch( imageInfo.BitsPerSample[ 0 ] ) { case 4 : pDst += ( imageData.XOffset ); j = imageData.BytesPerRow; if( Type == FDT_PALETTECOLOR ) { // if the image is inverted, color tables are // inverted. The image data is NOT inverted. while( j-- ) { *pDst++ = (( *pSrc ) & 0xF0 ) >> 4; *pDst++ = ( *pSrc++ ) & 0x0F; } } else { if( fInvert ) { while( j-- ) { *pDst++ = ( Lut4to8[(( *pSrc ) & 0xF0 ) >> 4 ] ) ^ 0xFF; *pDst++ = ( Lut4to8[( *pSrc++ ) & 0x0F ] ) ^ 0xFF; } } else { while( j-- ) { *pDst++ = Lut4to8[(( *pSrc ) & 0xF0 ) >> 4 ]; *pDst++ = Lut4to8[( *pSrc++ ) & 0x0F ]; } } } break; case 8 : pDst += ( imageData.XOffset ); // if the image is inverted, color tables are // inverted. The image data is NOT inverted. if(( Type == FDT_GRAYSCALE ) && fInvert ) not( pSrc, pDst,( int )( imageData.BytesPerRow )); else copy( pSrc, pDst,( int )( imageData.BytesPerRow )); pSrc += imageData.BytesPerRow; break; } } break; case FDT_RGBCOLOR : pDst += ( imageData.XOffset * 3 ); if( imageInfo.Planar ) { j = imageData.BytesPerRow; switch( ImagePlane ) { case 1 : // Red Plane break; case 2 : // Green Plane pDst++; break; case 3 : // Blue Plane pDst += 2; break; } if( fInvert ) { while( j-- ) { *pDst = *pSrc ^ 0xFF; *pSrc++; pDst += 3; } } else { while( j-- ) { *pDst = *pSrc++; pDst += 3; } } } else { if( fInvert ) { not( pSrc, pDst,( int )( imageData.BytesPerRow )); pSrc += imageData.BytesPerRow; } else { copy( pSrc, pDst,( int )( imageData.BytesPerRow )); pSrc += imageData.BytesPerRow; } } break; }
static LPTR FrameToLineArtDIB( LPFRAME lpFrame, LPRECT lpRect ) { int y, bpl, fbpl, i; LPTR lpDIBMem; HPTR lpDst; HPTR lpDstLine; LPTR lpSrc; DWORD lCount, lImageSize, lInfoSize; int iCount, dx, dy; LPBITMAPINFO lpInfo; if ( !lpFrame ) return( NULL ); dx = (lpRect->right - lpRect->left) + 1; dy = (lpRect->bottom - lpRect->top) + 1; bpl = (dx+7)/8; lInfoSize = sizeof(BITMAPINFOHEADER) + 2*sizeof(RGBQUAD); bpl = 4 * ((bpl + 3)/4); // DIB packing lImageSize = (long)bpl * dy; lCount = lInfoSize + lImageSize; // Allocate the memory to hold the DIB if ( !(lpDIBMem = Alloc( lCount )) ) { FrameSetError( ERR_MALLOC ); return( NULL ); } lpInfo = (LPBITMAPINFO)lpDIBMem; lpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); lpInfo->bmiHeader.biWidth = dx; lpInfo->bmiHeader.biHeight = dy; lpInfo->bmiHeader.biPlanes = 1; lpInfo->bmiHeader.biCompression = BI_RGB; lpInfo->bmiHeader.biBitCount = 1; lpInfo->bmiHeader.biSizeImage = lImageSize; lpInfo->bmiHeader.biXPelsPerMeter = (10000L * (long) FrameResolution(lpFrame)) / 254L; lpInfo->bmiHeader.biYPelsPerMeter = (10000L * (long) FrameResolution(lpFrame)) / 254L; lpInfo->bmiHeader.biClrUsed = 2; lpInfo->bmiHeader.biClrImportant = 2; for (i = 0; i < 2; i++) { lpInfo->bmiColors[i].rgbBlue = (i&1) ? 0xFF : 0x00; lpInfo->bmiColors[i].rgbGreen = (i&1) ? 0xFF : 0x00; lpInfo->bmiColors[i].rgbRed = (i&1) ? 0xFF : 0x00; lpInfo->bmiColors[i].rgbReserved = 0; } lpDstLine = (HPTR)lpDIBMem; lpDstLine += lInfoSize; fbpl = FrameByteWidth( lpFrame ); // This goes backwards... for (y = lpRect->bottom; y >= lpRect->top; y--) { lpDst = lpDstLine; lpSrc = FramePointerRaw(lpFrame, lpRect->left, y, NO); if (lpSrc) { if (bpl > fbpl) { iCount = fbpl; while (iCount-- > 0) *lpDst++ = *lpSrc++; iCount = bpl-fbpl; while (iCount-- > 0) *lpDst++ = 0; } else { iCount = bpl; while (iCount-- > 0) *lpDst++ = *lpSrc++; } } lpDstLine += bpl; } return( lpDIBMem ); }
LPFRAME DIBToLineArtFrame( LPTR lpDIBMem, BOOL bForceRGB ) { LPBITMAPINFO lpInfo; LPFRAME lpFrame; HPTR lpSrc; HPTR lpSrcLine; LPTR lpDst; int iResolution; int iFrameXSize; int iFrameYSize; int iPixelFlavor; int iSrcWidth; int iDstWidth; int iCount; int y; lpInfo = (LPBITMAPINFO)lpDIBMem; // Get the important bitmap information iFrameXSize = lpInfo->bmiHeader.biWidth; iFrameYSize = lpInfo->bmiHeader.biHeight; iResolution = ((lpInfo->bmiHeader.biXPelsPerMeter) * 254L) / 10000L; // Make sure that we have something to do if ((iFrameXSize <= 0) || (iFrameYSize <= 0)) { return(NULL); } // Validate the image resolution if (iResolution <= 0) { iResolution = 75; } // Find out whether or not we need to invert iPixelFlavor = (lpInfo->bmiColors[0].rgbBlue == 0); // Create the new image frame lpFrame = FrameOpen(FDT_LINEART, iFrameXSize, iFrameYSize, iResolution); if (!lpFrame) return(NULL); // Find the DIB data pointer and width lpSrcLine = lpDIBMem + lpInfo->bmiHeader.biSize + lpInfo->bmiHeader.biClrUsed * (sizeof(RGBQUAD)); iSrcWidth = ((lpInfo->bmiHeader.biWidth+31)/32) * 4; iDstWidth = FrameByteWidth( lpFrame ); // Copy the DIB data into the Frame for(y=iFrameYSize-1;y >= 0;y--) { lpSrc = lpSrcLine; lpDst = FramePointerRaw( lpFrame, 0, y, TRUE ); iCount = iDstWidth; if (iPixelFlavor) { while(iCount-- > 0) { *lpDst++ = *lpSrc++; } } else { while(iCount-- > 0) { *lpDst++ = (*lpSrc++)^0xFF; } } lpSrcLine += iSrcWidth; } return(lpFrame); }
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 ); }