home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / os / mswindo / programm / misc / 4549 < prev    next >
Encoding:
Text File  |  1993-01-02  |  6.3 KB  |  214 lines

  1. Newsgroups: comp.os.ms-windows.programmer.misc
  2. Path: sparky!uunet!cs.utexas.edu!torn!nott!emr1!jagrant
  3. From: jagrant@emr1.emr.ca (John Grant)
  4. Subject: results of tests to draw grid files as images
  5. Message-ID: <1993Jan2.234133.28713@emr1.emr.ca>
  6. Organization: Energy, Mines, and Resources, Ottawa
  7. Date: Sat, 2 Jan 1993 23:41:33 GMT
  8. Lines: 204
  9.  
  10. I posted a question recently, got a few ideas, but I wasn't
  11. overwhelmed with responses.  So, I rolled up my sleeves and
  12. wrote some code to explore.  I thought I'd share the results
  13. with the group...
  14.  
  15.     Results of Tests with Drawing Grid Files
  16.     ----------------------------------------
  17.  
  18. Requirement
  19. -----------
  20.   I have a floating point grid file (336 columns X 807 rows).
  21. I want to draw it as an image file, converting the float values
  22. to colours on the fly: converting it to an image file first is 
  23. not an option for this application.  I also want to be able to 
  24. show it on a 16-colour VGA by taking advantage of Windows 3.1 
  25. dithered colours - i.e. as much device independence as I can manage.
  26.  
  27. 1. First I tried Rectangle(), but it was slow.  There are a zillion
  28.    brush create/select/de-select/delete operations.  In addition,
  29.    Rectangle has to deal with both a brush and a pen (even if you
  30.    select NULL_PEN, I guess).
  31.  
  32. 2. Next I tried PatBlt which was I faster, but still slow.  Well
  33.    maybe it is the brush select/de-select operations? So...
  34.  
  35. 3. Tried FillRect which doesn't require that the brush be selected
  36.    first.  However, there must be some sort of implicit brush
  37.    selection internally.
  38.  
  39. 4. Ok, despite what I have read about "...limited GDI resources..."
  40.    (which was why I started with the first 3 methods), I'll try 
  41.    pre-creating a table of 256 brushes to eliminate the overhead 
  42.    of creating and deleting them.  Wow!  Now we're cookin!
  43.  
  44. 5.  Last combination to complete the set.  Back to PatBlt
  45.     but this time with a brush table.  Well, well, well.
  46.     Guess which method I'm going to use!
  47.  
  48. Summary
  49. =======
  50.     |           | No. of brush     | No. of brush
  51. Time(s) | Method    | Create & Delete  | Select & de-Select
  52. --------+-----------+------------------+---------------------
  53. 454     | Rectangle |   336 X 807 X 2  |   336 X 807 X 2
  54. 430     | PatBlt    |   336 X 807 X 2  |   336 X 807 X 2
  55. 438     | FillRect  |   336 X 807 X 2  |   0 *
  56. 133     | FillRect  |   256            |   0 *
  57. 107     | PatBlt    |   256            |   336 X 807
  58. --------+-----------+------------------+---------------------
  59.             * no explicit SelectObject(), but
  60.               FillRect probably does it internally?
  61.  
  62. System Information:
  63.     25 MHz 80386 + 80387 
  64.     ATI VGA Wonder 512k (in VGA mode)
  65.     200 Mb disk 96% full
  66.     8 Mb memory
  67.     Windows 3.1
  68.     DOS 5.0
  69.     Borland C++ 3.1
  70.  
  71. Right now, most of you are saying "Idiot, I could have told you
  72. that most of the overhead was in creating & deleting the brushes!".
  73. Well, I asked for ideas and got a few, but nobody told me much.
  74. So, now we all know.
  75.  
  76. Hope some of you find this of interest.  If you have any ideas on
  77. speeding it up even more, I sure would be interested in hearing
  78. them!
  79.  
  80. Happy New Year!  May all your bits be 32 in 1993!
  81.  
  82. John A. Grant                        jagrant@emr1.emr.ca
  83. Airborne Geophysics
  84. Geological Survey of Canada, Ottawa
  85.  
  86. ============================my test code=================================
  87. #define NCOLOUR 256
  88. #define SCALE(v) min(max(       \
  89.         (v-minvalue)*(NCOLOUR-1)/(maxvalue-minvalue) \
  90.         ,0),NCOLOUR-1)
  91. #define COLOUR RGB(xxx,0,0)
  92.  
  93. void DrawGrid(HWND hwnd,char *pathname,int ncol,int nrow,float undefined,
  94.         float minvalue,float maxvalue)
  95. {
  96. FILE *f;
  97. float *value;
  98. int col,row;
  99. int xxx;
  100. HDC hdc;
  101. RECT rect;
  102. HBRUSH hbrush,brushtable[NCOLOUR];
  103.  
  104.     value=(float *)malloc(nrow*sizeof(float));
  105.     f=fopen(pathname,"rb");
  106.  
  107.     hdc=GetDC(hwnd);
  108.  
  109. //method 1: Rectangle, many brush create/delete, many brush select/de-select
  110.     SelectObject(hdc,GetStockObject(NULL_PEN));
  111.     fseek(f,0,SEEK_SET);
  112.     for(col=0;col<ncol;col++){
  113.       fread(value,sizeof(float),nrow,f);
  114.       for(row=0;row<nrow;row++){
  115.         if(value[row]!=undefined){
  116.            xxx=SCALE(value[row]);
  117.            hbrush=SelectObject(hdc,CreateSolidBrush(COLOUR));
  118.            Rectangle(hdc,col,row,col+2,row+2);
  119.            DeleteObject(SelectObject(hdc,hbrush));
  120.         }
  121.       }
  122.     }
  123.     MessageBeep(-1);
  124.  
  125. //method 2: PatBlt, many brush create/delete, many brush select/de-select
  126.     fseek(f,0,SEEK_SET);
  127.     for(col=0;col<ncol;col++){
  128.       fread(value,sizeof(float),nrow,f);
  129.       for(row=0;row<nrow;row++){
  130.         if(value[row]!=undefined){
  131.            xxx=SCALE(value[row]);
  132.            hbrush=SelectObject(hdc,CreateSolidBrush(COLOUR));
  133.            PatBlt(hdc,col,row,1,1,PATCOPY);
  134.            DeleteObject(SelectObject(hdc,hbrush));
  135.         }
  136.       }
  137.     }
  138.     MessageBeep(-1);
  139.  
  140. //method 3: FillRect, many brush create/delete, no brush select/de-select
  141.     fseek(f,0,SEEK_SET);
  142.     for(col=0;col<ncol;col++){
  143.       fread(value,sizeof(float),nrow,f);
  144.       for(row=0;row<nrow;row++){
  145.         if(value[row]!=undefined){
  146.            xxx=SCALE(value[row]);
  147.            rect.left=  col;
  148.            rect.right= col+1;
  149.            rect.top=   row;
  150.            rect.bottom=row+1;
  151.            hbrush=CreateSolidBrush(COLOUR);
  152.            FillRect(hdc,&rect,hbrush);
  153.            DeleteObject(hbrush);
  154.         }
  155.       }
  156.     }
  157.     MessageBeep(-1);
  158.  
  159. //method 4: FillRect & brush table, no brush select/de-select
  160.     fseek(f,0,SEEK_SET);
  161.     for(xxx=0;xxx<NCOLOUR;xxx++){
  162.       brushtable[xxx]=CreateSolidBrush(COLOUR);
  163.     }       
  164.     for(col=0;col<ncol;col++){
  165.       fread(value,sizeof(float),nrow,f);
  166.       for(row=0;row<nrow;row++){
  167.         if(value[row]!=undefined){
  168.            xxx=SCALE(value[row]);
  169.            rect.left=  col;
  170.            rect.right= col+1;
  171.            rect.top=   row;
  172.            rect.bottom=row+1;
  173.            FillRect(hdc,&rect,brushtable[xxx]);
  174.         }
  175.       }
  176.     }
  177.     for(xxx=0;xxx<NCOLOUR;xxx++){
  178.       DeleteObject(brushtable[xxx]);
  179.     }       
  180.     MessageBeep(-1);
  181.  
  182. //method 5: PatBlt & brush table, many brush select, no brush de-select
  183.     fseek(f,0,SEEK_SET);
  184.     for(xxx=0;xxx<NCOLOUR;xxx++){
  185.       brushtable[xxx]=CreateSolidBrush(COLOUR);
  186.     }
  187.     for(col=0;col<ncol;col++){
  188.       fread(value,sizeof(float),nrow,f);
  189.       for(row=0;row<nrow;row++){
  190.         if(value[row]!=undefined){
  191.            xxx=SCALE(value[row]);
  192.            SelectObject(hdc,brushtable[xxx]);
  193.            PatBlt(hdc,col,row,1,1,PATCOPY);
  194.         }
  195.       }
  196.     }
  197.     SelectObject(hdc,GetStockObject(NULL_BRUSH));
  198.     for(xxx=0;xxx<NCOLOUR;xxx++){
  199.       DeleteObject(brushtable[xxx]);
  200.     }       
  201.     MessageBeep(-1);
  202.  
  203.     ReleaseDC(hwnd,hdc);
  204.  
  205.     fclose(f);
  206.     free(value);
  207.  
  208.     return;
  209. }
  210. -- 
  211. John A. Grant                        jagrant@emr1.emr.ca
  212. Airborne Geophysics
  213. Geological Survey of Canada, Ottawa
  214.