Speed of simple add operation



Newbie here. Can somebody post how long it takes to composite two 4096 x 2048 bmp images together? Comp operation is simple add. I’ve written simple command line compositing app. Well… It does just add right now, but I guess it can be called that way:D. My current time is 55 s on Athlon xp 2000+ with 768 MB RAM. That is slow, right?


You could check your results against the ImageMagick library, which is open source. I think you can use mogrify to composite, among others.


yes it is… Ps can do it in nearly realtime…
what language do you use?


Haven’t tried ImageMagick yet…

But yeah. 55 s was really slow. I don’t exactly remember my current times after few code changes, but it was about 11 s, which isn’t fastest, but it’s faster then before:).
I use c++. Right now I am trying to add some script/flow like file support.


The quickest thing available for comping images together is the GPU and some blending operations (you might need to split the images up and do it there). Whilst normally sending that amount of data about isn’t generally recommended, the benenifts of using the GPU should outweight the (comparitivly) small cost of sending and reading back the data.


btw, this sounds like a very slow thing imo. You are accessing raw data pointers i assume? ie, not doing something silly like :

setOutPixel( getInPixel1() + getInPixel2() );

Function call overhead of that sort would kill performance (and i assume you are building in release mode with optimisations turned on).

I assume you are using some sort of direct access to the pixels anyway, ie walking over the raw data with pointers rather than using some sort of (i*height + j) indexing to the pixels?

unsigned bytes_per_pixel=3; // rgb

unsigned char* pImage1 = someImageData;
 unsigned char* pImage2 = someOtherImageData;
 unsigned char* pOut = someOutputImage;
 unsigned char* pEndImage1 = pImage1 + img_w* img_h * bytes_per_pixel;

for( ; pImage1 != pEndImage1; )
	*pOut = *pImage1 + *pImage2;
	++pOut; ++pImage1; ++pImage2;


GPU maybe later. As I said before I’m a newbie, I started learning c++ 2 months ago so yes, I’m probably making something that silly or maybe something even more silly. Right now it is loading whole image to image class. It does that through two loops, reads value through (ifstream) read function and stores pixels in two dimensional array, then add those two images together and write it to disk. Pixel values are floats. That bad?:smiley:

edit: 3.5 s just thanks to the reading whole line of image instead of reading image by single char…


it’s quicker to read the whole image in one go (I prefer the old C-style File IO for binary files - should be able to do something similar with streams though). Typically image files are stored as unsigned char’s, however floating point formats are probably more useful for compositing (you’ll get much better results, though it may be slightly slower).

This will load any file in one go, it’s then a case of casting the data to your image file data and off you go… (not sure what format you are using)

// load the whole file in one go....
unsigned char* imageData=0;
FILE* fp = fopen("image.whatever","rb");
   size_t pos = ftell(fp);
   imageData = new unsigned char[pos];

//------------------------------------ define the image file format

// don't optimise memory layout !
#pragma pack(push,1)

struct PixelFormat {
   float r;
    float g;
    float b;
    float a;

struct SomeImageHeader
   unsigned int width;
    unsigned int height;
   PixelFormat pixels[1]; // << dirty hack to access pixel data
   // dont instance me!
   SomeImageHeader() {}

// return to compiler defaults
#pragma pack(pop)

SomeImageHeader* asImage = (SomeImageHeader*)((void*)imageData);

// do something with image data

// when done, don't forget to delete the data...
delete [] imageData;


Thanks Robert Bateman.

It’s quicker to read image in one go, but I have had some problems with it. Not sure if it’s my mistake or limitation but when I try to read bigger images (not sure… somewhere around 450 * 450) program crashes. Could it be char array limitation?

edit: Nevermind. Found solution.


This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.