Hi I was wondering if you could help me with a question on image
processing. I am using c++to process the image.I have a raw file, and I have read it by creating an fstream, and have saved the length of
the file in variable end.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;


int file_length(ifstream *is)
{
		int end;
		
		// get length of file:
		is->seekg (0, ios::end);
		end = is->tellg();
		is->seekg (0, ios::beg);

}


int main()
{
	char * buffer;
	
	//unsigned char buffer[128][128];
	ifstream myfile("Baboon.raw", ifstream::in); //the file size 4x9x54
	int end;
	end = file_length(&myfile);
	
	cout << end << "\n";
	buffer = (char*) malloc (end);
	/*
	* returns the length of a file (in bytes)
	*/


}

I now have to store the image into a matrix and create code to
process the image into a bmp file.

Any suggestions?

Thanks

Recommended Answers

All 7 Replies

Hi I was wondering if you could help me with a question on image
processing. I am using c++to process the image.I have a raw file, and I have read it by creating an fstream, and have saved the length of
the file in variable end.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;


int file_length(ifstream *is)
{
		int end;
		
		// get length of file:
		is->seekg (0, ios::end);
		end = is->tellg();
		is->seekg (0, ios::beg);

}


int main()
{
	char * buffer;
	
	//unsigned char buffer[128][128];
	ifstream myfile("Baboon.raw", ifstream::in); //the file size 4x9x54
	int end;
	end = file_length(&myfile);
	
	cout << end << "\n";
	buffer = (char*) malloc (end);
	/*
	* returns the length of a file (in bytes)
	*/


}

I now have to store the image into a matrix and create code to
process the image into a bmp file.

Any suggestions?

Thanks

what do you mean by processing? you just want to convert raw image to bmp or you want to modify the image (like antialising, bluring etc..,). i hope freeimage library tutorial will be very useful for you.

Regards,

Hi, thanks alot for the reply, I think im just not understanding what needs to be done. I just need to firstly convert the raw image to a bmp, then when i understand that then i will be modifying it.
I need to store the raw image in a matrix then read the matrix to display the image.
also what is the freeimage library tutorial?
thanks

FreeImage is an Open Source library project for developers. who wants to work around different image formats. All the images are just some bunch of pixel values (R-red,G-green,B-blue,A-alpha) when it goes to the display drivers. FreeImage will gives you all these options of modifying them or saving them (including convert and save as different format) just as a function calls. you will find many tutorials if you search google.

Thanks Ive looked but its a bit hard for me to understand, It should be quite simple to do Ive read the data into malloc, now I need to read it and convert it into a bmp, I just dont know how to do that!

I made a little snippet that allows you to directly access the pixels and save it as a .bmp. [link] I've simplified it a little for you.

#include <windows.h>
#include <iostream>
#include <cmath>

typedef unsigned long ulong;
typedef unsigned short ushort;
typedef unsigned char ubyte;

struct Pixel {
   union {
      ulong rgb;
      struct {
         // r, g, b share same memory as rgb
         unsigned char b, g, r;
      };
   };

   Pixel() {
      // Automatically set pixel black
      rgb = 0;
   }

   // Init pixel
   Pixel(byte _r, byte _g, byte _b) {
      r = _r;
      g = _g;
      b = _b;
   }

   operator ulong() {
      return rgb;
   }

   void operator ()(byte _r, byte _g, byte _b) {
      r = _r;
      g = _g;
      b = _b;
   }

   // Set pixel
   Pixel &operator =(ulong _RGB) {
      rgb = _RGB;
      return *this;
   }
};

template<short _Width, short _Height>
struct Bitmap {
   // Pixels
   Pixel *pixels;

   // Dimensions
   short width;
   short height;

   // Init
   Bitmap() {
      width = _Width;
      height = _Height;

      // Allocate Pixels
      pixels = new Pixel[width * height];
   }

   // Destroy
   ~Bitmap() {
      // Free all pixels
      delete[] pixels;
   }

   inline Pixel &operator()(short x, short y) {
      // Returns reference to pixel
      return pixels[(height - ++y) * width + x];
   }

   void save(const char *_FileName) {
      // Function to save as bitmap (no compression)
      BITMAPINFOHEADER bmpInfoHeader = {0};

      // Properties
      bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
      bmpInfoHeader.biBitCount = sizeof(Pixel) * 8;
      bmpInfoHeader.biClrImportant = 0;
      bmpInfoHeader.biClrUsed = 0;
      bmpInfoHeader.biCompression = BI_RGB;
      bmpInfoHeader.biHeight = height;
      bmpInfoHeader.biWidth = width;
      bmpInfoHeader.biPlanes = 1;
      bmpInfoHeader.biSizeImage = width * height * sizeof(Pixel);

      BITMAPFILEHEADER bfh = {0};
      bfh.bfType = 0x4D42;
      bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
      bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage;

      // Create File
      HANDLE hFile = CreateFile( _FileName, GENERIC_WRITE, 0, NULL,
                        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

      // Failed
      if (!hFile) {
         return;
      }

      DWORD dwWritten = 0;
      WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL );
      WriteFile( hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwWritten, NULL );
      WriteFile( hFile, &pixels[0], bmpInfoHeader.biSizeImage, &dwWritten, NULL );

      CloseHandle( hFile );
   }

};

int main() {
   // Make Bitmap Object
   Bitmap<500, 500> bmp;

   // Change one pixel to red
   bmp(100,100) = Pixel(255,0,0);

   // Save to file
   bmp.save("image.bmp");
   return 0;
}
commented: Sweet! Bookmarked. +10

Thanks so much for putting that up, I will try it out and make sure I understand the steps

I think it may be to complex, Is there an easier way, i.e. just addding a few extra lines to my original code, like creating a matrix from malloc (im not sure is this needs to be done in order to convert the image) then reading the image from the matrix and storing it as a bmp file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.