Changeset 3531


Ignore:
Timestamp:
May 27, 2009 7:04:41 AM (6 years ago)
Author:
sam
Message:

img2twit: store w+1,h+1 instead of w,h since neither width nor height can
be zero.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libpipi/trunk/examples/img2twit.cpp

    r3530 r3531  
    6363
    6464/* The maximum image size we want to support */
    65 #define MAX_W 4000
    66 #define MAX_H 4000
     65#define RANGE_W 4000
     66#define RANGE_H 4000
    6767
    6868/* How does the algorithm work: one point per cell, or two */
     
    443443}
    444444
     445static inline void index2cell(int index, int *dx, int *dy)
     446{
     447    // This check will be used once we add points to our list
     448    //if(index < dw * dh * POINTS_PER_CELL)
     449    {
     450        *dx = (index / POINTS_PER_CELL) % dw;
     451        *dy = (index / POINTS_PER_CELL) / dw;
     452    }
     453}
     454
    445455static inline void set_point(int index, float x, float y, float r,
    446456                             float g, float b, float s)
    447457{
    448     int dx = (index / POINTS_PER_CELL) % dw;
    449     int dy = (index / POINTS_PER_CELL) / dw;
     458    int dx, dy;
     459
     460    index2cell(index, &dx, &dy);
    450461
    451462    float fx = (x - dx * RANGE_X) / RANGE_X;
     
    469480{
    470481    uint32_t pt = points[index];
    471 
    472     unsigned int dx = (index / POINTS_PER_CELL) % dw;
    473     unsigned int dy = (index / POINTS_PER_CELL) / dw;
     482    int dx, dy;
     483
     484    index2cell(index, &dx, &dy);
    474485
    475486    *s = int2fullrange(pt % RANGE_S, RANGE_S); pt /= RANGE_S;
     
    937948    }
    938949
     950    bitstack b(MAX_MSG_LEN); /* We cannot declare this before, because
     951                              * MAX_MSG_LEN wouldn't be defined. */
     952
    939953    /* Autodetect charset if decoding, otherwise switch to CJK. */
    940954    if(dstname)
     
    973987    /* Precompute bit allocation */
    974988    NUM_CHARACTERS = count_unichars();
     989
     990    if(dstname)
     991    {
     992        /* Decoding mode: find each character's index in our character
     993         * list, and push it to our wonderful custom bitstream. */
     994        for(int i = MAX_MSG_LEN; i--; )
     995            b.push(uni2index(unicode_data[i]), NUM_CHARACTERS);
     996
     997        /* Read width and height from bitstream */
     998        src = NULL;
     999        width = b.pop(RANGE_W) + 1;
     1000        height = b.pop(RANGE_H) + 1;
     1001    }
     1002    else
     1003    {
     1004        /* Argument given: open image for encoding */
     1005        src = pipi_load(srcname);
     1006
     1007        if(!src)
     1008        {
     1009            fprintf(stderr, "Error loading %s\n", srcname);
     1010            return EXIT_FAILURE;
     1011        }
     1012
     1013        width = pipi_get_image_width(src);
     1014        height = pipi_get_image_height(src);
     1015    }
     1016
     1017    if(width <= 0 || height <= 0 || width > RANGE_W || height > RANGE_H)
     1018    {
     1019        fprintf(stderr, "Error: image size %ix%i is out of bounds\n",
     1020                width, height);
     1021        return EXIT_FAILURE;
     1022    }
     1023
    9751024    TOTAL_BITS = MAX_MSG_LEN * logf(NUM_CHARACTERS) / logf(2);
    976     HEADER_BITS = logf(MAX_W * MAX_H) / logf(2);
     1025    HEADER_BITS = logf(RANGE_W * RANGE_H) / logf(2);
    9771026    DATA_BITS = TOTAL_BITS - HEADER_BITS;
    9781027#if POINTS_PER_CELL == 1
     
    9881037    MAX_ITERATIONS = ITERATIONS_PER_POINT * POINTS_PER_CELL * TOTAL_CELLS;
    9891038
    990     bitstack b(MAX_MSG_LEN); /* We cannot declare this before, because
    991                               * MAX_MSG_LEN wouldn't be defined. */
    992 
    993     if(dstname)
    994     {
    995         /* Decoding mode: find each character's index in our character
    996          * list, and push it to our wonderful custom bitstream. */
    997         for(int i = MAX_MSG_LEN; i--; )
    998             b.push(uni2index(unicode_data[i]), NUM_CHARACTERS);
    999 
    1000         /* Read width and height from bitstream */
    1001         src = NULL;
    1002         width = b.pop(MAX_W);
    1003         height = b.pop(MAX_H);
    1004     }
    1005     else
    1006     {
    1007         /* Argument given: open image for encoding */
    1008         src = pipi_load(srcname);
    1009 
    1010         if(!src)
    1011         {
    1012             fprintf(stderr, "Error loading %s\n", srcname);
    1013             return EXIT_FAILURE;
    1014         }
    1015 
    1016         width = pipi_get_image_width(src);
    1017         height = pipi_get_image_height(src);
    1018     }
    1019 
    10201039    /* Compute "best" w/h ratio */
    10211040    dw = 1; dh = TOTAL_CELLS;
     
    10431062        fprintf(stderr, "Available characters: %i\n", NUM_CHARACTERS);
    10441063        fprintf(stderr, "Available bits: %f\n", TOTAL_BITS);
    1045         fprintf(stderr, "Maximum image resolution: %ix%i\n", MAX_W, MAX_H);
     1064        fprintf(stderr, "Width/Height ranges: %ix%i\n", RANGE_W, RANGE_H);
    10461065        fprintf(stderr, "Image resolution: %ix%i\n", width, height);
    10471066        fprintf(stderr, "Header bits: %f\n", HEADER_BITS);
    10481067        fprintf(stderr, "Bits available for data: %f\n", DATA_BITS);
     1068        fprintf(stderr, "X/Y/Red/Green/Blue/Extra ranges: %i %i %i %i %i %i\n",
     1069                RANGE_X, RANGE_Y, RANGE_R, RANGE_G, RANGE_B, RANGE_S);
    10491070        fprintf(stderr, "Cell bits: %f\n", CELL_BITS);
    10501071        fprintf(stderr, "Available cells: %i\n", TOTAL_CELLS);
     
    12111232        for(int i = 0; i < npoints; i++)
    12121233            b.push(points[i], RANGE_SYXRGB);
    1213         b.push(height, MAX_H);
    1214         b.push(width, MAX_W);
     1234        b.push(height - 1, RANGE_H);
     1235        b.push(width - 1, RANGE_W);
    12151236
    12161237        /* Pop Unicode characters from the bitstream and print them */
Note: See TracChangeset for help on using the changeset viewer.