Changeset 1816


Ignore:
Timestamp:
Sep 29, 2007, 5:15:17 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • Allow to rotate canvases with an odd width.
  • Added a few additional character pairs of the left/right rotations.
  • Added missing errno sets.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/transform.c

    r1814 r1816  
    230230 *
    231231 *  Note that the width of the canvas is divided by two and becomes the
    232  *  new height. Height is multiplied by two and becomes the new width. It
    233  *  is illegal to pass a canvas with an odd width to this function.
     232 *  new height. Height is multiplied by two and becomes the new width. If
     233 *  the original width is an odd number, the division is rounded up.
    234234 *
    235235 *  If an error occurs, -1 is returned and \b errno is set accordingly:
    236236 *  - \c EBUSY The canvas is in use by a display driver and cannot be rotated.
    237  *  - \c EINVAL The canvas' width is odd.
    238237 *  - \c ENOMEM Not enough memory to allocate the new canvas size. If this
    239238 *    happens, the previous canvas handle is still valid.
     
    245244{
    246245    uint32_t *newchars, *newattrs;
    247     unsigned int x, y, subwidth, subheight;
     246    unsigned int x, y, w2, h2;
    248247
    249248    if(cv->refcount)
    250249    {
    251250        seterrno(EBUSY);
    252         return -1;
    253     }
    254 
    255     if(cv->width & 1)
    256     {
    257         seterrno(EINVAL);
    258251        return -1;
    259252    }
     
    262255    _cucul_save_frame_info(cv);
    263256
    264     newchars = malloc(cv->width * cv->height * sizeof(uint32_t));
     257    w2 = (cv->width + 1) / 2;
     258    h2 = cv->height;
     259
     260    newchars = malloc(w2 * h2 * 2 * sizeof(uint32_t));
    265261    if(!newchars)
    266         return -1;
    267 
    268     newattrs = malloc(cv->width * cv->height * sizeof(uint32_t));
     262    {
     263        seterrno(ENOMEM);
     264        return -1;
     265    }
     266
     267    newattrs = malloc(w2 * h2 * 2 * sizeof(uint32_t));
    269268    if(!newattrs)
    270269    {
    271270        free(newchars);
    272         return -1;
    273     }
    274 
    275     subwidth = cv->width / 2;
    276     subheight = cv->height;
    277 
    278     for(y = 0; y < subheight; y++)
    279     {
    280         for(x = 0; x < subwidth; x++)
     271        seterrno(ENOMEM);
     272        return -1;
     273    }
     274
     275    for(y = 0; y < h2; y++)
     276    {
     277        for(x = 0; x < w2; x++)
    281278        {
    282279            uint32_t pair[2], attr1, attr2;
    283280
    284             pair[0] = cv->chars[(subwidth * y + x) * 2];
    285             attr1 = cv->attrs[(subwidth * y + x) * 2];
    286             pair[1] = cv->chars[(subwidth * y + x) * 2 + 1];
    287             attr2 = cv->attrs[(subwidth * y + x) * 2 + 1];
     281            pair[0] = cv->chars[cv->width * y + x * 2];
     282            attr1 = cv->attrs[cv->width * y + x * 2];
     283
     284            if((cv->width & 1) && x == w2 - 1)
     285            {
     286                /* Special case: odd column */
     287                pair[1] = ' ';
     288                attr2 = attr1;
     289            }
     290            else
     291            {
     292                pair[1] = cv->chars[cv->width * y + x * 2 + 1];
     293                attr2 = cv->attrs[cv->width * y + x * 2 + 1];
     294            }
     295
    288296
    289297            leftpair(pair);
    290298
    291             newchars[(subheight * (subwidth - 1 - x) + y) * 2] = pair[0];
    292             newattrs[(subheight * (subwidth - 1 - x) + y) * 2] = attr1;
    293             newchars[(subheight * (subwidth - 1 - x) + y) * 2 + 1] = pair[1];
    294             newattrs[(subheight * (subwidth - 1 - x) + y) * 2 + 1] = attr2;
     299            newchars[(h2 * (w2 - 1 - x) + y) * 2] = pair[0];
     300            newattrs[(h2 * (w2 - 1 - x) + y) * 2] = attr1;
     301            newchars[(h2 * (w2 - 1 - x) + y) * 2 + 1] = pair[1];
     302            newattrs[(h2 * (w2 - 1 - x) + y) * 2 + 1] = attr2;
    295303        }
    296304    }
     
    311319
    312320    cv->frames[cv->frame].width = cv->height * 2;
    313     cv->frames[cv->frame].height = cv->width / 2;
     321    cv->frames[cv->frame].height = (cv->width + 1) / 2;
    314322
    315323    cv->frames[cv->frame].chars = newchars;
     
    332340 *
    333341 *  Note that the width of the canvas is divided by two and becomes the
    334  *  new height. Height is multiplied by two and becomes the new width. It
    335  *  is illegal to pass a canvas with an odd width to this function.
     342 *  new height. Height is multiplied by two and becomes the new width. If
     343 *  the original width is an odd number, the division is rounded up.
    336344 *
    337345 *  If an error occurs, -1 is returned and \b errno is set accordingly:
    338346 *  - \c EBUSY The canvas is in use by a display driver and cannot be rotated.
    339  *  - \c EINVAL The canvas' width is odd.
    340347 *  - \c ENOMEM Not enough memory to allocate the new canvas size. If this
    341348 *    happens, the previous canvas handle is still valid.
     
    347354{
    348355    uint32_t *newchars, *newattrs;
    349     unsigned int x, y, subwidth, subheight;
     356    unsigned int x, y, w2, h2;
    350357
    351358    if(cv->refcount)
    352359    {
    353360        seterrno(EBUSY);
    354         return -1;
    355     }
    356 
    357     if(cv->width & 1)
    358     {
    359         seterrno(EINVAL);
    360361        return -1;
    361362    }
     
    364365    _cucul_save_frame_info(cv);
    365366
    366     newchars = malloc(cv->width * cv->height * sizeof(uint32_t));
     367    w2 = (cv->width + 1) / 2;
     368    h2 = cv->height;
     369
     370    newchars = malloc(w2 * h2 * 2 * sizeof(uint32_t));
    367371    if(!newchars)
    368         return -1;
    369 
    370     newattrs = malloc(cv->width * cv->height * sizeof(uint32_t));
     372    {
     373        seterrno(ENOMEM);
     374        return -1;
     375    }
     376
     377    newattrs = malloc(w2 * h2 * 2 * sizeof(uint32_t));
    371378    if(!newattrs)
    372379    {
    373380        free(newchars);
    374         return -1;
    375     }
    376 
    377     subwidth = cv->width / 2;
    378     subheight = cv->height;
    379 
    380     for(y = 0; y < subheight; y++)
    381     {
    382         for(x = 0; x < subwidth; x++)
     381        seterrno(ENOMEM);
     382        return -1;
     383    }
     384
     385    for(y = 0; y < h2; y++)
     386    {
     387        for(x = 0; x < w2; x++)
    383388        {
    384389            uint32_t pair[2], attr1, attr2;
    385390
    386             pair[0] = cv->chars[(subwidth * y + x) * 2];
    387             attr1 = cv->attrs[(subwidth * y + x) * 2];
    388             pair[1] = cv->chars[(subwidth * y + x) * 2 + 1];
    389             attr2 = cv->attrs[(subwidth * y + x) * 2 + 1];
     391            pair[0] = cv->chars[cv->width * y + x * 2];
     392            attr1 = cv->attrs[cv->width * y + x * 2];
     393
     394            if((cv->width & 1) && x == w2 - 1)
     395            {
     396                /* Special case: odd column */
     397                pair[1] = ' ';
     398                attr2 = attr1;
     399            }
     400            else
     401            {
     402                pair[1] = cv->chars[cv->width * y + x * 2 + 1];
     403                attr2 = cv->attrs[cv->width * y + x * 2 + 1];
     404            }
    390405
    391406            rightpair(pair);
    392407
    393             newchars[(subheight * x + subheight - 1 - y) * 2] = pair[0];
    394             newattrs[(subheight * x + subheight - 1 - y) * 2] = attr1;
    395             newchars[(subheight * x + subheight - 1 - y) * 2 + 1] = pair[1];
    396             newattrs[(subheight * x + subheight - 1 - y) * 2 + 1] = attr2;
     408            newchars[(h2 * x + h2 - 1 - y) * 2] = pair[0];
     409            newattrs[(h2 * x + h2 - 1 - y) * 2] = attr1;
     410            newchars[(h2 * x + h2 - 1 - y) * 2 + 1] = pair[1];
     411            newattrs[(h2 * x + h2 - 1 - y) * 2 + 1] = attr2;
    397412        }
    398413    }
     
    413428
    414429    cv->frames[cv->frame].width = cv->height * 2;
    415     cv->frames[cv->frame].height = cv->width / 2;
     430    cv->frames[cv->frame].height = (cv->width + 1) / 2;
    416431
    417432    cv->frames[cv->frame].chars = newchars;
     
    459474    newchars = malloc(cv->width * cv->height * sizeof(uint32_t));
    460475    if(!newchars)
    461         return -1;
     476    {
     477        seterrno(ENOMEM);
     478        return -1;
     479    }
    462480
    463481    newattrs = malloc(cv->width * cv->height * sizeof(uint32_t));
     
    465483    {
    466484        free(newchars);
     485        seterrno(ENOMEM);
    467486        return -1;
    468487    }
     
    968987    /* ASCII / Unicode */
    969988    '-', '-', 0x4e28, CUCUL_MAGIC_FULLWIDTH, /* -- 丨 */
     989    '|', '|', 0x2f06, CUCUL_MAGIC_FULLWIDTH, /* || ⼆ */
    970990    /* Unicode */
    971991    0x2584, 0x2580, 0x2580, 0x2584, /* ▄▀ ▀▄ */
     
    9881008    '\\', ' ', '_', ',', ' ', '\\', 0x00b4, 0x203e,    /* \  _,  \ ´‾ */
    9891009    '\\', '_', '_', '/', 0x203e, '\\', '/', 0x203e,    /* \_ _/ ‾\ /‾ */
     1010    '_', '\\', 0x203e, '/', '\\', 0x203e, '/', '_',    /* _\ ‾/ \‾ /_ */
    9901011    '|', ' ', '_', '_', ' ', '|', 0x203e, 0x203e,      /* |  __  | ‾‾ */
    9911012    '_', '|', 0x203e, '|', '|', 0x203e, '|', '_',      /* _| ‾| |‾ |_ */
     
    9941015    ' ', '_', ' ', 0x2575, 0x203e, ' ', 0x2577, ' ',   /*  _  ╵ ‾  ╷  */
    9951016    '.', '_', '.', 0x2575, 0x203e, '\'', 0x2577, '\'', /* ._ .╵ ‾' ╷' */
     1017    '(', '_', 0x203f, '|', 0x203e, ')', '|', 0x2040,   /* (_ ‿| ‾) |⁀ */
     1018    '(', 0x203e, '|', 0x203f, '_', ')', 0x2040, '|',   /* (‾ |‿ _) ⁀| */
    9961019    '\\', '/', 0xff1e, CUCUL_MAGIC_FULLWIDTH,
    9971020            '/', '\\', 0xff1c, CUCUL_MAGIC_FULLWIDTH,  /* \/ > /\ < */
     1021    ')', ' ', 0xfe35, CUCUL_MAGIC_FULLWIDTH,
     1022            ' ', '(', 0xfe36, CUCUL_MAGIC_FULLWIDTH,   /* )  ︵  ( ︶ */
     1023    '}', ' ', 0xfe37, CUCUL_MAGIC_FULLWIDTH,
     1024            ' ', '{', 0xfe38, CUCUL_MAGIC_FULLWIDTH,   /* }  ︷  { ︸ */
    9981025    /* Not perfect, but better than nothing */
    9991026    '(', ' ', 0x02ce, ',', ' ', ')', 0x00b4, '`',      /* (  ˎ,  ) ´` */
    1000     ')', ' ', ',', 0x02ce, ' ', '(', '`', 0x00b4,      /* )  ,ˎ  ( `´ */
     1027    ' ', 'v', '>', ' ', 0x028c, ' ', ' ', '<',         /*  v >  ʌ   < */
    10011028    ' ', 'V', '>', ' ', 0x039b, ' ', ' ', '<',         /*  V >  Λ   < */
     1029    'v', ' ', '>', ' ', ' ', 0x028c, ' ', '<',         /* v  >   ʌ  < */
    10021030    'V', ' ', '>', ' ', ' ', 0x039b, ' ', '<',         /* V  >   Λ  < */
     1031    '\\', '|', 0xff1e, CUCUL_MAGIC_FULLWIDTH,
     1032            '|', '\\', 0xff1c, CUCUL_MAGIC_FULLWIDTH,  /* \| > |\ < */
     1033    '|', '/', 0xff1e, CUCUL_MAGIC_FULLWIDTH,
     1034            '/', '|', 0xff1c, CUCUL_MAGIC_FULLWIDTH,   /* |/ > /| < */
    10031035    /* Unicode */
    10041036    0x2584, ' ', ' ', 0x2584, ' ', 0x2580, 0x2580, ' ',       /* ▄   ▄  ▀ ▀  */
Note: See TracChangeset for help on using the changeset viewer.