PDA

Click to See Complete Forum and Search --> : Saving A Picture?


Vlatko
Feb 5th, 2001, 04:47 AM
What are some of the possible ways to save a picture to a file using VC++.

parksie
Feb 5th, 2001, 01:42 PM
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:

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... ;)