Ignore:
Timestamp:
May 15, 2011, 6:22:08 AM (12 years ago)
Author:
Sam Hocevar
Message:

imlib: refactor a few image methods so that they use vec2i.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/imlib/image.cpp

    r650 r655  
    175175}
    176176
    177 void image::line(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color)
    178 {
    179   int16_t i, xc, yc, er, n, m, xi, yi, xcxi, ycyi, xcyi;
    180   unsigned dcy, dcx;
    181   // check to make sure that both endpoint are on the screen
    182 
    183   int cx1, cy1, cx2, cy2;
    184 
    185   // check to see if the line is completly clipped off
    186   GetClip(cx1, cy1, cx2, cy2);
    187   if ((x1 < cx1 && x2 < cx1) || (x1 >= cx2 && x2 >= cx2) ||
    188       (y1 < cy1 && y2 < cy1) || (y1 >= cy2 && y2 >= cy2))
    189     return;
    190 
    191   if (x1>x2)        // make sure that x1 is to the left
    192   {
    193     i=x1; x1=x2; x2=i;  // if not swap points
    194     i=y1; y1=y2; y2=i;
    195   }
    196 
    197   // clip the left side
    198   if (x1<cx1)
    199   {
    200     int my=(y2-y1);
    201     int mx=(x2-x1), b;
    202     if (!mx) return ;
    203     if (my)
    204     {
    205       b=y1-(y2-y1)*x1/mx;
    206       y1=my*cx1/mx+b;
    207       x1=cx1;
    208     }
    209     else x1=cx1;
    210   }
    211 
    212   // clip the right side
    213   if (x2 >= cx2)
    214   {
    215     int my=(y2-y1);
    216     int mx=(x2-x1), b;
    217     if (!mx) return ;
    218     if (my)
    219     {
    220       b=y1-(y2-y1)*x1/mx;
    221       y2=my * (cx2 - 1) / mx + b;
    222       x2 = cx2 - 1;
    223     }
    224     else x2 = cx2 - 1;
    225   }
    226 
    227   if (y1>y2)        // make sure that y1 is on top
    228   {
    229     i=x1; x1=x2; x2=i;  // if not swap points
    230     i=y1; y1=y2; y2=i;
    231   }
    232 
    233   // clip the bottom
    234   if (y2 >= cy2)
    235   {
    236     int mx=(x2-x1);
    237     int my=(y2-y1), b;
    238     if (!my)
    239       return ;
    240     if (mx)
    241     {
    242       b = y1 - (y2 - y1) * x1 / mx;
    243       x2 = (cy2 - 1 - b) * mx / my;
    244       y2 = cy2 - 1;
    245     }
    246     else y2 = cy2 - 1;
    247   }
    248 
    249   // clip the top
    250   if (y1<cy1)
    251   {
    252     int mx=(x2-x1);
    253     int my=(y2-y1), b;
    254     if (!my) return ;
    255     if (mx)
    256     {
    257       b=y1-(y2-y1)*x1/mx;
    258       x1=(cy1-b)*mx/my;
    259       y1=cy1;
    260     }
    261     else y1=cy1;
    262   }
    263 
    264 
    265   // see if it got cliped into the box, out out
    266   if (x1<cx1 || x2<cx1 || x1 >= cx2 || x2 >= cx2 || y1<cy1 || y2 <cy1 || y1 >= cy2 || y2 >= cy2)
    267     return;
    268 
    269 
    270 
    271   if (x1>x2)
    272   { xc=x2; xi=x1; }
    273   else { xi=x2; xc=x1; }
    274 
    275 
    276   // assume y1<=y2 from above swap operation
    277   yi=y2; yc=y1;
    278 
    279   AddDirty(xc, yc, xi + 1, yi + 1);
    280   dcx=x1; dcy=y1;
    281   xc=(x2-x1); yc=(y2-y1);
    282   if (xc<0) xi=-1; else xi=1;
    283   if (yc<0) yi=-1; else yi=1;
    284   n=abs(xc); m=abs(yc);
    285   ycyi=abs(2*yc*xi);
    286   er=0;
    287 
    288   Lock();
    289   if (n>m)
    290   {
    291     xcxi=abs(2*xc*xi);
    292     for (i=0; i<=n; i++)
    293     {
    294       *(scan_line(dcy)+dcx)=color;
    295       if (er>0)
    296       { dcy+=yi;
    297     er-=xcxi;
    298       }
    299       er+=ycyi;
    300       dcx+=xi;
    301     }
    302   }
    303   else
    304   {
    305     xcyi=abs(2*xc*yi);
    306     for (i=0; i<=m; i++)
    307     {
    308       *(scan_line(dcy)+dcx)=color;
    309       if (er>0)
    310       { dcx+=xi;
    311     er-=ycyi;
    312       }
    313       er+=xcyi;
    314       dcy+=yi;
    315     }
    316   }
    317   Unlock();
     177void image::Line(vec2i p1, vec2i p2, uint8_t color)
     178{
     179    int xc, yc, er, n, m, xi, yi, xcxi, ycyi, xcyi;
     180    unsigned int dcy, dcx;
     181
     182    int cx1, cy1, cx2, cy2;
     183
     184    // check to see if the line is completly clipped off
     185    GetClip(cx1, cy1, cx2, cy2);
     186    if ((p1.x < cx1 && p2.x < cx1) || (p1.x >= cx2 && p2.x >= cx2) ||
     187        (p1.y < cy1 && p2.y < cy1) || (p1.y >= cy2 && p2.y >= cy2))
     188        return;
     189
     190    if (p1.x > p2.x) // make sure that p1.x is to the left
     191    {
     192        vec2i tmp = p1; p1 = p2; p2 = tmp; // if not swap points
     193    }
     194
     195    // clip the left side
     196    if (p1.x < cx1)
     197    {
     198        vec2i m = p2 - p1;
     199
     200        if (!m.x)
     201            return;
     202        if (m.y)
     203        {
     204            int b = p1.y - m.y * p1.x / m.x;
     205            p1.y = b + m.y * cx1 / m.x;
     206        }
     207        p1.x = cx1;
     208    }
     209
     210    // clip the right side
     211    if (p2.x >= cx2)
     212    {
     213        vec2i m = p2 - p1;
     214        if (!m.x)
     215            return;
     216        if (m.y)
     217        {
     218            int b = p1.y - m.y * p1.x / m.x;
     219            p2.y = b + m.y * (cx2 - 1) / m.x;
     220        }
     221        p2.x = cx2 - 1;
     222    }
     223
     224    if (p1.y > p2.y) // make sure that p1.y is on top
     225    {
     226        vec2i tmp = p1; p1 = p2; p2 = tmp; // if not swap points
     227    }
     228
     229    // clip the bottom
     230    if (p2.y >= cy2)
     231    {
     232        vec2i m = p2 - p1;
     233        if (!m.y)
     234            return;
     235        if (m.x)
     236        {
     237            int b = p1.y - (p2.y - p1.y) * p1.x / m.x;
     238            p2.x = (cy2 - 1 - b) * m.x / m.y;
     239        }
     240        p2.y = cy2 - 1;
     241    }
     242
     243    // clip the top
     244    if (p1.y < cy1)
     245    {
     246        vec2i m = p2 - p1;
     247        if (!m.y)
     248            return;
     249        if (m.x)
     250        {
     251            int b = p1.y - m.y * p1.x / m.x;
     252            p1.x = (cy1 - b) * m.x / m.y;
     253        }
     254        p1.y = cy1;
     255    }
     256
     257    // see if it got cliped into the box, out out
     258    if (p1.x < cx1 || p2.x < cx1 || p1.x >= cx2 || p2.x >= cx2
     259         || p1.y < cy1 || p2.y < cy1 || p1.y >= cy2 || p2.y >= cy2)
     260        return;
     261
     262    if (p1.x > p2.x)
     263    {
     264        xc = p2.x; xi = p1.x;
     265    }
     266    else
     267    {
     268        xi = p2.x;
     269        xc = p1.x;
     270    }
     271
     272    // assume p1.y <= p2.y from above swap operation
     273    yi = p2.y; yc = p1.y;
     274
     275    AddDirty(xc, yc, xi + 1, yi + 1);
     276    dcx = p1.x; dcy = p1.y;
     277    xc = (p2.x - p1.x);
     278    yc = (p2.y - p1.y);
     279    xi = (xc < 0) ? -1 : 1;
     280    yi = (yc < 0) ? -1 : 1;
     281    n = abs(xc);
     282    m = abs(yc);
     283    ycyi = abs(2 * yc * xi);
     284    er = 0;
     285
     286    Lock();
     287    if (n > m)
     288    {
     289        xcxi = abs(2 * xc * xi);
     290        for (int i = 0; i <= n; i++)
     291        {
     292            scan_line(dcy)[dcx] = color;
     293            if (er > 0)
     294            {
     295                dcy += yi;
     296                er -= xcxi;
     297            }
     298            er += ycyi;
     299            dcx += xi;
     300        }
     301    }
     302    else
     303    {
     304        xcyi = abs(2 * xc * yi);
     305        for (int i = 0; i <= m; i++)
     306        {
     307            scan_line(dcy)[dcx] = color;
     308            if (er > 0)
     309            {
     310                dcx += xi;
     311                er -= ycyi;
     312            }
     313            er += xcyi;
     314            dcy += yi;
     315        }
     316    }
     317    Unlock();
    318318}
    319319
     
    631631}
    632632
    633 void image::rectangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color)
    634 {
    635   line(x1, y1, x2, y1, color);
    636   line(x2, y1, x2, y2, color);
    637   line(x1, y2, x2, y2, color);
    638   line(x1, y1, x1, y2, color);
     633void image::Rectangle(vec2i p1, vec2i p2, uint8_t color)
     634{
     635    Line(p1, vec2i(p2.x, p1.y), color);
     636    Line(vec2i(p2.x, p1.y), p2, color);
     637    Line(vec2i(p1.x, p2.y), p2, color);
     638    Line(p1, vec2i(p1.x, p2.y), color);
    639639}
    640640
     
    869869}
    870870
    871 void image::bar      (int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color)
    872 {
    873   int16_t y;
    874   if (x1>x2 || y1>y2) return ;
    875   if (m_special)
    876   { x1=m_special->bound_x1(x1);
    877     y1=m_special->bound_y1(y1);
    878     x2=m_special->bound_x2(x2+1)-1;
    879     y2=m_special->bound_y2(y2+1)-1;
    880   }
    881   else
    882   { if (x1<0) x1=0;
    883     if (y1<0) y1=0;
    884     if (x2>=m_size.x)  x2=m_size.x-1;
    885     if (y2>=m_size.y) y2=m_size.y-1;
    886   }
    887   if (x2<0 || y2<0 || x1>=m_size.x || y1>=m_size.y || x2<x1 || y2<y1)
    888     return ;
    889   Lock();
    890   for (y=y1; y<=y2; y++)
    891     memset(scan_line(y)+x1, color, (x2-x1+1));
    892   Unlock();
    893   AddDirty(x1, y1, x2 + 1, y2 + 1);
     871void image::Bar(vec2i p1, vec2i p2, uint8_t color)
     872{
     873    if (p1.x > p2.x || p1.y > p2.y)
     874        return;
     875    if (m_special)
     876    {
     877        p1.x = m_special->bound_x1(p1.x);
     878        p1.y = m_special->bound_y1(p1.y);
     879        p2.x = m_special->bound_x2(p2.x + 1) - 1;
     880        p2.y = m_special->bound_y2(p2.y + 1) - 1;
     881    }
     882    else
     883    {
     884        p1.x = Max(p1.x, 0);
     885        p1.y = Max(p1.y, 0);
     886        p2.x = Min(p2.x, m_size.x - 1);
     887        p2.y = Min(p2.y, m_size.y - 1);
     888    }
     889    if (p2.x < 0 || p2.y < 0 || p1.x >= m_size.x || p1.y >= m_size.y
     890         || p2.x < p1.x || p2.y < p1.y)
     891        return;
     892
     893    Lock();
     894    for (int y = p1.y; y <= p2.y; y++)
     895        memset(scan_line(y) + p1.x, color, (p2.x - p1.x + 1));
     896    Unlock();
     897    AddDirty(p1.x, p1.y, p2.x + 1, p2.y + 1);
    894898}
    895899
     
    953957void image::dither(palette *pal)
    954958{
    955   int16_t x, y, i, j;
     959  int16_t x, y, j;
    956960  uint8_t dt_matrix[]={ 0,  136, 24, 170,
    957961           68, 204, 102, 238,
     
    964968  {
    965969    sl=scan_line(y);
    966     for (i=0, j=y%4, x=0; x < m_size.x; x++)
     970    for (j=y%4, x=0; x < m_size.x; x++)
    967971      sl[x] = (pal->red(sl[x]) > dt_matrix[j * 4 + (x & 3)]) ? 255 : 0;
    968972  }
     
    10611065}
    10621066
    1063 void image::widget_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
    1064        uint8_t light, uint8_t med, uint8_t dark)
    1065 {
    1066   line(x1, y1, x2, y1, light);
    1067   line(x1, y1, x1, y2, light);
    1068   line(x2, y1+1, x2, y2, dark);
    1069   line(x1+1, y2, x2-1, y2, dark);
    1070   bar(x1+1, y1+1, x2-1, y2-1, med);
     1067void image::WidgetBar(vec2i p1, vec2i p2,
     1068                      uint8_t light, uint8_t med, uint8_t dark)
     1069{
     1070    Line(p1, vec2i(p2.x, p1.y), light);
     1071    Line(p1, vec2i(p1.x, p2.y), light);
     1072    Line(vec2i(p2.x, p1.y + 1), p2, dark);
     1073    Line(vec2i(p1.x + 1, p2.y), vec2i(p2.x - 1, p2.y - 1), dark);
     1074    Bar(p1 + vec2i(1, 1), p2 - vec2i(1, 1), med);
    10711075}
    10721076
     
    11851189      for (yy=0; yy<7; yy++)
    11861190    if ((1<<yy)&dig[zz])
    1187       line(x+ledx[yy*2]*scale, y+ledy[yy*2]*scale, x+ledx[yy*2+1]*scale,
    1188         y+ledy[yy*2+1]*scale, color);
     1191      Line(vec2i(x+ledx[yy*2]*scale, y+ledy[yy*2]*scale),
     1192           vec2i(x+ledx[yy*2+1]*scale, y+ledy[yy*2+1]*scale), color);
    11891193    }
    11901194    x+=6*scale;
Note: See TracChangeset for help on using the changeset viewer.