c++ - Quickly draw custom format image on screen -


i developing own image format game , other applications , want make image viewer of images. problem is, when try render bigger image (1920x1080), takes atleast 10 seconds , application stops responding few seconds , disappears.

class renderer{     //...     inline void getrgb(unsigned int color,unsigned char &r,unsigned char &g,unsigned char &b){         r=((color>>16)&0xff); g=((color>>8)&0xff); b=((color)&0xff);     }     inline bool isvalidpos(unsigned int x,unsigned int y){         if((x>=0&&x<width)&&(y>=0&&y<height))return true;         else return false;     }     inline void getpoint(unsigned int x,unsigned int y,unsigned int &color){         if(isvalidpos(x,y))             color=photostream[y*height+x];     }     void drawpoint(unsigned int& x,unsigned int& y){         unsigned char r,g,b;         unsigned int col;         getpoint(x,y,col);         //getrgb(col,r,g,b);         setpixel(hdc,x,y,col);     } }; void rendershmp(imageshmp<unsigned int>& img,renderer *renderer){     imageshmp<unsigned int> tmpimg(img.getx(),img.gety()); //create new image, 1 shown     for(unsigned int x=0;x<tmpimg.getx();x++)         for(unsigned int y=0;y<tmpimg.gety();y++)             tmpimg.setpoint(x,y,img.getpoint(x,y)); //copy data     if(img.getx()>width||img.gety()>height){ //resize image if bigger window resolution         double scale=1.0;         scale=static_cast<double>(width)/static_cast<double>(img.getx());         tmpimg.scale(scale);     }     //code above takes max. 0,2 seconds execute     unsigned int col=0;     for(unsigned int x=0;x<tmpimg.getx();x++)         for(unsigned int y=0;y<tmpimg.gety();y++){             col=tmpimg.getpoint(x,y);             renderer->setpoint(x,y,col);    //sets color in memory later use             renderer->drawpoint(x,y);         } } 

imageshmp contains simple buffer of type t equal width*height. data saved in buffer, example point [20,30] located @ [30*height+20]. how make draw faster, window won't in "not responding" state? mean fast simple windows image viewer it.


final code:

void rendershmp(imageshmp<unsigned int>& img,renderer *renderer){     imageshmp<unsigned int> tmpimg(img.getx(),img.gety());     for(unsigned int x=0;x<tmpimg.getx();x++)         for(unsigned int y=0;y<tmpimg.gety();y++)             tmpimg.setpoint(x,y,img.getpoint(x,y));     if(img.getx()>width||img.gety()>height){         double scale=1.0;         scale=static_cast<double>(width)/static_cast<double>(img.getx());         tmpimg.scale(scale);     }     int w=tmpimg.getx(),h=tmpimg.gety();     unsigned char r=0,g=0,b=0;     bitmapinfo bmi;     hbitmap hbm;     unsigned char *bytes=new unsigned char[w*h*3];     hdc hdc;     renderer->gethdc(hdc);     unsigned int ctr=0;     zeromemory(&bmi, sizeof(bmi));     bmi.bmiheader.bisize = sizeof(bmi.bmiheader);     bmi.bmiheader.biwidth = w;     bmi.bmiheader.biheight = h;     bmi.bmiheader.biplanes = 1;     bmi.bmiheader.bibitcount = 24;     bmi.bmiheader.bicompression = bi_rgb;     hbm = createdibsection(hdc, &bmi, dib_rgb_colors,(void **)&bytes, null, 0);     hdc compatibledc=createcompatibledc(hdc);     hbitmap hbmp = createcompatiblebitmap  (hdc, w, h);     for(int y=int(h)-1;y>=0;--y){         for(int x=0;x<int(w);++x){             renderer->getrgb(tmpimg.getpoint(x,y),r,g,b);             bytes[ctr++]=b;             bytes[ctr++]=g;             bytes[ctr++]=r;         }     }     setdibits (hdc, hbmp, 0, h, bytes,&bmi, dib_rgb_colors);     hbmp=(hbitmap)selectobject (compatibledc, hbmp);     bitblt(hdc, 0, 0, w, h, compatibledc, 0, 0, srccopy);     deleteobject(selectobject(compatibledc, hbmp));     deletedc(compatibledc);     delete [] bytes; } 

calling setpixel 2m times take bit of time. think you'd better of resizing image bitmap, , drawing screen. use example createdibsection, , populate via ppvbits, , use bitblit() draw screen itself.


Comments