Results 1 to 2 of 2

Thread: Saving A Picture?

  1. #1

    Thread Starter
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    What are some of the possible ways to save a picture to a file using VC++.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The easiest file type to use is Targa...I don't have any code for writing it, but I'm sure you can work it out from this reader code:
    Code:
    bool mvcTexture::LoadFromTGA(char* pcFilename, uint nTexMode) {
    	// Targa data type -- uncompressed, RGBA image; element
    	// 3 is always 2.
    	GLubyte    targaMagic[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    
    	GLubyte    fileMagic[12];
    
    	// Header data.
    	GLubyte    header[6];
    	GLuint     bytesPerPixel;
    
    	GLuint     imageSize;
    	GLuint     tmp;
    	GLint      i;
    	long lComponents;
    
    	FILE *file = NULL;
    	file = fopen(pcFilename, "rb");
    
    	// Start reading in the header, make sure we
    	// have the right Targa data type.  
    
    	if(file == NULL || fread(fileMagic, 1, sizeof(fileMagic), file) != sizeof(fileMagic) || memcmp(targaMagic, fileMagic, sizeof(targaMagic)) != 0 || fread(header, 1, sizeof(header), file) != sizeof(header)) {
    		fclose(file);
    		return false;
        }
    
    	// Determine width and height from header data.
    
    	m_nWidth  = header[1] * 256 + header[0];
    	m_nHeight = header[3] * 256 + header[2];
    
    	// Determine color bit depth of the image (24 or 32).
    
    	if(m_nWidth <= 0 || m_nHeight <= 0 || (header[4] != 24 && header[4] != 32)) {
    		fclose(file);
    		return false;
    	}
    
    	m_nBPP = header[4];
    	bytesPerPixel = m_nBPP / 8;
    	imageSize     = m_nWidth * m_nHeight * bytesPerPixel;
    
    	m_pucImageData = (GLubyte *)malloc(imageSize);
    
    	if(m_pucImageData == NULL || fread(m_pucImageData, 1, imageSize, file) != imageSize) {
    		if(m_pucImageData != NULL) {
    			free(m_pucImageData);
    		}
    
    		fclose(file);
    		return false;
    	} 
    
    	for(i = 0; i < imageSize; i += bytesPerPixel) {
    		tmp = m_pucImageData[i];
    		m_pucImageData[i] = m_pucImageData[i + 2];
    		m_pucImageData[i + 2] = tmp;
    	}
    
    	fclose (file);
    It needs a little tidying up to change to use new instead of malloc, but that shouldn't be too hard.

    PS: Targa files are stored as BGR, not RGB...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width