2006.07.21 12:40 "[Tiff] How to save in tiff format file?", by Toh kc

2006.08.16 02:05 "[Tiff] Re: How to save in tiff format file?", by Toh kc

Hi, Sorry to disturb again.. I have tried the method
given but still doesn't work.. I have attached 2 main
functions about saving my tiff files.

  1. onSave(): opening the dialog box to save tiff file
  2. SaveTiffFile(): the saving the tiff file
  3. for the SaveTiffFile(), have already tested and able
  4. to save a tiff file, what has really bother about ne
  5. is still the intialisation of the array, sImg. I have

doubt here:
Can I say that for most of the time when saving the
the tiff file, the intialisation of the array for the
tiff image to save in, is the same? Thanks again for
seeing my post again. :)

void CExoDialog2Dlg::OnSave()
{
    CString name1;

        CFileDialog dlg(FALSE,"tif","*.tif",
                OFN_HIDEREADONLY | OFN_FILEMUSTEXIST |
OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST |
            OFN_OVERWRITEPROMPT,
                "TIFF Files (*.tif)|*.tif|"
                "All Files (*.*)|*.*||",
                NULL);

    char title[]= {"Save Image"};
        dlg.m_ofn.lpstrTitle= title;

    if( dlg.DoModal() == IDOK )
        {
                CString szFile = dlg.GetFileName();
                TCHAR ext[_MAX_EXT];
                _tsplitpath(szFile,NULL,NULL,NULL,&ext[0]);

if(0 == _tcsicmp(ext,".tif"))
{
        int nTiffPixels = TiffWidth * TiffHeight;
        unsigned char *sImg = NULL;
        if (sImg == NULL)
        sImg = new unsigned char [nTiffPixels];

for (int i = 0; i < Gheight; i++)
   for(int j = 0; j < Gwidth; j++)

        sImg[i * Gwidth + j] = (unsigned char)(j * i);

name1.Format("image\\s.tif",szFile);
SaveTiffFile(name1, TiffWidth, TiffHeight, sImg);
        if (sImg != NULL) delete sImg;
                }
        else
                return;
}

void CExoDialog2Dlg::SaveTiffFile(CString szFileName,
int Width, int Height, unsigned char *Img)
{
        TIFF* out;
        out = TIFFOpen(szFileName, "w");
        if (out != NULL)
        {
                uint16 compression = COMPRESSION_NONE;
                uint16 photometric = PHOTOMETRIC_MINISBLACK;
                short config = PLANARCONFIG_CONTIG;

TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) Width);

TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) Height);

TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);

TIFFSetField(out, TIFFTAG_COMPRESSION, compression);

TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);

TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);

TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);

TIFFSetField(out, TIFFTAG_MINSAMPLEVALUE, 0);

TIFFSetField(out, TIFFTAG_MAXSAMPLEVALUE, 255);

TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);

TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1L);

TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, szFileName);

tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out));

                uint8* pp = (uint8*) buf;

                long line = 0;
                for (int y = 0; y < Height; y++)
                {
                for (int x = 0; x < Width; x++)
                pp[x] = Img[line+x];
                if (TIFFWriteScanline(out, buf, Height-y-1, 0) < 0)
                break;
                line+=Width;
                }
                _TIFFfree(buf);
                TIFFClose(out);
        }
}

__________________________________
New and Improved Yahoo! Mail - 1GB free storage!
http://sg.whatsnew.mail.yahoo.com