
Originally Posted by
christop
You use width three times but height only once.
Besides, where would you expect to see "a bunch of zeros to the left of the FF"? Each of those ofs.put() calls is writing one byte of data to a file, correct? I would expect the file to contain exactly what you write to it (i.e., only 4 bytes, so no room for a bunch of zeroes).
I was talking what would be used to do a bitwise '&' with the most significant bits of width and height (from bit 8 to 31). With '0xFF' being 1111 1111, I suppose that when doing the bitwise operation, there would be a bunch of zeros to do the operation with bytes 2 to 4 of height and width;
I don't see why your code isn't working. You need to post a complete example program that demonstrates the problem.
Here is:
Code:
#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;
class Image {
public:
Image (int _width,int _height,int bpp) : width (_width), height (_height), bytes_per_pixel (bpp/8), ofs ("output.tga", ios::binary) {
ofs.put (0), ofs.put (0);
ofs.put (2);
ofs.put (0), ofs.put (0);
ofs.put (0), ofs.put (0);
ofs.put (0);
ofs.put (0), ofs.put (0);
ofs.put (0), ofs.put (0);
ofs.put (width & 0xFF), ofs.put ((width >> 8) & 0xFF00);
ofs.put (height & 0xFF), ofs.put ((height >> 8) & 0xFF00);
ofs.put (bpp), ofs.put (0);
image_array = new unsigned char [width*height*bytes_per_pixel];
}
unsigned char* get_image_array () { return image_array; }
ofstream ofs;
int width,height,bytes_per_pixel;
private:
unsigned char * image_array;
};
struct Vec3f {
Vec3f (float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
float x,y,z;
};
struct Sphere {
Sphere (Vec3f _center, float _radius) : center (_center), radius (_radius) {}
Vec3f center;
float radius;
};
struct Scene {
vector <Sphere> sphere_vector;
vector <Vec3f> light_vector;
vector <Vec3f> material_vector;
};
main () {
Image image (255,255,24);
Scene scene;
scene.sphere_vector.push_back (Sphere (Vec3f (0.0,0,-20),4));
cout << scene.sphere_vector[0].radius << endl;
unsigned char * image_array = image.get_image_array ();
int image_ctr=0;
for (int y=0; y<image.height; y++) {
for (int x=0; x<image.width; x++) {
float t = 1000000;
//for (int z=0; z<scene.sphere_vector.size(); z++)
image_array [image_ctr++] = 102;
image_array [image_ctr++] = 101;
image_array [image_ctr++] = 100;
}
}
image.ofs.write ((char*)image_array,(image.width*image.height*image.bytes_per_pixel));
delete image_array;
image.ofs.close ();
}