Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Improving code

Improving code

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structurescode-review
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    uus99
    wrote on last edited by
    #1

    Hi everyone, i'm trying to write a real-time image processing program.I've made the following class, how can i improve it further? array[] contains all the image bits from a CBitmap using GetBitmapBits. First 8 bits=Blue, next 8 Green, next 8 Red and next 8 unused. BitDepth=4; How can i improve the Threshold, Grayscale & Rotate functions? It must be timecritical. Usage, uImage image; image.Create(bmp_x_width,bmp_y_width,32); image.GetImage(&memDCLoad); image.Grayscale(); image.Rotate(45); image.SetImage(&memDCLoad); pDC->StretchBlt(243,30,stretch_x,stretch_y,&memDCLoad,0,0,bmp_x_width,bmp_y_width,SRCCOPY); class uImage { private: int height_y; int width_x; int bitDepth; int imgDepth; public: BYTE *array; BYTE *array_copy; CBitmap *pBitmap; void Create(int x,int y,int depth); void GetImage(CDC *MemDC); void SetImage(CDC *MemDC); void Delete(); BYTE GetR(int x,int y); BYTE GetG(int x,int y); BYTE GetB(int x,int y); void SetR(int x,int y, BYTE value); void SetG(int x,int y, BYTE value); void SetB(int x,int y, BYTE value); void Threshold(int value); void Grayscale(); void Rotate(double degree); }; void uImage::GetImage(CDC *MemDC) { pBitmap =MemDC->GetCurrentBitmap(); pBitmap->GetBitmapBits(bitDepth*width_x*height_y,array); } void uImage::SetImage(CDC *MemDC) { pBitmap =MemDC->GetCurrentBitmap(); pBitmap->SetBitmapBits(bitDepth*width_x*height_y,array); } void uImage::Create(int x,int y, int depth) { bitDepth=depth/8; imgDepth=depth; array=new BYTE[x*y*bitDepth]; array_copy=new BYTE[x*y*bitDepth]; height_y=y; width_x=x; } void uImage::Delete() { delete[] array; delete[] array_copy; } BYTE uImage::GetR(int x, int y) { return array[(x+y*width_x)*bitDepth+2]; } BYTE uImage::GetG(int x, int y) { return array[(x+y*width_x)*bitDepth+1]; } BYTE uImage::GetB(int x, int y) { return array[(x+y*width_x)*bitDepth+0]; } void uImage::SetR(int x, int y, BYTE value) { array[(x+y*width_x)*bitDepth+2]=value; } void uImage::SetG(int x, int y, BYTE value) { array[(x+y*width_x)*bitDepth+1]=value; } void uImage::SetB(int x, int y, BYTE value) { array[(x+y*width_x)*bitDepth+0]=value; } void uImage::Threshold(int value) { BYTE gray; int temp; for(int i=0;i

    M 1 Reply Last reply
    0
    • U uus99

      Hi everyone, i'm trying to write a real-time image processing program.I've made the following class, how can i improve it further? array[] contains all the image bits from a CBitmap using GetBitmapBits. First 8 bits=Blue, next 8 Green, next 8 Red and next 8 unused. BitDepth=4; How can i improve the Threshold, Grayscale & Rotate functions? It must be timecritical. Usage, uImage image; image.Create(bmp_x_width,bmp_y_width,32); image.GetImage(&memDCLoad); image.Grayscale(); image.Rotate(45); image.SetImage(&memDCLoad); pDC->StretchBlt(243,30,stretch_x,stretch_y,&memDCLoad,0,0,bmp_x_width,bmp_y_width,SRCCOPY); class uImage { private: int height_y; int width_x; int bitDepth; int imgDepth; public: BYTE *array; BYTE *array_copy; CBitmap *pBitmap; void Create(int x,int y,int depth); void GetImage(CDC *MemDC); void SetImage(CDC *MemDC); void Delete(); BYTE GetR(int x,int y); BYTE GetG(int x,int y); BYTE GetB(int x,int y); void SetR(int x,int y, BYTE value); void SetG(int x,int y, BYTE value); void SetB(int x,int y, BYTE value); void Threshold(int value); void Grayscale(); void Rotate(double degree); }; void uImage::GetImage(CDC *MemDC) { pBitmap =MemDC->GetCurrentBitmap(); pBitmap->GetBitmapBits(bitDepth*width_x*height_y,array); } void uImage::SetImage(CDC *MemDC) { pBitmap =MemDC->GetCurrentBitmap(); pBitmap->SetBitmapBits(bitDepth*width_x*height_y,array); } void uImage::Create(int x,int y, int depth) { bitDepth=depth/8; imgDepth=depth; array=new BYTE[x*y*bitDepth]; array_copy=new BYTE[x*y*bitDepth]; height_y=y; width_x=x; } void uImage::Delete() { delete[] array; delete[] array_copy; } BYTE uImage::GetR(int x, int y) { return array[(x+y*width_x)*bitDepth+2]; } BYTE uImage::GetG(int x, int y) { return array[(x+y*width_x)*bitDepth+1]; } BYTE uImage::GetB(int x, int y) { return array[(x+y*width_x)*bitDepth+0]; } void uImage::SetR(int x, int y, BYTE value) { array[(x+y*width_x)*bitDepth+2]=value; } void uImage::SetG(int x, int y, BYTE value) { array[(x+y*width_x)*bitDepth+1]=value; } void uImage::SetB(int x, int y, BYTE value) { array[(x+y*width_x)*bitDepth+0]=value; } void uImage::Threshold(int value) { BYTE gray; int temp; for(int i=0;i

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      in what way ? do you really need to create the array_copy when you create your object ? maybe delay it when you actually need it. maybe put array and array_copy in private part of the class ? when you rotate an image, me think the size of the array needs to be changed ? no ?


      Maximilien Lincourt Your Head A Splode - Strong Bad

      U 1 Reply Last reply
      0
      • M Maximilien

        in what way ? do you really need to create the array_copy when you create your object ? maybe delay it when you actually need it. maybe put array and array_copy in private part of the class ? when you rotate an image, me think the size of the array needs to be changed ? no ?


        Maximilien Lincourt Your Head A Splode - Strong Bad

        U Offline
        U Offline
        uus99
        wrote on last edited by
        #3

        I want to improve the code, to speed it up. I mean, perhaps using some MMX, more pointers etc to really speed the code... The .create will only be executed once, so creating an array_copy is ok there, other functions like Threshold, Grayscale, Rotate will be called over and over again, and if i declare array_copy there will cause overhead. Yeah, you are right, i'll put array & array_copy in private. The size of the image need not be changed, coz anything that goes beyond the current size of the image, i will ommit. So, how can i speed the code to be faster? Can you see any bottlenecks anywhere? Thx

        R 1 Reply Last reply
        0
        • U uus99

          I want to improve the code, to speed it up. I mean, perhaps using some MMX, more pointers etc to really speed the code... The .create will only be executed once, so creating an array_copy is ok there, other functions like Threshold, Grayscale, Rotate will be called over and over again, and if i declare array_copy there will cause overhead. Yeah, you are right, i'll put array & array_copy in private. The size of the image need not be changed, coz anything that goes beyond the current size of the image, i will ommit. So, how can i speed the code to be faster? Can you see any bottlenecks anywhere? Thx

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          Nothing really stands out to me from a quick look. However, one thing you could improve - in the Rotate function the array indexes are this : array_copy[(xd+yd*width_x)*bitDepth]=array[(xs+ys*width_x)*bitDepth]; the values (xd+yd*width_x)*bitDepth and (xs+ys*width_x)*bitDepth are computed three times per loop. You could save those into temporary variables and speed things up a bit. Also, the code appears garbled for the loop terms. You could possibly save any factors from the outer loop so that they are not recalculated for each term of the inner loop. These may seem minor but when they are done hundreds or thousands of times per pass they can add up. a two cent stamp short of going postal.

          U 1 Reply Last reply
          0
          • R Rick York

            Nothing really stands out to me from a quick look. However, one thing you could improve - in the Rotate function the array indexes are this : array_copy[(xd+yd*width_x)*bitDepth]=array[(xs+ys*width_x)*bitDepth]; the values (xd+yd*width_x)*bitDepth and (xs+ys*width_x)*bitDepth are computed three times per loop. You could save those into temporary variables and speed things up a bit. Also, the code appears garbled for the loop terms. You could possibly save any factors from the outer loop so that they are not recalculated for each term of the inner loop. These may seem minor but when they are done hundreds or thousands of times per pass they can add up. a two cent stamp short of going postal.

            U Offline
            U Offline
            uus99
            wrote on last edited by
            #5

            You are right, i can cut some time there, thanks. Anything else?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups