Changeset 1809


Ignore:
Timestamp:
Jul 17, 2007, 10:46:18 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Renamed cucul_rotate_*_wide to cucul_rotate_* and cucul_rotate_* to cucul_stretch_* to avoid long function names. And the pairwise rotations look so nice now that they should be the real rotation chars.
Location:
libcaca/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/cucul.h

    r1805 r1809  
    123123int cucul_rotate_180(cucul_canvas_t *);
    124124int cucul_rotate_left(cucul_canvas_t *);
    125 int cucul_rotate_left_wide(cucul_canvas_t *);
    126125int cucul_rotate_right(cucul_canvas_t *);
    127 int cucul_rotate_right_wide(cucul_canvas_t *);
     126int cucul_stretch_left(cucul_canvas_t *);
     127int cucul_stretch_right(cucul_canvas_t *);
    128128/*  @} */
    129129
  • libcaca/trunk/cucul/transform.c

    r1808 r1809  
    223223 *
    224224 *  Apply a 90-degree transformation to a canvas, choosing characters
    225  *  that look like the rotated version wherever possible. Some characters
    226  *  will stay unchanged by the process, some others will be replaced by
    227  *  close equivalents. Fullwidth characters will be lost. The operation is
     225 *  that look like the rotated version wherever possible. Characters cells
     226 *  are rotated two-by-two. Some characters will stay unchanged by the
     227 *  process, some others will be replaced by close equivalents. Fullwidth
     228 *  characters at odd horizontal coordinates will be lost. The operation is
    228229 *  not guaranteed to be reversible at all.
    229230 *
    230  *  Note that the width and height of the canvas are swapped.
     231 *  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.
    231234 *
    232235 *  If an error occurs, -1 is returned and \b errno is set accordingly:
    233236 *  - \c EBUSY The canvas is in use by a display driver and cannot be rotated.
     237 *  - \c EINVAL The canvas' width is odd.
    234238 *  - \c ENOMEM Not enough memory to allocate the new canvas size. If this
    235239 *    happens, the previous canvas handle is still valid.
     
    241245{
    242246    uint32_t *newchars, *newattrs;
    243     unsigned int x, y;
     247    unsigned int x, y, subwidth, subheight;
    244248
    245249    if(cv->refcount)
    246250    {
    247251        seterrno(EBUSY);
     252        return -1;
     253    }
     254
     255    if(cv->width & 1)
     256    {
     257        seterrno(EINVAL);
    248258        return -1;
    249259    }
     
    263273    }
    264274
    265     for(y = 0; y < cv->height; y++)
    266     {
    267         for(x = 0; x < cv->width; x++)
    268         {
    269             uint32_t ch, attr;
    270 
    271             ch = cv->chars[cv->width * y + x];
    272             attr = cv->attrs[cv->width * y + x];
    273 
    274             /* FIXME: do something about fullwidth characters */
    275             ch = leftchar(ch);
    276 
    277             newchars[cv->height * (cv->width - 1 - x) + y] = ch;
    278             newattrs[cv->height * (cv->width - 1 - x) + y] = attr;
     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++)
     281        {
     282            uint32_t pair[2], attr1, attr2;
     283
     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];
     288
     289            leftpair(pair);
     290
     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;
    279295        }
    280296    }
     
    286302    x = cv->frames[cv->frame].x;
    287303    y = cv->frames[cv->frame].y;
    288     cv->frames[cv->frame].x = y;
    289     cv->frames[cv->frame].y = cv->width - 1 - x;
     304    cv->frames[cv->frame].x = y * 2;
     305    cv->frames[cv->frame].y = (cv->width - 1 - x) / 2;
    290306
    291307    x = cv->frames[cv->frame].handlex;
    292308    y = cv->frames[cv->frame].handley;
    293     cv->frames[cv->frame].handlex = y;
    294     cv->frames[cv->frame].handley = cv->width - 1 - x;
    295 
    296     cv->frames[cv->frame].width = cv->height;
    297     cv->frames[cv->frame].height = cv->width;
     309    cv->frames[cv->frame].handlex = y * 2;
     310    cv->frames[cv->frame].handley = (cv->width - 1 - x) / 2;
     311
     312    cv->frames[cv->frame].width = cv->height * 2;
     313    cv->frames[cv->frame].height = cv->width / 2;
    298314
    299315    cv->frames[cv->frame].chars = newchars;
     
    306322}
    307323
    308 /** \brief Rotate a canvas, 90 degrees counterclockwise (widechar version).
     324/** \brief Rotate a canvas, 90 degrees counterclockwise.
    309325 *
    310326 *  Apply a 90-degree transformation to a canvas, choosing characters
     
    325341 *    happens, the previous canvas handle is still valid.
    326342 *
    327  *  \param cv The canvas to rotate left.
     343 *  \param cv The canvas to rotate right.
    328344 *  \return 0 in case of success, -1 if an error occurred.
    329345 */
    330 int cucul_rotate_left_wide(cucul_canvas_t *cv)
     346int cucul_rotate_right(cucul_canvas_t *cv)
    331347{
    332348    uint32_t *newchars, *newattrs;
     
    373389            attr2 = cv->attrs[(subwidth * y + x) * 2 + 1];
    374390
    375             leftpair(pair);
    376 
    377             newchars[(subheight * (subwidth - 1 - x) + y) * 2] = pair[0];
    378             newattrs[(subheight * (subwidth - 1 - x) + y) * 2] = attr1;
    379             newchars[(subheight * (subwidth - 1 - x) + y) * 2 + 1] = pair[1];
    380             newattrs[(subheight * (subwidth - 1 - x) + y) * 2 + 1] = attr2;
     391            rightpair(pair);
     392
     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;
    381397        }
    382398    }
     
    388404    x = cv->frames[cv->frame].x;
    389405    y = cv->frames[cv->frame].y;
    390     cv->frames[cv->frame].x = y * 2;
    391     cv->frames[cv->frame].y = (cv->width - 1 - x) / 2;
     406    cv->frames[cv->frame].x = (cv->height - 1 - y) * 2;
     407    cv->frames[cv->frame].y = x / 2;
    392408
    393409    x = cv->frames[cv->frame].handlex;
    394410    y = cv->frames[cv->frame].handley;
    395     cv->frames[cv->frame].handlex = y * 2;
    396     cv->frames[cv->frame].handley = (cv->width - 1 - x) / 2;
     411    cv->frames[cv->frame].handlex = (cv->height - 1 - y) * 2;
     412    cv->frames[cv->frame].handley = x / 2;
    397413
    398414    cv->frames[cv->frame].width = cv->height * 2;
     
    408424}
    409425
    410 /** \brief Rotate a canvas, 90 degrees clockwise.
     426/** \brief Rotate and stretch a canvas, 90 degrees counterclockwise.
     427 *
     428 *  Apply a 90-degree transformation to a canvas, choosing characters
     429 *  that look like the rotated version wherever possible. Some characters
     430 *  will stay unchanged by the process, some others will be replaced by
     431 *  close equivalents. Fullwidth characters will be lost. The operation is
     432 *  not guaranteed to be reversible at all.
     433 *
     434 *  Note that the width and height of the canvas are swapped, causing its
     435 *  aspect ratio to look stretched.
     436 *
     437 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     438 *  - \c EBUSY The canvas is in use by a display driver and cannot be rotated.
     439 *  - \c ENOMEM Not enough memory to allocate the new canvas size. If this
     440 *    happens, the previous canvas handle is still valid.
     441 *
     442 *  \param cv The canvas to rotate left.
     443 *  \return 0 in case of success, -1 if an error occurred.
     444 */
     445int cucul_stretch_left(cucul_canvas_t *cv)
     446{
     447    uint32_t *newchars, *newattrs;
     448    unsigned int x, y;
     449
     450    if(cv->refcount)
     451    {
     452        seterrno(EBUSY);
     453        return -1;
     454    }
     455
     456    /* Save the current frame shortcuts */
     457    _cucul_save_frame_info(cv);
     458
     459    newchars = malloc(cv->width * cv->height * sizeof(uint32_t));
     460    if(!newchars)
     461        return -1;
     462
     463    newattrs = malloc(cv->width * cv->height * sizeof(uint32_t));
     464    if(!newattrs)
     465    {
     466        free(newchars);
     467        return -1;
     468    }
     469
     470    for(y = 0; y < cv->height; y++)
     471    {
     472        for(x = 0; x < cv->width; x++)
     473        {
     474            uint32_t ch, attr;
     475
     476            ch = cv->chars[cv->width * y + x];
     477            attr = cv->attrs[cv->width * y + x];
     478
     479            /* FIXME: do something about fullwidth characters */
     480            ch = leftchar(ch);
     481
     482            newchars[cv->height * (cv->width - 1 - x) + y] = ch;
     483            newattrs[cv->height * (cv->width - 1 - x) + y] = attr;
     484        }
     485    }
     486
     487    free(cv->chars);
     488    free(cv->attrs);
     489
     490    /* Swap X and Y information */
     491    x = cv->frames[cv->frame].x;
     492    y = cv->frames[cv->frame].y;
     493    cv->frames[cv->frame].x = y;
     494    cv->frames[cv->frame].y = cv->width - 1 - x;
     495
     496    x = cv->frames[cv->frame].handlex;
     497    y = cv->frames[cv->frame].handley;
     498    cv->frames[cv->frame].handlex = y;
     499    cv->frames[cv->frame].handley = cv->width - 1 - x;
     500
     501    cv->frames[cv->frame].width = cv->height;
     502    cv->frames[cv->frame].height = cv->width;
     503
     504    cv->frames[cv->frame].chars = newchars;
     505    cv->frames[cv->frame].attrs = newattrs;
     506
     507    /* Reset the current frame shortcuts */
     508    _cucul_load_frame_info(cv);
     509
     510    return 0;
     511}
     512
     513/** \brief Rotate and stretch a canvas, 90 degrees clockwise.
    411514 *
    412515 *  Apply a 270-degree transformation to a canvas, choosing characters
     
    416519 *  not guaranteed to be reversible at all.
    417520 *
    418  *  Note that the width and height of the canvas are swapped.
     521 *  Note that the width and height of the canvas are swapped, causing its
     522 *  aspect ratio to look stretched.
    419523 *
    420524 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     
    426530 *  \return 0 in case of success, -1 if an error occurred.
    427531 */
    428 int cucul_rotate_right(cucul_canvas_t *cv)
     532int cucul_stretch_right(cucul_canvas_t *cv)
    429533{
    430534    uint32_t *newchars, *newattrs;
     
    488592    cv->frames[cv->frame].width = cv->height;
    489593    cv->frames[cv->frame].height = cv->width;
    490 
    491     cv->frames[cv->frame].chars = newchars;
    492     cv->frames[cv->frame].attrs = newattrs;
    493 
    494     /* Reset the current frame shortcuts */
    495     _cucul_load_frame_info(cv);
    496 
    497     return 0;
    498 }
    499 
    500 /** \brief Rotate a canvas, 90 degrees counterclockwise (widechar version).
    501  *
    502  *  Apply a 90-degree transformation to a canvas, choosing characters
    503  *  that look like the rotated version wherever possible. Characters cells
    504  *  are rotated two-by-two. Some characters will stay unchanged by the
    505  *  process, some others will be replaced by close equivalents. Fullwidth
    506  *  characters at odd horizontal coordinates will be lost. The operation is
    507  *  not guaranteed to be reversible at all.
    508  *
    509  *  Note that the width of the canvas is divided by two and becomes the
    510  *  new height. Height is multiplied by two and becomes the new width. It
    511  *  is illegal to pass a canvas with an odd width to this function.
    512  *
    513  *  If an error occurs, -1 is returned and \b errno is set accordingly:
    514  *  - \c EBUSY The canvas is in use by a display driver and cannot be rotated.
    515  *  - \c EINVAL The canvas' width is odd.
    516  *  - \c ENOMEM Not enough memory to allocate the new canvas size. If this
    517  *    happens, the previous canvas handle is still valid.
    518  *
    519  *  \param cv The canvas to rotate right.
    520  *  \return 0 in case of success, -1 if an error occurred.
    521  */
    522 int cucul_rotate_right_wide(cucul_canvas_t *cv)
    523 {
    524     uint32_t *newchars, *newattrs;
    525     unsigned int x, y, subwidth, subheight;
    526 
    527     if(cv->refcount)
    528     {
    529         seterrno(EBUSY);
    530         return -1;
    531     }
    532 
    533     if(cv->width & 1)
    534     {
    535         seterrno(EINVAL);
    536         return -1;
    537     }
    538 
    539     /* Save the current frame shortcuts */
    540     _cucul_save_frame_info(cv);
    541 
    542     newchars = malloc(cv->width * cv->height * sizeof(uint32_t));
    543     if(!newchars)
    544         return -1;
    545 
    546     newattrs = malloc(cv->width * cv->height * sizeof(uint32_t));
    547     if(!newattrs)
    548     {
    549         free(newchars);
    550         return -1;
    551     }
    552 
    553     subwidth = cv->width / 2;
    554     subheight = cv->height;
    555 
    556     for(y = 0; y < subheight; y++)
    557     {
    558         for(x = 0; x < subwidth; x++)
    559         {
    560             uint32_t pair[2], attr1, attr2;
    561 
    562             pair[0] = cv->chars[(subwidth * y + x) * 2];
    563             attr1 = cv->attrs[(subwidth * y + x) * 2];
    564             pair[1] = cv->chars[(subwidth * y + x) * 2 + 1];
    565             attr2 = cv->attrs[(subwidth * y + x) * 2 + 1];
    566 
    567             rightpair(pair);
    568 
    569             newchars[(subheight * x + subheight - 1 - y) * 2] = pair[0];
    570             newattrs[(subheight * x + subheight - 1 - y) * 2] = attr1;
    571             newchars[(subheight * x + subheight - 1 - y) * 2 + 1] = pair[1];
    572             newattrs[(subheight * x + subheight - 1 - y) * 2 + 1] = attr2;
    573         }
    574     }
    575 
    576     free(cv->chars);
    577     free(cv->attrs);
    578 
    579     /* Swap X and Y information */
    580     x = cv->frames[cv->frame].x;
    581     y = cv->frames[cv->frame].y;
    582     cv->frames[cv->frame].x = (cv->height - 1 - y) * 2;
    583     cv->frames[cv->frame].y = x / 2;
    584 
    585     x = cv->frames[cv->frame].handlex;
    586     y = cv->frames[cv->frame].handley;
    587     cv->frames[cv->frame].handlex = (cv->height - 1 - y) * 2;
    588     cv->frames[cv->frame].handley = x / 2;
    589 
    590     cv->frames[cv->frame].width = cv->height * 2;
    591     cv->frames[cv->frame].height = cv->width / 2;
    592594
    593595    cv->frames[cv->frame].chars = newchars;
  • libcaca/trunk/test/text.c

    r1808 r1809  
    8383    free(buffer);
    8484
    85     cucul_rotate_left_wide(cv);
     85    cucul_rotate_left(cv);
    8686    buffer = cucul_export_memory(cv, "utf8", &len);
    8787    fwrite(buffer, len, 1, stdout);
Note: See TracChangeset for help on using the changeset viewer.