Changeset 1805 for libcaca


Ignore:
Timestamp:
Jul 10, 2007, 7:02:46 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Implemented cucul_rotate_left_wide() and cucul_rotate_right_wide(). They suck massively for now.
Location:
libcaca/trunk
Files:
3 edited

Legend:

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

    r1788 r1805  
    123123int cucul_rotate_180(cucul_canvas_t *);
    124124int cucul_rotate_left(cucul_canvas_t *);
     125int cucul_rotate_left_wide(cucul_canvas_t *);
    125126int cucul_rotate_right(cucul_canvas_t *);
     127int cucul_rotate_right_wide(cucul_canvas_t *);
    126128/*  @} */
    127129
  • libcaca/trunk/cucul/transform.c

    r1788 r1805  
    304304}
    305305
     306/** \brief Rotate a canvas, 90 degrees counterclockwise (widechar version).
     307 *
     308 *  Apply a 90-degree transformation to a canvas, choosing characters
     309 *  that look like the rotated version wherever possible. Characters cells
     310 *  are rotated two-by-two. Some characters will stay unchanged by the
     311 *  process, some others will be replaced by close equivalents. Fullwidth
     312 *  characters at odd horizontal coordinates will be lost. The operation is
     313 *  not guaranteed to be reversible at all.
     314 *
     315 *  Note that the width of the canvas is divided by two and becomes the
     316 *  new height. Height is multiplied by two and becomes the new width. It
     317 *  is illegal to pass a canvas with an odd width to this function.
     318 *
     319 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     320 *  - \c EBUSY The canvas is in use by a display driver and cannot be rotated.
     321 *  - \c EINVAL The canvas' width is odd.
     322 *  - \c ENOMEM Not enough memory to allocate the new canvas size. If this
     323 *    happens, the previous canvas handle is still valid.
     324 *
     325 *  \param cv The canvas to rotate left.
     326 *  \return 0 in case of success, -1 if an error occurred.
     327 */
     328int cucul_rotate_left_wide(cucul_canvas_t *cv)
     329{
     330    uint32_t *newchars, *newattrs;
     331    unsigned int x, y, subwidth, subheight;
     332
     333    if(cv->refcount)
     334    {
     335        seterrno(EBUSY);
     336        return -1;
     337    }
     338
     339    if(cv->width & 1)
     340    {
     341        seterrno(EINVAL);
     342        return -1;
     343    }
     344
     345    /* Save the current frame shortcuts */
     346    _cucul_save_frame_info(cv);
     347
     348    newchars = malloc(cv->width * cv->height * sizeof(uint32_t));
     349    if(!newchars)
     350        return -1;
     351
     352    newattrs = malloc(cv->width * cv->height * sizeof(uint32_t));
     353    if(!newattrs)
     354    {
     355        free(newchars);
     356        return -1;
     357    }
     358
     359    subwidth = cv->width / 2;
     360    subheight = cv->height;
     361
     362    for(y = 0; y < subheight; y++)
     363    {
     364        for(x = 0; x < subwidth; x++)
     365        {
     366            uint32_t ch1, ch2, attr1, attr2;
     367
     368            ch1 = cv->chars[(subwidth * y + x) * 2];
     369            attr1 = cv->attrs[(subwidth * y + x) * 2];
     370            ch2 = cv->chars[(subwidth * y + x) * 2 + 1];
     371            attr2 = cv->attrs[(subwidth * y + x) * 2 + 1];
     372
     373            newchars[(subheight * (subwidth - 1 - x) + y) * 2] = ch1;
     374            newattrs[(subheight * (subwidth - 1 - x) + y) * 2] = attr1;
     375            newchars[(subheight * (subwidth - 1 - x) + y) * 2 + 1] = ch2;
     376            newattrs[(subheight * (subwidth - 1 - x) + y) * 2 + 1] = attr2;
     377        }
     378    }
     379
     380    free(cv->chars);
     381    free(cv->attrs);
     382
     383    /* Swap X and Y information */
     384    x = cv->frames[cv->frame].x;
     385    y = cv->frames[cv->frame].y;
     386    cv->frames[cv->frame].x = y * 2;
     387    cv->frames[cv->frame].y = (cv->width - 1 - x) / 2;
     388
     389    x = cv->frames[cv->frame].handlex;
     390    y = cv->frames[cv->frame].handley;
     391    cv->frames[cv->frame].handlex = y * 2;
     392    cv->frames[cv->frame].handley = (cv->width - 1 - x) / 2;
     393
     394    cv->frames[cv->frame].width = cv->height * 2;
     395    cv->frames[cv->frame].height = cv->width / 2;
     396
     397    cv->frames[cv->frame].chars = newchars;
     398    cv->frames[cv->frame].attrs = newattrs;
     399
     400    /* Reset the current frame shortcuts */
     401    _cucul_load_frame_info(cv);
     402
     403    return 0;
     404}
     405
    306406/** \brief Rotate a canvas, 90 degrees clockwise.
    307407 *
     
    384484    cv->frames[cv->frame].width = cv->height;
    385485    cv->frames[cv->frame].height = cv->width;
     486
     487    cv->frames[cv->frame].chars = newchars;
     488    cv->frames[cv->frame].attrs = newattrs;
     489
     490    /* Reset the current frame shortcuts */
     491    _cucul_load_frame_info(cv);
     492
     493    return 0;
     494}
     495
     496/** \brief Rotate a canvas, 90 degrees counterclockwise (widechar version).
     497 *
     498 *  Apply a 90-degree transformation to a canvas, choosing characters
     499 *  that look like the rotated version wherever possible. Characters cells
     500 *  are rotated two-by-two. Some characters will stay unchanged by the
     501 *  process, some others will be replaced by close equivalents. Fullwidth
     502 *  characters at odd horizontal coordinates will be lost. The operation is
     503 *  not guaranteed to be reversible at all.
     504 *
     505 *  Note that the width of the canvas is divided by two and becomes the
     506 *  new height. Height is multiplied by two and becomes the new width. It
     507 *  is illegal to pass a canvas with an odd width to this function.
     508 *
     509 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     510 *  - \c EBUSY The canvas is in use by a display driver and cannot be rotated.
     511 *  - \c EINVAL The canvas' width is odd.
     512 *  - \c ENOMEM Not enough memory to allocate the new canvas size. If this
     513 *    happens, the previous canvas handle is still valid.
     514 *
     515 *  \param cv The canvas to rotate right.
     516 *  \return 0 in case of success, -1 if an error occurred.
     517 */
     518int cucul_rotate_right_wide(cucul_canvas_t *cv)
     519{
     520    uint32_t *newchars, *newattrs;
     521    unsigned int x, y, subwidth, subheight;
     522
     523    if(cv->refcount)
     524    {
     525        seterrno(EBUSY);
     526        return -1;
     527    }
     528
     529    if(cv->width & 1)
     530    {
     531        seterrno(EINVAL);
     532        return -1;
     533    }
     534
     535    /* Save the current frame shortcuts */
     536    _cucul_save_frame_info(cv);
     537
     538    newchars = malloc(cv->width * cv->height * sizeof(uint32_t));
     539    if(!newchars)
     540        return -1;
     541
     542    newattrs = malloc(cv->width * cv->height * sizeof(uint32_t));
     543    if(!newattrs)
     544    {
     545        free(newchars);
     546        return -1;
     547    }
     548
     549    subwidth = cv->width / 2;
     550    subheight = cv->height;
     551
     552    for(y = 0; y < subheight; y++)
     553    {
     554        for(x = 0; x < subwidth; x++)
     555        {
     556            uint32_t ch1, ch2, attr1, attr2;
     557
     558            ch1 = cv->chars[(subwidth * y + x) * 2];
     559            attr1 = cv->attrs[(subwidth * y + x) * 2];
     560            ch2 = cv->chars[(subwidth * y + x) * 2 + 1];
     561            attr2 = cv->attrs[(subwidth * y + x) * 2 + 1];
     562
     563            newchars[(subheight * x + subheight - 1 - y) * 2] = ch1;
     564            newattrs[(subheight * x + subheight - 1 - y) * 2] = attr1;
     565            newchars[(subheight * x + subheight - 1 - y) * 2 + 1] = ch2;
     566            newattrs[(subheight * x + subheight - 1 - y) * 2 + 1] = attr2;
     567        }
     568    }
     569
     570    free(cv->chars);
     571    free(cv->attrs);
     572
     573    /* Swap X and Y information */
     574    x = cv->frames[cv->frame].x;
     575    y = cv->frames[cv->frame].y;
     576    cv->frames[cv->frame].x = (cv->height - 1 - y) * 2;
     577    cv->frames[cv->frame].y = x / 2;
     578
     579    x = cv->frames[cv->frame].handlex;
     580    y = cv->frames[cv->frame].handley;
     581    cv->frames[cv->frame].handlex = (cv->height - 1 - y) * 2;
     582    cv->frames[cv->frame].handley = x / 2;
     583
     584    cv->frames[cv->frame].width = cv->height * 2;
     585    cv->frames[cv->frame].height = cv->width / 2;
    386586
    387587    cv->frames[cv->frame].chars = newchars;
  • libcaca/trunk/test/text.c

    r1788 r1805  
    6969    free(buffer);
    7070
    71     cucul_rotate_left(cv);
     71    cucul_rotate_left_wide(cv);
    7272    buffer = cucul_export_memory(cv, "utf8", &len);
    7373    fwrite(buffer, len, 1, stdout);
Note: See TracChangeset for help on using the changeset viewer.