Advertisement

Unsigned char conversion prob

Started by February 05, 2004 04:59 PM
1 comment, last by Nerothos 21 years, 1 month ago
I''m having problems with this Targa writing function - i''m trying to convert it from C to C++ (using the fstream class). However, I cannot figure out how to give tga.write() the necessary info without losing any data! Since fwrite takes a void pointer, it automatically converts it without losing data, but write takes a string. I know the answer is right there, but I can''t figure it out! Here is the function:

int WriteTGA(char *file, short int w, short int h, unsigned char *data) {
	unsigned char bs;
	short int ss;
	unsigned char imgt;
	int cm;
	unsigned char cs;
	unsigned char bd;
	int is;
	ofstream tga(file, (ios::out | ios::binary));
	if(!tga.is_open()) {
		tga.close();
		return 0;
	}
	imgt = 2;
	bd = 24;
	cm = 3;
	bs = 0;
	ss = 0;
	tga.write(&bs, sizeof(unsigned char));
	tga.write(&bs, sizeof(unsigned char));
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
	tga.write(&imgt, sizeof(unsigned char));
	tga.write((char *) ss, sizeof(short int));
	tga.write((char *) ss, sizeof(short int));
//	fwrite(&imgt, sizeof(unsigned char), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
	tga.write(&bs, sizeof(unsigned char));
	tga.write((char *) ss, sizeof(short int));
	tga.write((char *) ss, sizeof(short int));
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
	tga.write((char *) w, sizeof(short int));
	tga.write((char *) h, sizeof(short int));
//	fwrite(&w, sizeof(short int), 1, filePtr);
//	fwrite(&h, sizeof(short int), 1, filePtr);
	tga.write(&bd, sizeof(unsigned char));
	tga.write(&bs, sizeof(unsigned char));
//	fwrite(&bd, sizeof(unsigned char), 1, filePtr);
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
	is = (w * h * cm);
	for(int idx = 0; (idx < is); idx += cm) {
		cs = data[idx];
		data[idx] = data[idx + 2];
		data[idx + 2] = cs;
	}
	tga.write(data, (sizeof(unsigned char) * is));
	tga.close();
	return 1;
}
  
Extreme thanks!
Jump in and crash the system...it''s quite humbling.
Please don''t crosspost.

Enigma
Advertisement
quote:
Original post by Enigma
Please don''t crosspost.
Exactly.

This topic is closed to new replies.

Advertisement