BMMRES BitmapIO_GIF::Write(int frame) { //-- If we haven't gone through an OpenOutput(), leave if (openMode != BMM_OPEN_W) return (ProcessImageIOError(&bi,BMMRES_INTERNALERROR)); //-- Resolve Filename -------------------------------- TCHAR filename[MAX_PATH]; if (frame == BMM_SINGLEFRAME) { _tcscpy(filename,bi.Name()); } else { if (!BMMCreateNumberedFilename(bi.Name(),frame,filename)) { return (ProcessImageIOError(&bi,BMMRES_NUMBEREDFILENAMEERROR)); } } //-- Create Image File ------------------------------- File file(filename, _T("wb")); if (!file.stream) { return (ProcessImageIOError(&bi)); } // Below this line formatted for Tom's editor (sorry.) outStream = file.stream; // Find out what kind of output file we're dealing with saveStorage = map->Storage(); if(!saveStorage) return (ProcessImageIOError(&bi,BMMRES_INTERNALERROR)); int result = SaveGIF(map); switch(result) { case 1: return BMMRES_SUCCESS; case 0: default: return (ProcessImageIOError(&bi,GetString(IDS_WRITE_ERROR))); } }
BMMRES BitmapIO_PNG::Write(int frame) { //-- If we haven't gone through an OpenOutput(), leave if (openMode != BMM_OPEN_W) return (ProcessImageIOError(&bi,BMMRES_INTERNALERROR)); //-- Resolve Filename -------------------------------- TCHAR filename[MAX_PATH]; if (frame == BMM_SINGLEFRAME) { _tcscpy(filename,bi.Name()); } else { if (!BMMCreateNumberedFilename(bi.Name(),frame,filename)) { return (ProcessImageIOError(&bi,BMMRES_NUMBEREDFILENAMEERROR)); } } return Save(filename, map); }
BMMRES BitmapIO_CIN::Write(int frameNum) { TCHAR filename[MAX_PATH]; if (openMode != BMM_OPEN_W) return ProcessImageIOError(&bi, GetResIDCaption(IDS_CIN_Internal_Error)); if (frameNum != BMM_SINGLEFRAME) { if (!BMMCreateNumberedFilename(bi.Name(), frameNum, filename)) { return ProcessImageIOError(&bi, BMMRES_NUMBEREDFILENAMEERROR); } } else { _tcscpy(filename, bi.Name()); } File file(filename, _T("wb")); if (!file.mStream) return ProcessImageIOError(&bi, GetResIDCaption(IDS_CIN_File_Open_Error)); mStream = file.mStream; BitmapStorage* bms = map->Storage(); if (!bms) return ProcessImageIOError(&bi, GetResIDCaption(IDS_CIN_Internal_Error)); unsigned int height = map->Height(); unsigned int width = map->Width(); CineonFile cineonImage; cineonImage.SetImageFileName(filename); char cDate[128] = ""; char cTime[128] = ""; WIN32_FIND_DATA findFile; HANDLE findhnd = FindFirstFile(bi.Name(), &findFile); SYSTEMTIME time; SYSTEMTIME local; FindClose(findhnd); if (findhnd != INVALID_HANDLE_VALUE) { FileTimeToSystemTime(&findFile.ftLastWriteTime, &time); if (!SystemTimeToTzSpecificLocalTime(NULL, &time, &local)) local = time; sprintf(cDate,"%d:%02d:%02d", local.wYear, local.wMonth, local.wDay); sprintf(cTime,"%02d:%02d:%02d", local.wHour, local.wMinute, local.wSecond); } cineonImage.SetImageCreationDate(cDate); cineonImage.SetImageCreationTime(cTime); cineonImage.SetImageInputDevice("3D Studio MAX"); cineonImage.SetImageInputDeviceModelNumber("4.0"); char buf[CINEON_HDR_DEVICE_SERIAL_NUMBER_LENGTH]; sprintf(buf, "%d", HardwareLockID()); cineonImage.SetImageInputDeviceSerialNumber(buf); cineonImage.SetImageOrientation(0); cineonImage.SetNumberChannels(3); for (int i = 0; i < 3; i++) { cineonImage.SetBitsPerPixel(i, 10); cineonImage.SetPixelsPerLine(i, width); cineonImage.SetLinesPerImage(i, height); } // Put in the NTSC, defaults. max don't allow for correction/modification // FIXME cineonImage.SetWhitePt(0.3324f, 0.3474f); cineonImage.SetRedPt(0.67f, 0.33f); cineonImage.SetGreenPt(0.21f, 0.71f); cineonImage.SetBluePt(0.14f, 0.08f); cineonImage.SetDataInterleave(0); cineonImage.SetPacking(5); cineonImage.SetSigned(0); cineonImage.SetSense(0); cineonImage.SetEOLPadding(0); cineonImage.SetEOCPadding(0); cineonImage.SetImageGamma(1.0f); cineonImage.SetImageXOffset(0); cineonImage.SetImageYOffset(0); cineonImage.SetFramePosition(frameNum); if (cineonImage.VerifyHeader() == FALSE) return ProcessImageIOError(&bi, GetResIDCaption(IDS_CIN_Invalid_Header_Error)); if (!cineonImage.IsSupported()) return ProcessImageIOError(&bi, GetResIDCaption(IDS_CIN_Unsupported_File_Error)); BMM_Color_64* scanLine = (BMM_Color_64*) calloc(width, sizeof(BMM_Color_64)); if (!scanLine) return ProcessImageIOError(&bi, GetResIDCaption(IDS_CIN_Memory_Error)); if (!cineonImage.SetLUTs(10, (float) mUserData.mRefWhite, (float) mUserData.mRefBlack)) return ProcessImageIOError(&bi, GetResIDCaption(IDS_CIN_Memory_Error)); for (unsigned int lineCnt = 0; lineCnt < height; lineCnt++) { GetOutputPixels(0, lineCnt, width, scanLine); if (!cineonImage.WriteScanLine(file.mStream, (unsigned short*) scanLine, lineCnt, width)) { if (scanLine) { free(scanLine); scanLine = NULL; } return ProcessImageIOError(&bi, GetResIDCaption(IDS_CIN_File_IO_Error)); } } if (scanLine) { free(scanLine); scanLine = NULL; } fpos_t fileSize; fgetpos(file.mStream, &fileSize); cineonImage.SetFileSize((unsigned int) fileSize); if (cineonImage.WriteHeader(mStream) == FALSE) return ProcessImageIOError(&bi, GetResIDCaption(IDS_CIN_File_IO_Error)); return BMMRES_SUCCESS; }
BMMRES BitmapIO_BMP::Write(int frame) { BMMRES result = BMMRES_SUCCESS; //-- If we haven't gone through an OpenOutput(), leave if (openMode != BMM_OPEN_W) return (ProcessImageIOError(&bi,BMMRES_INTERNALERROR)); //-- Resolve Filename -------------------------------- TCHAR filename[MAX_PATH]; if (frame == BMM_SINGLEFRAME) { _tcscpy(filename,bi.Name()); } else { if (!BMMCreateNumberedFilename(bi.Name(),frame,filename)) return (ProcessImageIOError(&bi,BMMRES_NUMBEREDFILENAMEERROR)); } //-- Create Image File ------------------------------- File file(filename, _T("wb")); if (!file.stream) return (ProcessImageIOError(&bi)); //-- Create File Header------------------------------- BITMAPFILEHEADER hdr; PBITMAPINFO pbmi; int lx,y; memset(&hdr,0,sizeof(BITMAPFILEHEADER)); hdr.bfType = 0x4d42; hdr.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER); //-- Pallette buffer and pixel buffer for 8bit output PixelBuf8 *pixBuf; BYTE *pix; PixelBuf48 *palBuf; BMM_Color_48 *pal; int w = map->Width(); int wb = (map->Width() + 3) & ~3; // width must be multiple of 4 int h = map->Height(); switch(mParams.outDepth) { //-- Paletted BMP required case BMM_PALETTED: pixBuf = new PixelBuf8(wb*h); palBuf = new PixelBuf48(256); if( (!pixBuf) || (!palBuf) ) ProcessImageIOError(&bi,BMMRES_MEMORYERROR); pix = pixBuf->Ptr(); pal = palBuf->Ptr(); if( (!pix) || (!pal) ) ProcessImageIOError(&bi,BMMRES_MEMORYERROR); if( Storage()->Paletted()) { //-- Existing map is palletted...so get the pallete and //-- the look up table..we are done.. Storage()->GetPalette(0, 256, pal); for(y = 0; y < h; y++) Storage()->GetIndexPixels(0,(h-y-1),w,pix+wb*y); } else { //-- Caluculate the pallete for the image.. //-- Then create the look up table if(CalcOutputPalette(256,pal) == 0) ProcessImageIOError(&bi); PixelBuf64 line(w); ColorPacker* cPack = BMMNewColorPacker(w,pal,256); for(y=0; y<h; y++) { if(!GetOutputPixels(0,(h-y-1),w,line.Ptr())) ProcessImageIOError(&bi); cPack->PackLine(line.Ptr(),pix+y*wb,w); } cPack->DeleteThis(); } //-- Fill in the BITMAPINFO structure--------------------- lx = sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD) + wb*h; pbmi = (PBITMAPINFO)LocalAlloc(LPTR,lx); if (!pbmi) return (ProcessImageIOError(&bi,GetString(IDS_CONVERT_ERROR))); memset(pbmi,0,lx); pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmi->bmiHeader.biWidth = w; pbmi->bmiHeader.biHeight = h; pbmi->bmiHeader.biPlanes = 1; pbmi->bmiHeader.biBitCount = 8; pbmi->bmiHeader.biCompression = BI_RGB; pbmi->bmiHeader.biSizeImage = w*h; //256*sizeof(RGBQUAD) + wb*h; pbmi->bmiHeader.biXPelsPerMeter = 2834; pbmi->bmiHeader.biYPelsPerMeter = 2834; hdr.bfOffBits += 256*sizeof(RGBQUAD); // DS 2/16/98 RGBQUAD *rgb; //-- Fill in the palette rgb = (RGBQUAD*) &(pbmi->bmiColors[0]); for(y=0; y<256; y++) { rgb->rgbRed = pal[y].r >> 8; rgb->rgbGreen = pal[y].g >> 8; rgb->rgbBlue = pal[y].b >> 8; rgb++; } //-- Fill in the look up table memcpy((LPBYTE)rgb, pix,wb*h); break; //-- RGB24 requested-------------------------------------- case BMM_NO_TYPE: case BMM_TRUE_24: { //-- Convert Bitmap to DIB --------------------------- pbmi = GetDitheredOutputDib(); if (!pbmi) return (ProcessImageIOError(&bi,GetString(IDS_CONVERT_ERROR))); //-- Prepare Header ------------------------ if (bi.GetUpdateWindow()) SendMessage(bi.GetUpdateWindow(),BMM_PROGRESS,25,100); int rb = (map->Width() * 3 + 3) & ~3; // must be multiple of 4 bytes lx = sizeof(BITMAPINFOHEADER) + (rb * map->Height()); } break; default: assert(0); return BMMRES_IOERROR; break; } hdr.bfSize = lx + sizeof(BITMAPFILEHEADER); //-- Write Header ------------------------ size_t res = fwrite(&hdr,1,sizeof(BITMAPFILEHEADER),file.stream); if (res != sizeof(BITMAPFILEHEADER)) { io_error: result = ProcessImageIOError(&bi); LocalFree(pbmi); return (result); } //-- Write Image File -------------------------------- if (bi.GetUpdateWindow()) SendMessage(bi.GetUpdateWindow(),BMM_PROGRESS,50,100); res = fwrite(pbmi,1,lx,file.stream); if (res != lx) goto io_error; LocalFree(pbmi); if (bi.GetUpdateWindow()) SendMessage(bi.GetUpdateWindow(),BMM_PROGRESS,100,100); return (result); }