Thread: Bitwise mask operations to write targa file

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    15

    Bitwise mask operations to write targa file

    Hello. I did write some code to write a targa file in the disk.
    The code I came up is this:

    Code:
    ofs.put (width & 0xFF),  ofs.put ((width >> 8) & 0xFF);
             ofs.put (height & 0xFF), ofs.put ((width >> 8) & 0xFF);
    Explanation: to write the less significant bits I pick up the integer variables and do a bitwise '&' with FF or 256, to try to preserve the less sginicant 8bits of the integer variable and discharging the most significant ones. Howerver this did not work.

    Why Doesn't the compiler put a bunch of zeros to the left of the FF? If not, why? Can't he imagine that if am doing a bitwise with something larger than FF, everything to the left should be all zeros?


    Now, the right side, My idea was to get rid of the eight less significant bits, or basically to put the bits 8-15 in place of 0-7 and then doing bitwise & with FF to preserve them for the write. But this doen't work either.

    Can someone shed some light on this matter?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    I don't see why your code isn't working. You need to post a complete example program that demonstrates the problem.
    Code:
    #include <iostream>
    int main() {
        unsigned width = 1234; // must be < 65536
        std::cout << (width & 0xFF) << " | " << ((width >> 8) & 0xFF) << '\n';
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by colt View Post
    Code:
    ofs.put (width & 0xFF),  ofs.put ((width >> 8) & 0xFF);
             ofs.put (height & 0xFF), ofs.put ((width >> 8) & 0xFF);
    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).

  4. #4
    Registered User
    Join Date
    Sep 2019
    Posts
    15
    Quote Originally Posted by christop View Post
    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 ();
    }
    Last edited by colt; 09-23-2022 at 08:31 PM.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Oh, the code you posted is different than your first post. Can you spot the difference?

    From #1:
    Code:
    ofs.put (width & 0xFF),  ofs.put ((width >> 8) & 0xFF);
    From #4:
    Code:
    ofs.put (width & 0xFF), ofs.put ((width >> 8) & 0xFF00);
    Besides that, I don't see any other issues. The Targa format takes 16-bit values for width and height, so you don't need bytes 3 and 4 (only bytes 0 and 1) of width and height.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise mask and manipulation...
    By kraghavan in forum C++ Programming
    Replies: 3
    Last Post: 06-15-2010, 11:43 AM
  2. Bitwise Operations
    By drkidd22 in forum C Programming
    Replies: 2
    Last Post: 11-12-2009, 11:09 PM
  3. Bitwise set and mask issue
    By hamsteroid in forum C Programming
    Replies: 5
    Last Post: 04-02-2007, 02:00 PM
  4. Targa File Help
    By jimboob in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2005, 04:34 AM
  5. bitwise operations
    By andrew_tucker in forum C Programming
    Replies: 2
    Last Post: 11-28-2002, 12:46 AM

Tags for this Thread