Team Ai
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes847downloads
GrBitMap.txt2002 linesDownload Raw Back to Gr
1 2#help_index "Graphics"3 4Option(OPTf_WARN_HEADER_MISMATCH,OFF);5public Bool GrPlot0(CDC *dc=gr.dc,I64 x,I64 y)6{//2D. No clipping or transformation or thick.7  U8 *dst;8  I32 *db;9  I64 d,dist;10  CColorROPU32 c,color=dc->color,bkcolor=dc->bkcolor;11 12  if (dc->flags & DCF_LOCATE_NEAREST) {13    dist=DistSqrI64(x,y,dc->cur_x,dc->cur_y);14    if (dist<=dc->nearest_dist)15      dc->nearest_dist=dist;16  }17  if (dc->flags & DCF_RECORD_EXTENTS) {18    if (x<dc->min_x) dc->min_x=x;19    if (x>dc->max_x) dc->max_x=x;20    if (y<dc->min_y) dc->min_y=y;21    if (y>dc->max_y) dc->max_y=y;22  }23  if (dc->flags & DCF_DONT_DRAW)24    return TRUE;25  d=dc->width_internal*y+x;26  if (db=dc->depth_buf) {27    db+=d;28    if (0<=dc->db_z<=*db)29      *db=dc->db_z;30    else31      return TRUE;32  }33  if (color.c1.rop&(ROPBF_DITHER|ROPBF_PROBABILITY_DITHER)) {34    if (color.c1.rop&ROPBF_PROBABILITY_DITHER) {35      if (RandU16<dc->dither_probability_u16) {36        color.c1.rop=color.c0.rop;37        color.c0=color.c1;38      }39    } else {40      if ((x^y)&1) {41        color.c1.rop=color.c0.rop;42        color.c0=color.c1;43      }44    }45  }46  dst=dc->body+d;47  switch [color.c0.rop] {48    case ROPB_EQU:49    case ROPB_MONO:50      *dst=color.c0.color;51      break;52    case ROPB_COLLISION:53      c=*dst;54      if (c!=TRANSPARENT && c!=bkcolor.c0.color)55        dc->collision_cnt++;56      break;57    case ROPB_XOR:58      *dst^=color.c0.color;59      break;60  }61  return TRUE;62}63Option(OPTf_WARN_HEADER_MISMATCH,ON);64 65public I64 GrPeek0(CDC *dc=gr.dc,I64 x,I64 y)66{//2D. No clipping or transformation.67  return dc->body[dc->width_internal*y+x];68}69 70#help_index "Graphics;Graphics/Device Contexts"71 72public I64 GrBlot(CDC *dc=gr.dc,I64 x,I64 y,CDC *img)73{//2D. Clipping but not transformation..74  I64 i,j,k,k1,kk,kk1,w1,h1,w2,h2,dist,75        leading_pixels,leading_pixel_mask,whole_I64s,76        trailing_pixels,trailing_pixel_mask,77        reg bit_shift,win_z_buf_line_inc,win_z_buf_line_dec,win_z_num,78        color_mask;79  U8 reg *dst,*src;80  I32 *db;81  U16 reg *win_z_buf_ptr;82  CColorROPU32 color,c,old_color;83  CTask *win_task;84 85  if (dc->flags & DCF_SCRN_BITMAP) {86    win_task=dc->win_task;87    x+=win_task->scroll_x;88    y+=win_task->scroll_y;89  }90  if (x<0)91    w1=-x;92  else93    w1=0;94  if (y<0)95    h1=-y;96  else97    h1=0;98  w2=img->width;99  h2=img->height;100  if (dc->flags & DCF_SCRN_BITMAP) {101    x+=win_task->pix_left;102    y+=win_task->pix_top;103  }104  if (dc->flags & DCF_LOCATE_NEAREST) {105    dist=DistSqrI64(x+img->width>>1,y+img->height>>1,dc->cur_x,dc->cur_y);106    if (dist<=dc->nearest_dist)107      dc->nearest_dist=dist;108  }109  if (dc->flags & DCF_SCRN_BITMAP) {110    if (x+w1<0) w1=-x;111    if (x+w2>win_task->pix_right+1)112      w2=win_task->pix_right+1-x;113 114    if (y+h1<0) h1=-y;115    if (y+h2>win_task->pix_bottom+1)116      h2=win_task->pix_bottom+1-y;117  }118  if (x+w2>dc->width)119    w2=dc->width-x;120  if (y+h2>dc->height)121    h2=dc->height-y;122  if (w1<w2<=img->width && h1<h2<=img->height) {123    if (dc->flags & DCF_RECORD_EXTENTS) {124      if (x+w1<dc->min_x) dc->min_x=x+w1;125      if (x+w2-1>dc->max_x) dc->max_x=x+w2-1;126      if (y+h1<dc->min_y) dc->min_y=y+h1;127      if (y+h2-1>dc->max_y) dc->max_y=y+h2-1;128    }129    if (dc->flags & DCF_DONT_DRAW)130      return 1;131    old_color=dc->color;132    db=dc->depth_buf;133    dc->depth_buf=NULL;134    dc->color&=~ROPF_DITHER;135    color=dc->color;136    leading_pixels=-(w1+x)&7;137    leading_pixel_mask=gr.to_8_bits[0xFF>>leading_pixels];138    bit_shift=-x&7;139    whole_I64s=(w2-w1-leading_pixels)>>3;140    if (whole_I64s<0) whole_I64s=0;141    trailing_pixels=(x+w2)&7;142    trailing_pixel_mask=gr.to_8_bits[0xFF<<trailing_pixels&0xFF];143    if (leading_pixels+trailing_pixels>w2-w1) {144      leading_pixel_mask|=trailing_pixel_mask;145      trailing_pixels=0;146    }147    switch (color.c0.rop) {148      case ROPB_COLLISION: //TODO: Might want to check win_z_buf149        color =dc->bkcolor.c0.color;150        k=h1*img->width_internal;151        k1=(h1+y)*dc->width_internal+x;152        for (j=h2-h1;j;j--) {153          for (i=w1;i<w2;i++) {154            c=dc->body[k1+i];155            if (c!=TRANSPARENT&&c!=color&&img->body[k+i]!=TRANSPARENT)156              dc->collision_cnt++;157          }158          k+=img->width_internal;159          k1+=dc->width_internal;160        }161        break;162      case ROPB_MONO:163        color_mask=gr.to_8_colors[color.c0.color];164        if (img->flags&DCF_NO_TRANSPARENTS) {165          if (!(dc->flags & DCF_SCRN_BITMAP) || dc->flags&DCF_ON_TOP)166            win_z_buf_ptr=NULL;167          else {168            win_z_num=win_task->win_z_num;169            win_z_buf_ptr=gr.win_z_buf(U8 *)+((h1+y)/FONT_HEIGHT*TEXT_COLS+170                  (w1+x)/FONT_WIDTH)*sizeof(U16);171            win_z_buf_line_dec=whole_I64s;172            if (leading_pixels)173              win_z_buf_line_dec++;174            if (trailing_pixels)175              win_z_buf_line_dec++;176            win_z_buf_line_dec*=sizeof(U16);177            win_z_buf_line_inc=TEXT_COLS*sizeof(U16)-win_z_buf_line_dec;178          }179          kk = h1   *img ->width_internal+w1;180          kk1=(h1+y)*dc->width_internal+x+w1;181          kk =(kk-bit_shift)&~7+bit_shift;182          bit_shift*=8;183          if (win_z_buf_ptr)184            for (j=h1;j<h2;j++) {185              src=img->body+kk&~7;186              dst=dc->body+kk1&~7;187              if (leading_pixels) {188                if (win_z_num>=*win_z_buf_ptr++) {189                  if (bit_shift)190                    *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|191                          (*src(U64 *)++>>bit_shift|192                          *src(I64 *)<<(64-bit_shift))&193                          ~leading_pixel_mask&color_mask;194                  else195                    *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|196                          *src(I64 *)++&~leading_pixel_mask&color_mask;197                } else {198                  src(I64 *)++;199                  dst(I64 *)++;200                }201              }202              if (bit_shift)203                for (i=0;i<whole_I64s;i++)204                  if (win_z_num>=*win_z_buf_ptr++)205                    *dst(I64 *)++=(*src(U64 *)++>>bit_shift|206                          *src(I64 *)<<(64-bit_shift))&color_mask;207                  else {208                    src(I64 *)++;209                    dst(I64 *)++;210                  }211              else212                for (i=0;i<whole_I64s;i++)213                  if (win_z_num>=*win_z_buf_ptr++)214                    *dst(I64 *)++=*src(I64 *)++&color_mask;215                  else {216                    src(I64 *)++;217                    dst(I64 *)++;218                  }219              if (trailing_pixels && win_z_num>=*win_z_buf_ptr++) {220                if (bit_shift)221                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|222                        (*src(U64 *)++>>bit_shift|223                        *src(I64 *)<<(64-bit_shift))&224                        ~trailing_pixel_mask&color_mask;225                else226                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|227                        *src(I64 *)++&~trailing_pixel_mask&color_mask;228              }229              kk +=img->width_internal;230              kk1+=dc->width_internal;231              if ((j+y)&7==7)232                win_z_buf_ptr(U8 *)+=win_z_buf_line_inc;233              else234                win_z_buf_ptr(U8 *)-=win_z_buf_line_dec;235            }236          else237            for (j=h2-h1;j;j--) {238              src=img->body+kk&~7;239              dst=dc->body+kk1&~7;240              if (leading_pixels) {241                if (bit_shift)242                  *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|243                        (*src(U64 *)++>>bit_shift|244                        *src(I64 *)<<(64-bit_shift))&245                        ~leading_pixel_mask&color_mask;246                else247                  *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|248                        *src(I64 *)++&~leading_pixel_mask&color_mask;249              }250              if (bit_shift)251                for (i=0;i<whole_I64s;i++)252                  *dst(I64 *)++=(*src(U64 *)++>>bit_shift|253                        *src(I64 *)<<(64-bit_shift))&color_mask;254              else255                for (i=0;i<whole_I64s;i++)256                  *dst(I64 *)++=*src(I64 *)++&color_mask;257 258              if (trailing_pixels) {259                if (bit_shift)260                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|261                        (*src(U64 *)++>>bit_shift|262                        *src(I64 *)<<(64-bit_shift))&263                        ~trailing_pixel_mask&color_mask;264                else265                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|266                        *src(I64 *)++&~trailing_pixel_mask&color_mask;267              }268              kk +=img->width_internal;269              kk1+=dc->width_internal;270            }271        } else {272          k=h1*img->width_internal;273          if (!(dc->flags & DCF_SCRN_BITMAP) || dc->flags&DCF_ON_TOP) {274            for (j=h1;j<h2;j++) {275              for (i=w1;i<w2;i++)276                if (img->body[k+i])277                  GrPlot0(dc,x+i,y+j);278              k+=img->width_internal;279            }280          } else {281            win_z_num           =win_task->win_z_num;282            win_z_buf_ptr       =gr.win_z_buf(U8 *)+283                  ((h1+y)/FONT_HEIGHT*TEXT_COLS+(w1+x)/FONT_WIDTH)*sizeof(U16);284            win_z_buf_line_dec=whole_I64s;285            if (leading_pixels)286              win_z_buf_line_dec++;287            if (trailing_pixels)288              win_z_buf_line_dec++;289            win_z_buf_line_dec*=sizeof(U16);290            win_z_buf_line_inc=TEXT_COLS*sizeof(U16)-win_z_buf_line_dec;291            for (j=h1;j<h2;j++) {292              if (win_z_num>=*win_z_buf_ptr++)293                color_mask=TRUE;294              else295                color_mask=FALSE;296              for (i=w1;i<w2;) {297                if (color_mask)298                  if (img->body[k+i])299                    GrPlot0(dc,x+i,y+j);300                if (!((++i+x) &7) && i<w2) {301                  if (win_z_num>=*win_z_buf_ptr++)302                    color_mask=TRUE;303                  else304                    color_mask=FALSE;305                }306              }307              if ((j+y)&7==7)308                win_z_buf_ptr(U8 *)+=win_z_buf_line_inc;309              else310                win_z_buf_ptr(U8 *)-=win_z_buf_line_dec;311              k+=img->width_internal;312            }313          }314        }315        break;316      case ROPB_EQU:317        if (img->flags&DCF_NO_TRANSPARENTS) {318          if (!(dc->flags & DCF_SCRN_BITMAP) || dc->flags&DCF_ON_TOP)319            win_z_buf_ptr=NULL;320          else {321            win_z_num=win_task->win_z_num;322            win_z_buf_ptr=gr.win_z_buf(U8 *)+323                  ((h1+y)/FONT_HEIGHT*TEXT_COLS+(w1+x)/FONT_WIDTH)*sizeof(U16);324            win_z_buf_line_dec=whole_I64s;325            if (leading_pixels)326              win_z_buf_line_dec++;327            if (trailing_pixels)328              win_z_buf_line_dec++;329            win_z_buf_line_dec*=sizeof(U16);330            win_z_buf_line_inc=TEXT_COLS*sizeof(U16)-win_z_buf_line_dec;331          }332          kk = h1   *img ->width_internal+w1;333          kk1=(h1+y)*dc->width_internal+x+w1;334          kk =(kk-bit_shift)&~7+bit_shift;335          bit_shift*=8;336          if (win_z_buf_ptr)337            for (j=h1;j<h2;j++) {338              src=img->body+kk&~7;339              dst=dc->body+kk1&~7;340              if (leading_pixels) {341                if (win_z_num>=*win_z_buf_ptr++) {342                  if (bit_shift)343                    *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|344                          (*src(U64 *)++>>bit_shift|345                          *src(I64 *)<<(64-bit_shift))&~leading_pixel_mask;346                  else347                    *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|348                          *src(I64 *)++&~leading_pixel_mask;349                } else {350                  src(I64 *)++;351                  dst(I64 *)++;352                }353              }354              if (bit_shift)355                for (i=0;i<whole_I64s;i++)356                  if (win_z_num>=*win_z_buf_ptr++)357                    *dst(I64 *)++=*src(U64 *)++>>bit_shift|358                          *src(I64 *)<<(64-bit_shift);359                  else {360                    src(I64 *)++;361                    dst(I64 *)++;362                  }363              else364                for (i=0;i<whole_I64s;i++)365                  if (win_z_num>=*win_z_buf_ptr++)366                    *dst(I64 *)++=*src(I64 *)++;367                  else {368                    src(I64 *)++;369                    dst(I64 *)++;370                  }371              if (trailing_pixels && win_z_num>=*win_z_buf_ptr++) {372                if (bit_shift)373                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|374                        (*src(U64 *)++>>bit_shift|375                        *src(I64 *)<<(64-bit_shift))&~trailing_pixel_mask;376                else377                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|378                        *src(I64 *)++&~trailing_pixel_mask;379              }380              kk +=img->width_internal;381              kk1+=dc->width_internal;382              if ((j+y)&7==7)383                win_z_buf_ptr(U8 *)+=win_z_buf_line_inc;384              else385                win_z_buf_ptr(U8 *)-=win_z_buf_line_dec;386            }387          else388            for (j=h2-h1;j;j--) {389              src=img->body+kk&~7;390              dst=dc->body+kk1&~7;391              if (leading_pixels) {392                if (bit_shift)393                  *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|394                        (*src(U64 *)++>>bit_shift|395                        *src(I64 *)<<(64-bit_shift))&~leading_pixel_mask;396                else397                  *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|398                        *src(I64 *)++&~leading_pixel_mask;399              }400              if (bit_shift)401                for (i=0;i<whole_I64s;i++)402                  *dst(I64 *)++=*src(U64 *)++>>bit_shift|403                        *src(I64 *)<<(64-bit_shift);404              else405                for (i=0;i<whole_I64s;i++)406                  *dst(I64 *)++=*src(I64 *)++;407 408              if (trailing_pixels) {409                if (bit_shift)410                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|411                        (*src(U64 *)++>>bit_shift|412                        *src(I64 *)<<(64-bit_shift))&~trailing_pixel_mask;413                else414                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|415                        *src(I64 *)++&~trailing_pixel_mask;416              }417              kk +=img->width_internal;418              kk1+=dc->width_internal;419            }420        } else {421here1a:422          k=h1*img->width_internal;423          if (!(dc->flags & DCF_SCRN_BITMAP) || dc->flags&DCF_ON_TOP) {424            for (j=h1;j<h2;j++) {425              for (i=w1;i<w2;i++) {426                c=img->body[k+i];427                if (c!=TRANSPARENT) {428                  dc->color.c0.color=c;429                  GrPlot0(dc,x+i,y+j);430                }431              }432              k+=img->width_internal;433            }434          } else {435            win_z_num           =win_task->win_z_num;436            win_z_buf_ptr       =gr.win_z_buf(U8 *)+437                  ((h1+y)/FONT_HEIGHT*TEXT_COLS+(w1+x)/FONT_WIDTH)*sizeof(U16);438            win_z_buf_line_dec=whole_I64s;439            if (leading_pixels)440              win_z_buf_line_dec++;441            if (trailing_pixels)442              win_z_buf_line_dec++;443            win_z_buf_line_dec*=sizeof(U16);444            win_z_buf_line_inc=TEXT_COLS*sizeof(U16)-win_z_buf_line_dec;445            for (j=h1;j<h2;j++) {446              if (win_z_num>=*win_z_buf_ptr++)447                color_mask=TRUE;448              else449                color_mask=FALSE;450              for (i=w1;i<w2;) {451                if (color_mask) {452                  c=img->body[k+i];453                  if (c!=TRANSPARENT) {454                    dc->color.c0.color=c;455                    GrPlot0(dc,x+i,y+j);456                  }457                }458                if (!((++i+x) &7) && i<w2) {459                  if (win_z_num>=*win_z_buf_ptr++)460                    color_mask=TRUE;461                  else462                    color_mask=FALSE;463                }464              }465              if ((j+y)&7==7)466                win_z_buf_ptr(U8 *)+=win_z_buf_line_inc;467              else468                win_z_buf_ptr(U8 *)-=win_z_buf_line_dec;469              k+=img->width_internal;470            }471          }472          dc->color=color;473        }474        break;475      case ROPB_XOR:476        if (img->flags&DCF_NO_TRANSPARENTS) {477          if (!(dc->flags & DCF_SCRN_BITMAP) || dc->flags&DCF_ON_TOP)478            win_z_buf_ptr=NULL;479          else {480            win_z_num=win_task->win_z_num;481            win_z_buf_ptr=gr.win_z_buf(U8 *)+482                  ((h1+y)/FONT_HEIGHT*TEXT_COLS+(w1+x)/FONT_WIDTH)*sizeof(U16);483            win_z_buf_line_dec=whole_I64s;484            if (leading_pixels)485              win_z_buf_line_dec++;486            if (trailing_pixels)487              win_z_buf_line_dec++;488            win_z_buf_line_dec*=sizeof(U16);489            win_z_buf_line_inc=TEXT_COLS*sizeof(U16)-win_z_buf_line_dec;490          }491          kk = h1   *img ->width_internal  +w1;492          kk1=(h1+y)*dc->width_internal+x+w1;493          kk =(kk-bit_shift)&~7+bit_shift;494          bit_shift*=8;495          if (win_z_buf_ptr)496            for (j=h1;j<h2;j++) {497              src=img->body+kk&~7;498              dst=dc->body+kk1&~7;499              if (leading_pixels) {500                if (win_z_num>=*win_z_buf_ptr++) {501                  if (bit_shift)502                    *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|503                          (*dst(I64 *)^(*src(U64 *)++>>bit_shift|504                          *src(I64 *)<<(64-bit_shift)))&~leading_pixel_mask;505                  else506                    *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|507                          (*dst(I64 *)^*src(I64 *)++)&~leading_pixel_mask;508                } else {509                  src(I64 *)++;510                  dst(I64 *)++;511                }512              }513              if (bit_shift)514                for (i=0;i<whole_I64s;i++)515                  if (win_z_num>=*win_z_buf_ptr++)516                    *dst(I64 *)++^=*src(U64 *)++>>bit_shift|517                          *src(I64 *)<<(64-bit_shift);518                  else {519                    src(I64 *)++;520                    dst(I64 *)++;521                  }522              else523                for (i=0;i<whole_I64s;i++)524                  if (win_z_num>=*win_z_buf_ptr++)525                    *dst(I64 *)++^=*src(I64 *)++;526                  else {527                    src(I64 *)++;528                    dst(I64 *)++;529                  }530              if (trailing_pixels && win_z_num>=*win_z_buf_ptr++) {531                if (bit_shift)532                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|533                        (*dst(I64 *)^(*src(U64 *)++>>bit_shift|534                        *src(I64 *)<<(64-bit_shift)))&~trailing_pixel_mask;535                else536                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|537                        (*dst(I64 *)^*src(I64 *)++)&~trailing_pixel_mask;538              }539              kk +=img->width_internal;540              kk1+=dc->width_internal;541              if ((j+y)&7==7)542                win_z_buf_ptr(U8 *)+=win_z_buf_line_inc;543              else544                win_z_buf_ptr(U8 *)-=win_z_buf_line_dec;545            }546          else547            for (j=h2-h1;j;j--) {548              src=img->body+kk&~7;549              dst=dc->body+kk1&~7;550              if (leading_pixels) {551                if (bit_shift)552                  *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|553                        (*dst(I64 *)^(*src(U64 *)++>>bit_shift|554                        *src(I64 *)<<(64-bit_shift)))&~leading_pixel_mask;555                else556                  *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|557                        (*dst(I64 *)^*src(I64 *)++)&~leading_pixel_mask;558              }559              if (bit_shift)560                for (i=0;i<whole_I64s;i++)561                  *dst(I64 *)++^=*src(U64 *)++>>bit_shift|562                        *src(I64 *)<<(64-bit_shift);563              else564                for (i=0;i<whole_I64s;i++)565                  *dst(I64 *)++^=*src(I64 *)++;566              if (trailing_pixels) {567                if (bit_shift)568                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|569                        (*dst(I64 *)^(*src(U64 *)++>>bit_shift|570                        *src(I64 *)<<(64-bit_shift)))&~trailing_pixel_mask;571                else572                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|573                        (*dst(I64 *)^*src(I64 *)++)&~trailing_pixel_mask;574              }575              kk +=img->width_internal;576              kk1+=dc->width_internal;577            }578        } else579          goto here1a;580        break;581    }582    dc->depth_buf=db;583    dc->color=old_color;584    return 1;585  } else586    return 0;587}588 589#help_index "Graphics/Device Contexts"590 591U8 *GrBitMap4ToBitMap8(U8 *dst,U8 *src,I64 src_size,I64 bkcolor)592{593  I64 c,k,i=src_size*2,i1=i>>3;594  for (k=0;k<i;k++) {595    c=0;596    if (Bt(src     ,k)) c|=1;597    if (Bt(src+i1  ,k)) c|=2;598    if (Bt(src+i1*2,k)) c|=4;599    if (Bt(src+i1*3,k)) c|=8;600    if (c==bkcolor) c=TRANSPARENT;601    *dst++=c;602  }603  return dst;604}605 606U8 *GrBitMap1ToBitMap8(U8 *dst,U8 *src,I64 src_size,I64 bkcolor)607{608  I64 c,k,i=src_size*8;609  for (k=0;k<i;k++) {610    c=0;611    if (Bt(src,k))  c=COLOR_MONO;612    if (c==bkcolor) c=TRANSPARENT;613    *dst++=c;614  }615  return dst;616}617 618public CDC *DCExt(CDC *dc=gr.dc,I64 x1,I64 y1,I64 x2,I64 y2,619        CTask *task=NULL)620{//Extract new device context rect from device context.621  CDC *res;622  CTask *win_task;623  if (x1>x2) SwapI64(&x1,&x2);624  if (y1>y2) SwapI64(&y1,&y2);625  if (dc->flags & DCF_SCRN_BITMAP) {626    win_task=dc->win_task;627    x1+=win_task->pix_left+win_task->scroll_x;628    y1+=win_task->pix_top +win_task->scroll_y;629    x2+=win_task->pix_left+win_task->scroll_x;630    y2+=win_task->pix_top +win_task->scroll_y;631  }632  res=DCNew(x2-x1+1,y2-y1+1,task);633  DCFill(res);634  GrBlot(res,-x1,-y1,dc);635  return res;636}637 638#help_index "Graphics/Char;Char/Graphics"639 640public I64 GrPutChar(CDC *dc=gr.dc,I64 x,I64 y,U8 ch)641{//2D. Clipping but not transformation.642  U8 reg *src,reg *dst,*font_ptr;643  I64 i,m,leading_pixels,trailing_pixels,leading_pixel_mask,trailing_pixel_mask,644        j,k1,kk1,w1,h1,w2,h2,reg bit_shift,reg color_mask,dist;645  CColorROPU32 color,c;646  CTask *win_task;647 648  if (dc->flags & DCF_SCRN_BITMAP) {649    win_task=dc->win_task;650    x+=win_task->scroll_x;651    y+=win_task->scroll_y;652  }653 654  if (x<0)655    w1=-x;656  else657    w1=0;658  if (y<0)659    h1=-y;660  else661    h1=0;662  w2=FONT_WIDTH;663  h2=FONT_HEIGHT;664 665  if (dc->flags & DCF_SCRN_BITMAP) {666    x+=win_task->pix_left;667    y+=win_task->pix_top;668  }669  if (dc->flags & DCF_LOCATE_NEAREST) {670    dist=DistSqrI64(x+w2>>1,y+h2>>1,dc->cur_x,dc->cur_y);671    if (dist<=dc->nearest_dist)672      dc->nearest_dist=dist;673  }674  if (dc->flags & DCF_SCRN_BITMAP) {675    if (x+w1<0) w1=-x;676    if (x+w2>win_task->pix_right+1)677      w2=win_task->pix_right+1-x;678 679    if (y+h1<0) h1=-y;680    if (y+h2>win_task->pix_bottom+1)681      h2=win_task->pix_bottom+1-y;682  }683  if (x+w2>dc->width)684    w2=dc->width-x;685  if (y+h2>dc->height)686    h2=dc->height-y;687  if (w1<w2<=FONT_WIDTH && h1<h2<=FONT_HEIGHT) {688    if (dc->flags & DCF_RECORD_EXTENTS) {689      if (x+w1  <dc->min_x) dc->min_x=x+w1;690      if (x+w2-1>dc->max_x) dc->max_x=x+w2-1;691      if (y+h1  <dc->min_y) dc->min_y=y+h1;692      if (y+h2-1>dc->max_y) dc->max_y=y+h2-1;693    }694    if (dc->flags & DCF_DONT_DRAW)695      return 1;696    color=dc->color;697    leading_pixels=-(w1+x)&7;698    if (!leading_pixels) leading_pixels=8;699    leading_pixel_mask=gr.to_8_bits[0xFF>>leading_pixels];700    bit_shift=-x&7;701    trailing_pixels=(x+w2)&7;702    trailing_pixel_mask=gr.to_8_bits[0xFF<<trailing_pixels&0xFF];703    if (leading_pixels+trailing_pixels>w2-w1) {704      leading_pixel_mask|=trailing_pixel_mask;705      trailing_pixels=0;706    }707    font_ptr=&text.font(U8 *)[FONT_HEIGHT*ch+h1];708    if (color.c0.rop==ROPB_COLLISION) {709      m=w1&(FONT_WIDTH-1);710#assert FONT_WIDTH==8711      color =dc->bkcolor.c0.color;712      for (i=w1;i<w2;i++,m++) {713        k1=(h1+y)*dc->width_internal+x;714        src=font_ptr;715        for (j=h2-h1;j;j--) {716          c=dc->body[k1+i];717          if (c!=TRANSPARENT && c!=color && Bt(src,m))718            dc->collision_cnt++;719          k1+=dc->width_internal;720          src++;721        }722      }723    } else {724      color_mask=gr.to_8_colors[color.c0.color];725      k1=x+w1;726      kk1=(h1+y)*dc->width_internal+k1;727      if (!(dc->flags & DCF_SCRN_BITMAP) || dc->flags&DCF_ON_TOP) {728        if (leading_pixels) {729          dst=dc->body+kk1&~7;730          src=font_ptr;731          if (bit_shift)732            src--;733          switch [color.c0.rop] {734            case ROPB_EQU:735            case ROPB_MONO:736              for (j=h2-h1;j;j--) {737                m=gr.to_8_bits[*src(U16 *)>>bit_shift&0xFF];738                *dst(I64 *)=*dst(I64 *)&leading_pixel_mask|739                      (color_mask&m|*dst(I64 *)&~m)&~leading_pixel_mask;740                src++;741                dst+=dc->width_internal;742              }743              break;744            case ROPB_XOR:745              if (color_mask) {746                for (j=h2-h1;j;j--) {747                  *dst(I64 *)=*dst(I64 *)&leading_pixel_mask|748                        (*dst(I64 *)^gr.to_8_bits[*src(U16 *)>>bit_shift&0xFF])&749                        ~leading_pixel_mask;750                  src++;751                  dst+=dc->width_internal;752                }753              }754              break;755          }756          kk1+=8;757        }758        if (trailing_pixels) {759          dst=dc->body+kk1&~7;760          src=font_ptr+1;761          if (bit_shift)762            src--;763          switch [color.c0.rop] {764            case ROPB_EQU:765            case ROPB_MONO:766              for (j=h2-h1;j;j--) {767                m=gr.to_8_bits[*src(U16 *)>>bit_shift&0xFF];768                *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|769                      (color_mask&m|*dst(I64 *)&~m)&~trailing_pixel_mask;770                src++;771                dst+=dc->width_internal;772              }773              break;774            case ROPB_XOR:775              if (color_mask)776                for (j=h2-h1;j;j--) {777                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|778                        (*dst(I64 *)^gr.to_8_bits[*src(U16 *)>>bit_shift&0xFF])&779                        ~trailing_pixel_mask;780                  src++;781                  dst+=dc->width_internal;782                }783              break;784          }785        }786      } else {787        if (leading_pixels) {788          dst=dc->body+kk1&~7;789          src=font_ptr;790          if (bit_shift)791            src--;792          switch [color.c0.rop] {793            case ROPB_EQU:794            case ROPB_MONO:795              for (j=h1;j<h2;j++) {796                if (!IsPixCovered0(win_task,k1,y+j)) {797                  m=gr.to_8_bits[*src(U16 *)>>bit_shift&0xFF];798                  *dst(I64 *)=*dst(I64 *)&leading_pixel_mask|799                        (color_mask&m|*dst(I64 *)&~m)&~leading_pixel_mask;800                }801                src++;802                dst+=dc->width_internal;803              }804              break;805            case ROPB_XOR:806              if (color_mask)807                for (j=h1;j<h2;j++) {808                  if (!IsPixCovered0(win_task,k1,y+j))809                    *dst(I64 *)=*dst(I64 *)&leading_pixel_mask|810                          (*dst(I64 *)^gr.to_8_bits811                          [*src(U16 *)>>bit_shift&0xFF])&812                          ~leading_pixel_mask;813                  src++;814                  dst+=dc->width_internal;815                }816              break;817          }818          k1+=8;819          kk1+=8;820        }821        if (trailing_pixels) {822          dst=dc->body+kk1&~7;823          src=font_ptr+1;824          if (bit_shift)825            src--;826          switch [color.c0.rop] {827            case ROPB_EQU:828            case ROPB_MONO:829              for (j=h1;j<h2;j++) {830                if (!IsPixCovered0(win_task,k1,y+j)) {831                  m=gr.to_8_bits[*src(U16 *)>>bit_shift&0xFF];832                  *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|833                        (color_mask&m|*dst(I64 *)&~m)&~trailing_pixel_mask;834                }835                src++;836                dst+=dc->width_internal;837              }838              break;839            case ROPB_XOR:840              if (color_mask)841                for (j=h1;j<h2;j++) {842                  if (!IsPixCovered0(win_task,k1,y+j))843                    *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|844                          (*dst(I64 *)^gr.to_8_bits845                          [*src(U16 *)>>bit_shift&0xFF])&846                          ~trailing_pixel_mask;847                  src++;848                  dst+=dc->width_internal;849                }850              break;851          }852        }853      }854    }855    return 1;856  } else857    return 0;858}859 860I64 GrPutS(CDC *dc=gr.dc,I64 x,I64 y,U8 *_s)861{//Use GrPrint()862  I64 x0,sx=0,sy=0,res;863  if (!_s) return 0;864  x0=x;865  res=0;866  while (*_s) {867    if (*_s=='\n') {868      x=x0;869      y+=FONT_HEIGHT;870      _s++;871    } else if (*_s=='\t') {872      x=x0+CeilU64(x-x0+FONT_WIDTH,8*FONT_WIDTH);873      _s++;874    } else if (*_s(U32 *)=='$SY,') {875      if (_s[4]=='-') {876        _s++;877        sy='0'-_s[4];878      } else879        sy=_s[4]-'0';880      _s+=6;881    } else if (*_s(U32 *)=='$SX,') {882      if (_s[4]=='-') {883        _s++;884        sx='0'-_s[4];885      } else886        sx=_s[4]-'0';887      _s+=6;888    } else {889      res+=GrPutChar(dc,x+sx,y+sy,*_s);890      x+=FONT_WIDTH;891      _s++;892    }893  }894  return res;895}896 897I64 GrVPutS(CDC *dc=gr.dc,I64 x,I64 y,U8 *_s)898{//Vertical Text.  Use GrVPrint()899  I64 y0,sx=0,sy=0,res;900  U8 buf[2];901  if (!_s) return 0;902  y0=y;903  res=0;904  buf[1]=0;905  while (*_s) {906    if (*_s=='\n') {907      y=y0;908      x+=FONT_WIDTH;909      _s++;910    } else if (*_s=='\t') {911      y=y0+CeilU64(y-y0+FONT_HEIGHT,8*FONT_HEIGHT);912      _s++;913    } else if (*_s(U32 *)=='$SY,') {914      if (_s[4]=='-') {915        _s++;916        sx='0'-_s[4];917      } else918        sx=_s[4]-'0';919      _s+=6;920    } else if (*_s(U32 *)=='$SX,') {921      if (_s[4]=='-') {922        _s++;923        sy='0'-_s[4];924      } else925        sy=_s[4]-'0';926      _s+=6;927    } else {928      *buf=*_s++;929      res+=GrPutS(dc,x,y,buf);930      y+=FONT_HEIGHT;931    }932  }933  return res;934}935 936public I64 GrPrint(CDC *dc=gr.dc,I64 x,I64 y,U8 *fmt,...)937{//2D. Clipping but not transformation.938  I64 res;939  U8 *buf=StrPrintJoin(NULL,fmt,argc,argv);940  res=GrPutS(dc,x,y,buf);941  Free(buf);942  return res;943}944 945public I64 GrVPrint(CDC *dc=gr.dc,I64 x,I64 y,U8 *fmt,...)946{//2D. Vertical text. Clipping but not transformation.947  I64 res;948  U8 *buf=StrPrintJoin(NULL,fmt,argc,argv);949  res=GrVPutS(dc,x,y,buf);950  Free(buf);951  return res;952}953 954#help_index "Graphics"955public I64 GrRect(CDC *dc=gr.dc,I64 x,I64 y,I64 w,I64 h)956{//2D. Width Height. Clipping but not transformation.957//Returns cnt of pixs changed.958  I64 i,res=0,j,k1,kk1,w1,h1,w2,h2,dist,959        leading_pixels,original_leading_pixels,whole_I64s,960        trailing_pixels,leading_pixel_mask,trailing_pixel_mask,961        win_z_buf_line_inc,win_z_buf_line_dec,win_z_num,color_mask;962  U8 reg *dst;963  U16 reg *win_z_buf_ptr;964  CColorROPU32 color,c,dither_colors;965  Bool dither,probability_dither;966  CTask *win_task;967 968  if (dc->flags & DCF_SCRN_BITMAP) {969    win_task=dc->win_task;970    x+=win_task->scroll_x;971    y+=win_task->scroll_y;972  }973 974  if (x<0)975    w1=-x;976  else977    w1=0;978  if (y<0)979    h1=-y;980  else981    h1=0;982  w2=w;983  h2=h;984 985  if (dc->flags & DCF_SCRN_BITMAP) {986    x+=win_task->pix_left;987    y+=win_task->pix_top;988  }989  if (dc->flags & DCF_LOCATE_NEAREST) {//TODO:Untested990    if (x<=dc->cur_x<=x+w && y<=dc->cur_y<=y+h)991      dist=0;992    else993      dist=DistSqrI64(x+w>>1,y+h>>1,dc->cur_x,dc->cur_y);994    if (dist<=dc->nearest_dist)995      dc->nearest_dist=dist;996  }997  if (dc->flags & DCF_SCRN_BITMAP) {998    if (x+w1<0) w1=-x;999    if (x+w2>win_task->pix_right+1)1000      w2=win_task->pix_right+1-x;1001 1002    if (y+h1<0) h1=-y;1003    if (y+h2>win_task->pix_bottom+1)1004      h2=win_task->pix_bottom+1-y;1005  }1006  if (x+w2>dc->width)1007    w2=dc->width-x;1008  if (y+h2>dc->height)1009    h2=dc->height-y;1010  if (w1<w2<=w && h1<h2<=h) {1011    if (dc->flags & DCF_RECORD_EXTENTS) {1012      if (x+w1  <dc->min_x) dc->min_x=x+w1;1013      if (x+w2-1>dc->max_x) dc->max_x=x+w2-1;1014      if (y+h1  <dc->min_y) dc->min_y=y+h1;1015      if (y+h2-1>dc->max_y) dc->max_y=y+h2-1;1016    }1017    if (dc->flags & DCF_DONT_DRAW)1018      return TRUE;1019    color=dc->color;1020    if (color.c1.rop&(ROPBF_DITHER|ROPBF_PROBABILITY_DITHER)) {1021      dither=TRUE;1022      if (color.c1.rop&ROPBF_PROBABILITY_DITHER) {1023        probability_dither=TRUE;1024        color.c1.rop=color.c0.rop;1025        dither_colors=color;1026      } else {1027        probability_dither=FALSE;1028        color.c1.rop=color.c0.rop;1029      }1030    } else1031      dither=FALSE;1032    original_leading_pixels=leading_pixels=-(w1+x)&7;1033    leading_pixel_mask=gr.to_8_bits[0xFF>>leading_pixels];1034    whole_I64s=(w2-w1-leading_pixels)>>3;1035    if (whole_I64s<0) whole_I64s=0;1036    trailing_pixels=(x+w2)&7;1037    trailing_pixel_mask=gr.to_8_bits[0xFF<<trailing_pixels&0xFF];1038    if (leading_pixels+trailing_pixels>w2-w1) {1039      leading_pixel_mask|=trailing_pixel_mask;1040      leading_pixels=w2-w1; //Correct so it's right for res.1041      trailing_pixels=0;1042    }1043    if (color.c0.rop==ROPB_COLLISION) {//TODO: Might want to check win_z_buf1044      color =dc->bkcolor.c0.color;1045      k1=(h1+y)*dc->width_internal+x;1046      res=-dc->collision_cnt;1047      for (j=h2-h1;j;j--) {1048        for (i=w1;i<w2;i++) {1049          c=dc->body[k1+i];1050          if (c!=TRANSPARENT && c!=color)1051            dc->collision_cnt++;1052        }1053        k1+=dc->width_internal;1054      }1055      res+=dc->collision_cnt;1056    } else {1057      if (!(dc->flags & DCF_SCRN_BITMAP) || dc->flags&DCF_ON_TOP)1058        win_z_buf_ptr=NULL;1059      else {1060        win_z_num=win_task->win_z_num;1061        win_z_buf_ptr=gr.win_z_buf(U8 *)+((h1+y)/FONT_HEIGHT*TEXT_COLS+1062              (w1+x)/FONT_WIDTH)*sizeof(U16);1063        win_z_buf_line_dec=whole_I64s;1064        if (leading_pixels)1065          win_z_buf_line_dec++;1066        if (trailing_pixels)1067          win_z_buf_line_dec++;1068        win_z_buf_line_dec*=sizeof(U16);1069        win_z_buf_line_inc=TEXT_COLS*sizeof(U16)-win_z_buf_line_dec;1070      }1071      kk1=(h1+y)*dc->width_internal+x+w1;1072      if (dither) {1073        if (probability_dither) {1074          if (RandU16<dc->dither_probability_u16)1075            color.c0=dither_colors.c1;1076          else1077            color.c0=dither_colors.c0;1078          switch [color.c0.rop] {1079            case ROPB_EQU:1080            case ROPB_MONO:1081              if (win_z_buf_ptr) {1082                res=0;1083                for (j=h1;j<h2;j++) {1084                  color_mask=gr.to_8_colors[color.c0.color];1085                  dst=dc->body+kk1&~7;1086                  if (leading_pixels) {1087                    if (win_z_num>=*win_z_buf_ptr++) {1088                      *dst(I64 *)=*dst(I64 *)&leading_pixel_mask|1089                            color_mask&~leading_pixel_mask;1090                      res+=leading_pixels;1091                    }1092                    dst(I64 *)++;1093                  }1094                  for (i=0;i<whole_I64s;i++,dst(I64 *)++)1095                    if (win_z_num>=*win_z_buf_ptr++) {1096                      *dst(I64 *)=color_mask;1097                      res+=8;1098                    }1099                  if (trailing_pixels && win_z_num>=*win_z_buf_ptr++) {1100                    *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|1101                          color_mask&~trailing_pixel_mask;1102                    res+=trailing_pixels;1103                  }1104                  if ((j+y)&7==7)1105                    win_z_buf_ptr(U8 *)+=win_z_buf_line_inc;1106                  else1107                    win_z_buf_ptr(U8 *)-=win_z_buf_line_dec;1108                  kk1+=dc->width_internal;1109                  if (RandU16<dc->dither_probability_u16)1110                    color.c0=dither_colors.c1;1111                  else1112                    color.c0=dither_colors.c0;1113                }1114              } else {1115                for (j=h2-h1;j;j--) {1116                  color_mask=gr.to_8_colors[color.c0.color];1117                  dst=dc->body+kk1&~7;1118                  if (leading_pixels)1119                    *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|1120                          color_mask&~leading_pixel_mask;1121                  for (i=0;i<whole_I64s;i++,dst(I64 *)++)1122                    *dst(I64 *)=color_mask;1123                  if (trailing_pixels)1124                    *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|1125                          color_mask&~trailing_pixel_mask;1126                  kk1+=dc->width_internal;1127                  if (RandU16<dc->dither_probability_u16)1128                    color.c0=dither_colors.c1;1129                  else1130                    color.c0=dither_colors.c0;1131                }1132                res=(h2-h1)*(w2-w1);1133              }1134              break;1135            case ROPB_XOR:1136              if (win_z_buf_ptr) {1137                res=0;1138                for (j=h1;j<h2;j++) {1139                  color_mask=gr.to_8_colors[color.c0.color];1140                  dst=dc->body+kk1&~7;1141                  if (leading_pixels) {1142                    if (win_z_num>=*win_z_buf_ptr++) {1143                      *dst(I64 *)=*dst(I64 *)&leading_pixel_mask|1144                            *dst(I64 *)^color_mask&~leading_pixel_mask;1145                      res+=leading_pixels;1146                    }1147                    dst(I64 *)++;1148                  }1149                  for (i=0;i<whole_I64s;i++,dst(I64 *)++)1150                    if (win_z_num>=*win_z_buf_ptr++) {1151                      *dst(I64 *)^=color_mask;1152                      res+=8;1153                    }1154                  if (trailing_pixels && win_z_num>=*win_z_buf_ptr++) {1155                    *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|1156                          *dst(I64 *)^color_mask&~trailing_pixel_mask;1157                    res+=trailing_pixels;1158                  }1159                  if ((j+y)&7==7)1160                    win_z_buf_ptr(U8 *)+=win_z_buf_line_inc;1161                  else1162                    win_z_buf_ptr(U8 *)-=win_z_buf_line_dec;1163                  kk1+=dc->width_internal;1164                  if (RandU16<dc->dither_probability_u16)1165                    color.c0=dither_colors.c1;1166                  else1167                    color.c0=dither_colors.c0;1168                }1169              } else {1170                for (j=h2-h1;j;j--) {1171                  color_mask=gr.to_8_colors[color.c0.color];1172                  dst=dc->body+kk1&~7;1173                  if (leading_pixels)1174                    *dst(I64 *)++=*dst(I64 *)&leading_pixel_mask|1175                          *dst(I64 *)^color_mask&~leading_pixel_mask;1176                  for (i=0;i<whole_I64s;i++,dst(I64 *)++)1177                    *dst(I64 *)^=color_mask;1178                  if (trailing_pixels)1179                    *dst(I64 *)=*dst(I64 *)&trailing_pixel_mask|1180                          *dst(I64 *)^color_mask&~trailing_pixel_mask;1181                  kk1+=dc->width_internal;1182                  if (RandU16<dc->dither_probability_u16)1183                    color.c0=dither_colors.c1;1184                  else1185                    color.c0=dither_colors.c0;1186                }1187                res=(h2-h1)*(w2-w1);1188              }1189              break;1190          }1191        } else {1192          if (((x+w1-original_leading_pixels)^(y+h1))&1)1193            SwapU16(&color.c0,&color.c1);1194          switch [color.c0.rop] {1195            case ROPB_EQU:1196            case ROPB_MONO:1197              if (win_z_buf_ptr) {1198                res=0;1199                for (j=h1;j<h2;j++) {1200                  color_mask=gr.to_8_bits[0x55]&gr.to_8_colors[color.c0.color]|

Showing the first 1,200 of 2002 lines. Download the file for the rest.