c++ - How to update single pixels efficiently on SDL 1.2 surface? -


I am writing a small item that calculates RGB values ​​for each pixel of the image.

I want to display each after the calculation. The problem is that I did not know how to make it faster, it takes a few seconds to calculate the entire image and then display it, where each pixel takes a few minutes to appear.

It is important that I can see progress on the image count. Is there any way to solve this task efficiently in SDL 1.2?

My code now looks like this:

  SDL_Surface * screen = NULL; SDL_Surface * image = NULL; SDL_Init (SDL_INIT_EVERYTHING); // Set screen screen = SDL_SetVideoMode (img.width), img.height (), 32, SDL_SWSURFACE); // Create the surface with the image of the basic values ​​= SDL_CreateRGBSurface (SDL_SWSURFACE, img.width), img.height (), 32,0,0,0,0,0,0); If (image == faucet) {std :: cout & lt; & Lt; "SDL failed to form surface"; } // Create an array for pixels Uint32 * pixel = (Uint32 *) image- & gt; Pixels; // Apply the image to the SDL_ BlitSurface screen (image, zero, screen, tap); // Update Screen SDL_Flip (screen); // compute image (int i = 0; i  Format, (Uint8) getR (IMG (I),), (UINT8) getG (IMG (I, J)), (UIT 8) getB (img (i, j))); SDL_BlitSurface (image, tap, screen, tap); SDL_UpdateRect (Screen, I, J, I, J); }} // stop SDL Della (2000); // Free the loaded image SDL_FreeSurface (image); // Exit SDL SDL_Quit;  

You do not need an intermediate image, you can put pixels directly on the surface of the screen Are there. For each pixel, you can call SDL_Flip () or SDL_FupetRect () for each pixel, it will be very fast.

In addition, looping through lines rather than columns, will boost you a little speed, because the pixels are located in a physical memory on a single line, and the modern CPU's sequential memory access It's fast.

Create an array for pixels Uint32 * pixels = (Uint32 *) screen-> Pixels; // Clear screen SDL_FillRect (screen, tap, 0); // Update Screen SDL_Flip (screen); // for compute image (int j = 0; j & lt; img.height (); j ++) {for (int i = 0; i & lt; img.width (); i ++) {img (I, j) = computecolor (i, j, img, wthath), img. height ()); Pixel [i + j * img.width ()] = SDL_MapRGB (image-> Format, (Uint8) getR (IMG (I, J)), (Uint8) getG (IMG (I, J)), (UIT 8) getB (img (i, j))); } SDL_UpdateRect (screen, i, j, img.width (), 1); } // Display the final result SDL_Flip (screen); SDL_Delay (2000);

You can also create a different variable variable for img.height () and img.width (), so that you can not call each repetition function, but the compiler usually does anyway Able to customize


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -