BOOL CWriteBitmap::CTWrite() { RECT rSave; FRMTYPEINFO inType, outType; LPFRAME lpFrame; CFrameTypeConvert TypeConvert; LPOBJECT lpObject; CFile theFile; CFile* pTheFile = NULL; BOOL fRet = FALSE; LPTR lpTH, lp, lp2,lpBuffer, lpImgScanline; int i,x,j,y, LineSize, npix, nlin; double dbl; int NumOfChannels = 4; if (m_lpObjList) { lpObject = m_lpObjList->lpHead; if (lpObject) lpFrame = ObjGetEditFrame(lpObject); } else { lpFrame = m_lpFrame; lpObject = NULL; } ASSERT(lpFrame); if (m_fSrcArea) rSave = m_rSrcArea; else { rSave.top = rSave.left = 0; rSave.bottom = FrameYSize(lpFrame)-1; rSave.right = FrameXSize(lpFrame)-1; } npix = RectWidth(&rSave); nlin = RectHeight(&rSave); inType = FrameTypeInfo(lpFrame); // cause FramePointer never returns line art if (inType.DataType == FDT_LINEART) inType.DataType = FDT_GRAYSCALE; if (inType.DataType == FDT_GRAYSCALE) { NumOfChannels = 1; FrameSetTypeInfo(&outType, FDT_GRAYSCALE); } else FrameSetTypeInfo(&outType, FDT_CMYKCOLOR); // initialize stuff to do the type conversion ProgressBegin(1); if ((pTheFile = OpenFile()) == NULL) { ProgressEnd(); return(FALSE); } if (npix & 1) // odd pixels LineSize = npix + 1; else LineSize = npix; AllocLines(&lpBuffer, 1, max(LineSize*NumOfChannels, FrameByteWidth(lpFrame)), 1); AllocLines(&lpImgScanline, 1, max(LineSize*NumOfChannels, FrameByteWidth(lpFrame)), 1); if (!lpBuffer || !lpImgScanline) { SetError(BEC_errMemory); goto Exit; } if (!(lpTH = Alloc(1024))) { SetError(BEC_errMemory); return(NULL); } clr( (LPTR)lpTH, 1024); set(lpTH, 80, ' '); lstrcpy((LPSTR)lpTH, m_lpFileName); lpTH[lstrlen(m_lpFileName)] = ' '; lpTH[80] = 'C'; lpTH[81] = 'T'; pTheFile->Write(lpTH, 1024); // write first cluster clr( (LPTR)lpTH, 84); lpTH[0] = 1; // inches lpTH[1] = NumOfChannels; // if (NumOfChannels == 1) *((LPWORD)&lpTH[2]) = 0x0800; else *((LPWORD)&lpTH[2]) = 0x0F00; dbl = nlin / (double)lpFrame->Resolution; sprintf((LPSTR)&lpTH[4], "%+.7E",dbl); lpTH[6] = lpTH[5]; lpTH[5] = '.'; if (lpTH[15] == '+') lpTH[17] = lpTH[18] + 1; else { lpTH[17] = lpTH[18] - 1; lpTH[15] = '+'; } dbl = npix / (double)lpFrame->Resolution; sprintf((LPSTR)&lpTH[18], "%+.7E",dbl); lpTH[20] = lpTH[19]; lpTH[19] = '.'; if (lpTH[29] == '+') lpTH[31] = lpTH[32] + 1; else { lpTH[31] = lpTH[32] - 1; lpTH[29] = '+'; } sprintf((LPSTR)&lpTH[32], "%+12d",nlin); lpTH[32] = '+'; for (i=33; i < 44;i++) if (lpTH[i] == ' ' || lpTH[i] == '+') lpTH[i] = '0'; sprintf((LPSTR)&lpTH[44], "%+12d",npix); lpTH[44] = '+'; for (i=45; i < 56;i++) if (lpTH[i] == ' ' || lpTH[i] == '+') lpTH[i] = '0'; lpTH[56] = 0; // scan direction pTheFile->Write(lpTH, 1024); // write second cluster, Parameter Block TRY { if (!TypeConvert.Init(inType, outType, npix, DT_DEFAULT)) { SetError(BEC_errMemory); goto Exit; } for( y=rSave.top; y <= rSave.bottom; y++) { if (Progress( y-rSave.top, nlin, m_bEscapable )) goto Cancelled; if (lpObject) { if (!ObjGetLine( lpObject, rSave.left, y, (rSave.right - rSave.left) + 1, lpImgScanline)) goto BadRead; lp = lpImgScanline; } else { if ( !(lp = FramePointer( lpFrame, rSave.left, y, NO )) ) goto BadRead; } // convert the data to the new type TypeConvert.ConvertData(lp, lpBuffer, y, npix); lp2 = lpBuffer; for (x=0; x < npix; x++) for (j=0; j < NumOfChannels; j++) *(lpImgScanline + j * LineSize + x) = 255 - *lp2++; pTheFile->Write( lpImgScanline, LineSize*NumOfChannels); } fRet = TRUE; goto Exit; } CATCH_ALL(e) { goto BadWrite; } END_CATCH_ALL BadWrite: SetError(BEC_errWriteFile); goto Exit; BadRead: SetError(BEC_errFrameRead); Cancelled: Exit: CloseFile(pTheFile, fRet); if (lpTH) FreeUp (lpTH); if (lpBuffer) FreeUp(lpBuffer); if (lpImgScanline) FreeUp(lpImgScanline); ProgressEnd(); return( fRet ); }
BOOL CWriteBitmap::GIFWrite() { GIFHDR hdr; GIFDESC imDesc; GIFMAP ColorMap; int y, i, j, npix, nlin; LPTR lp, lpBuffer, lpImgScanline; BYTE cTerm; int codeSize; RECT rSave; BOOL bEscapable; BOOL compressInit; LPLZW_STUFF lpLZW; LPRGB lpRGBmap; LPFRAME lpFrame; LPOBJECT lpObject; FRMTYPEINFO inType, outType, inTypeLA, outTypeLA; CFrameTypeConvert TypeConvert, LineArtConvert; BOOL fRet = FALSE; CFile theFile; CFile* pTheFile = NULL; if (m_lpObjList) { lpObject = m_lpObjList->lpHead; if (lpObject) lpFrame = ObjGetEditFrame(lpObject); } else { lpFrame = m_lpFrame; lpObject = NULL; } ASSERT(lpFrame); ProgressBegin(1); if ((pTheFile = OpenFile()) == NULL) { ProgressEnd(); return(FALSE); } TRY { lpBuffer = NULL; lpImgScanline = NULL; compressInit = NO; /* full color not supported, force mini color */ if (m_DataType != FDT_GRAYSCALE && m_DataType != FDT_LINEART) m_DataType = FDT_PALETTECOLOR; if (m_fSrcArea) rSave = m_rSrcArea; else { rSave.top = rSave.left = 0; rSave.bottom = FrameYSize(lpFrame)-1; rSave.right = FrameXSize(lpFrame)-1; } inType = FrameTypeInfo(lpFrame); // cause FramePointer never returns line art if (inType.DataType == FDT_LINEART) inType.DataType = FDT_GRAYSCALE; // make sure colormap is inited for outType so we don't free bogus ptr FrameSetTypeInfo(&outType, FDT_NONE); npix = RectWidth(&rSave); nlin = RectHeight(&rSave); /* initialize file header */ hdr.ScnWidth = npix; hdr.ScnHeight = nlin; hdr.Background = 0; hdr.GlobalMap = TRUE; hdr.ColorRes = 8; /* initialize image descriptor */ imDesc.ImLeft = 0; imDesc.ImTop = 0; imDesc.ImWidth = npix; imDesc.ImHeight = nlin; imDesc.Zero = 0; /* determine type of image to create */ if ( m_DataType == FDT_GRAYSCALE ) { /* gray */ hdr.ColorRes = 8; hdr.bpp = 8; codeSize = 8; FrameSetTypeInfo(&outType, FDT_GRAYSCALE); } else if ( m_DataType == FDT_LINEART ) { /* line art or scatter */ hdr.ColorRes = 1; hdr.bpp = 1; codeSize = 2; FrameSetTypeInfo(&outType, FDT_LINEART); } else //if ( m_DataType == FDT_PALETTECOLOR ) { /* mini color */ hdr.ColorRes = 8; hdr.bpp = 8; codeSize = 8; if (inType.DataType == FDT_PALETTECOLOR) outType = inType; else FrameSetTypeInfo(&outType, FDT_PALETTECOLOR); } AllocLines( &lpBuffer, 1, npix, 4 ); // max size is cool AllocLines( &lpImgScanline, 1, npix, 4 ); if ( !lpBuffer || !lpImgScanline ) { SetError(BEC_errMemory); goto Exit; } /* set raw info byte */ hdr.RawImInfo = 0x80 | ((hdr.ColorRes-1) << 4) | (hdr.bpp-1); /* write file header */ pTheFile->Write((LPTR)"GIF87a", 6); pTheFile->Write((LPTR)&hdr.ScnWidth, 2); pTheFile->Write((LPTR)&hdr.ScnHeight, 2); pTheFile->Write((LPTR)&hdr.RawImInfo, 1); pTheFile->Write((LPTR)&hdr.Background, 1); pTheFile->Write((LPTR)"", 1); switch (m_DataType) { case FDT_LINEART: /* initialize the color map */ ColorMap.Length = 2; ColorMap.Map[0].red = ColorMap.Map[0].green = ColorMap.Map[0].blue = 0xFF; ColorMap.Map[1].red = ColorMap.Map[1].green = ColorMap.Map[1].blue = 0x00; /* write image descriptor, color map and code size */ if (gifWriteImDesc (pTheFile, &imDesc, &ColorMap, codeSize)) goto BadWrite; break; case FDT_GRAYSCALE: /* initialize the color map */ ColorMap.Length = 256; for (i = 0; i < 256; i++) { ColorMap.Map[i].red = ColorMap.Map[i].green = ColorMap.Map[i].blue = i; } /* write image descriptor, color map and code size */ if (gifWriteImDesc (pTheFile, &imDesc, &ColorMap, codeSize)) goto BadWrite; break; case FDT_PALETTECOLOR: // see if we need to create a palette if (!outType.ColorMap) { outType.ColorMap = FrameCreateColorMap(); if (!outType.ColorMap) { SetError(BEC_errMemory); goto Exit; } outType.ColorMap->NumEntries = 256; if (!CreateOptimizedPalette(lpObject, lpFrame, outType.ColorMap->RGBData, &outType.ColorMap->NumEntries, TRUE, NULL)) { SetError(BEC_errMemory); goto Exit; } } lpRGBmap = outType.ColorMap->RGBData; /* initialize the color map */ ColorMap.Length = 256; for (i = 0; i < 256; i++) { ColorMap.Map[i].red = lpRGBmap[i].red; ColorMap.Map[i].green = lpRGBmap[i].green; ColorMap.Map[i].blue = lpRGBmap[i].blue; } /* write image descriptor, color map and code size */ if (gifWriteImDesc (pTheFile, &imDesc, &ColorMap, codeSize)) goto BadWrite; break; } // initialize stuff to do the type conversion if (!TypeConvert.Init(inType, outType, npix, m_DitherType)) { SetError(BEC_errMemory); goto Exit; } if (outType.DataType == FDT_LINEART) { FrameSetTypeInfo(&inTypeLA, FDT_LINEART); FrameSetTypeInfo(&outTypeLA, FDT_GRAYSCALE); if (!LineArtConvert.Init(inTypeLA, outTypeLA, npix)) { SetError(BEC_errMemory); goto Exit; } } /* write the image */ if ( !( lpLZW = CompressLZW_GIF (pTheFile, NULL, 0, codeSize, NULL ) ) ) goto BadWrite; compressInit = YES; for (y = rSave.top; y <= rSave.bottom; y++) { if (Progress (y-rSave.top, nlin, bEscapable )) goto Exit; if (lpObject) { if (!ObjGetLine( lpObject, rSave.left, y, npix, lpImgScanline)) goto BadRead; lp = lpImgScanline; } else { if ( !(lp = FramePointer(lpFrame, rSave.left, y, NO)) ) goto BadRead; } // convert the data to the new type TypeConvert.ConvertData(lp, lpBuffer, y, npix); if (outType.DataType == FDT_LINEART) { /* convert back to depth one, but with pixel values of 0 and 1 */ LineArtConvert.ConvertData(lpBuffer, lpImgScanline, y, npix); for (lp = lpImgScanline, j = 0; j < npix; j++, lp++) *lp = (*lp) ? 0 : 1; lp = lpImgScanline; } else lp = lpBuffer; /* compress the data */ if (!(CompressLZW_GIF (pTheFile, lp, npix, codeSize, lpLZW))) goto BadWrite; } if ( compressInit ) CompressLZW_GIF (pTheFile, NULL, 0, codeSize, lpLZW); cTerm = 0; pTheFile->Write((LPTR)&cTerm, 1); cTerm = GIFTerm; pTheFile->Write((LPTR)&cTerm, 1); fRet = TRUE; goto Exit; } CATCH_ALL(e) { goto BadWrite; } END_CATCH_ALL BadWrite: SetError(BEC_errWriteFile); goto Exit; BadRead: SetError(BEC_errFrameRead); Exit: /* clean up */ CloseFile(pTheFile, fRet); compressInit = NO; if (lpBuffer) FreeUp (lpBuffer); if (lpImgScanline) FreeUp ( lpImgScanline ); if (outType.ColorMap && (outType.ColorMap != inType.ColorMap)) FrameDestroyColorMap(outType.ColorMap); ProgressEnd(); return (fRet); }
BOOL CWriteBitmap::TGAWrite() { TGAHDR PicHdr; int i, bpl, npix, nlin; LPTR lp, lpBuffer, lpImgScanline; RECT rSave; BOOL bEscapable; LPRGB lpRGBmap; RGBS rgb; BOOL fCompressed; LPFRAME lpFrame = NULL; LPOBJECT lpObject; FRMTYPEINFO inType, outType; CFrameTypeConvert TypeConvert; BOOL fRet = FALSE; CFile* pTheFile = NULL; LPMASK lpMask = NULL; LPTGAOPTIONS lpOptions = (LPTGAOPTIONS)m_lpOptions; ASSERT(lpOptions); if (!lpOptions) return(FALSE); if (m_lpObjList) { lpObject = m_lpObjList->lpHead; if (lpObject) { lpFrame = ObjGetEditFrame(lpObject); if (lpObject->lpAlpha && lpOptions->bSaveMask) lpMask = lpObject->lpAlpha; // if only one object don't do combines if (!lpObject->lpNext) lpObject = NULL; } } else { lpFrame = m_lpFrame; lpObject = NULL; } ASSERT(lpFrame); ProgressBegin(1); if ((pTheFile = OpenFile()) == NULL) { ProgressEnd(); return(FALSE); } TRY { if (m_fSrcArea) rSave = m_rSrcArea; else { rSave.top = rSave.left = 0; rSave.bottom = FrameYSize(lpFrame)-1; rSave.right = FrameXSize(lpFrame)-1; } inType = FrameTypeInfo(lpFrame); // cause FramePointer never returns line art if (inType.DataType == FDT_LINEART) inType.DataType = FDT_GRAYSCALE; // make sure colormap is inited for outType so we don't free bogus ptr FrameSetTypeInfo(&outType, FDT_NONE); npix = RectWidth(&rSave); nlin = RectHeight(&rSave); /* image compression does not currently work */ fCompressed = NO; /* initialize file header */ PicHdr.textSize = 0; /* no id field */ PicHdr.XOffset = 0; PicHdr.YOffset = 0; PicHdr.x = npix; PicHdr.y = nlin; PicHdr.imType = 0; /*************************************/ /* determine type of image to create */ /*************************************/ /* save line art and scatter as gray */ switch (m_DataType) { case FDT_LINEART: case FDT_GRAYSCALE: /* gray */ PicHdr.mapType = 1; PicHdr.mapOrig = 0; PicHdr.mapLength = 256; PicHdr.CMapBits = 24; PicHdr.dataType = (fCompressed) ? 9 : 1; PicHdr.dataBits = 8; bpl = npix; FrameSetTypeInfo(&outType, FDT_GRAYSCALE); lpMask = NULL; break; case FDT_PALETTECOLOR: /* mini color */ PicHdr.mapType = 1; PicHdr.mapOrig = 0; PicHdr.mapLength = 256; PicHdr.CMapBits = 24; PicHdr.dataType = (fCompressed) ? 9 : 1; PicHdr.dataBits = 8; bpl = npix; if (inType.DataType == FDT_PALETTECOLOR) outType = inType; else FrameSetTypeInfo(&outType, FDT_PALETTECOLOR); lpMask = NULL; break; case FDT_RGBCOLOR: case FDT_CMYKCOLOR: /* full color */ PicHdr.mapType = 0; /* no color map data */ PicHdr.mapOrig = 0; PicHdr.mapLength = 0; PicHdr.CMapBits = 0; PicHdr.dataType = 2; PicHdr.dataType = (fCompressed) ? 10 : 2; if (m_DataType == FDT_CMYKCOLOR) lpMask = NULL; if (lpMask || (m_DataType == FDT_CMYKCOLOR)) { PicHdr.dataBits = 32; bpl = npix * 4; } else { PicHdr.dataBits = 24; bpl = npix * 3; } FrameSetTypeInfo(&outType, m_DataType); break; default: //Print ("Unknown flag value"); goto BadRead; break; } AllocLines (&lpBuffer, 1, npix, 4); AllocLines (&lpImgScanline, 1, npix, 4 ); if ( !lpBuffer || !lpImgScanline ) { SetError (BEC_errMemory); goto Exit; } /* write file header */ pTheFile->Write(&PicHdr.textSize, 1); pTheFile->Write(&PicHdr.mapType, 1); pTheFile->Write(&PicHdr.dataType, 1); pTheFile->Write(&PicHdr.mapOrig, 2); pTheFile->Write(&PicHdr.mapLength, 2); pTheFile->Write(&PicHdr.CMapBits, 1); pTheFile->Write(&PicHdr.XOffset, 2); pTheFile->Write(&PicHdr.YOffset, 2); pTheFile->Write(&PicHdr.x, 2); pTheFile->Write(&PicHdr.y, 2); pTheFile->Write(&PicHdr.dataBits, 1); pTheFile->Write(&PicHdr.imType, 1); switch (outType.DataType) { case FDT_LINEART: case FDT_GRAYSCALE: /* gray */ /* write the color map */ for (i = 0; i < 256; i++) { rgb.red = rgb.green = rgb.blue = i; pTheFile->Write((LPTR) &rgb, 3); } break; case FDT_PALETTECOLOR: /* mini color */ // see if we need to create a palette if (!outType.ColorMap) { outType.ColorMap = FrameCreateColorMap(); if (!outType.ColorMap) { SetError(BEC_errMemory); goto Exit; } outType.ColorMap->NumEntries = 256; if (!CreateOptimizedPalette(lpObject, lpFrame, outType.ColorMap->RGBData, &outType.ColorMap->NumEntries, TRUE, NULL)) { SetError(BEC_errMemory); goto Exit; } } lpRGBmap = outType.ColorMap->RGBData; /* write the color map */ for (i = 0; i < 256; i++) { pTheFile->Write((LPTR) &lpRGBmap[i].blue, 1); pTheFile->Write((LPTR) &lpRGBmap[i].green, 1); pTheFile->Write((LPTR) &lpRGBmap[i].red, 1); } break; case FDT_RGBCOLOR: case FDT_CMYKCOLOR: /* full color */ break; } // initialize stuff to do the type conversion if (!TypeConvert.Init(inType, outType, npix, m_DitherType)) { SetError(BEC_errMemory); goto Exit; } /* write the image */ for (i = rSave.bottom; i >= rSave.top; i--) { if (Progress(rSave.bottom-i, nlin, bEscapable)) goto Exit; if (lpObject) { if (!ObjGetLine( lpObject, rSave.left, i, (rSave.right - rSave.left) + 1, lpImgScanline)) goto BadRead; lp = lpImgScanline; } else { if ( !(lp = FramePointer(lpFrame, rSave.left, i, NO)) ) goto BadRead; } // convert the data to the new type TypeConvert.ConvertData(lp, lpBuffer, i, npix); if (outType.DataType == FDT_RGBCOLOR) { swapBGR( lpBuffer, lpBuffer, npix ); if (lpMask) { MergeMaskData(lpMask, rSave.left, i, npix, 3, lpBuffer, lpImgScanline); lp = lpImgScanline; } else lp = lpBuffer; } else lp = lpBuffer; #ifdef UNUSED if ( fCompressed ) { if (tgaComp8Bit (ofh, lp, npix)) goto BadWrite; } else #endif // UNUSED { pTheFile->Write(lp, bpl); } } fRet = TRUE; goto Exit; } CATCH_ALL(e) { goto BadWrite; } END_CATCH_ALL BadWrite: SetError(BEC_errWriteFile); goto Exit; BadRead: SetError(BEC_errFrameRead); Exit: /* clean up */ CloseFile(pTheFile, fRet); if (lpBuffer) FreeUp (lpBuffer); if (lpImgScanline) FreeUp( lpImgScanline ); if (outType.ColorMap && (outType.ColorMap != inType.ColorMap)) FrameDestroyColorMap(outType.ColorMap); ProgressEnd(); return (fRet); }