Changeset 3351


Ignore:
Timestamp:
Jan 16, 2009, 3:23:14 PM (14 years ago)
Author:
Sam Hocevar
Message:

storyboard.c:

  • generate smaller thumbnail mosaics
  • process more images (1 out of 15 instead of 20)
  • try to rule out more similar images
  • cope with streams that do not advertise their picture size in the headers
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libpipi/trunk/examples/storyboard.c

    r3349 r3351  
    2525#include <pipi.h>
    2626
    27 #define STEP 20
     27#define STEP 15
    2828
    2929#define TWIDTH 90
    3030#define THEIGHT 60
    3131#define TCOLS 10
    32 #define TROWS 200
     32#define TROWS 50
    3333#define NTHUMBS (TCOLS*TROWS)
     34
     35static int similar(uint8_t *img1, uint8_t *img2);
    3436
    3537int main(int argc, char *argv[])
     
    4143    AVCodec *codec;
    4244    AVFrame *frame;
    43     struct SwsContext *sws;
     45    struct SwsContext *sws = NULL;
    4446    pipi_image_t *image;
    4547    pipi_pixels_t *p;
     
    8486    frame = avcodec_alloc_frame();
    8587
    86     sws = sws_getContext(ctx->width, ctx->height, ctx->pix_fmt,
    87                          TWIDTH, THEIGHT, PIX_FMT_RGB32,
    88                          SWS_BICUBIC, NULL, NULL, NULL);
    89 
    9088    for(;;)
    9189    {
     
    9694        if(idx == NTHUMBS || (idx > 0 && ret < 0))
    9795        {
    98             /* Only process every 20th image */
    9996            char buf[1024];
    10097            sprintf(buf, fmtstr, k++);
     
    121118        }
    122119
    123         n++;
    124 
    125         if((n % STEP) == STEP / 2)
    126         {
     120        /* Only process every 20th image */
     121        if((++n % STEP) == STEP / 2)
     122        {
     123            uint8_t *start;
    127124            int pitch = TWIDTH * TCOLS * 4;
    128             uint8_t *start;
     125            int good = 1;
    129126
    130127            start = buffer + (idx % TCOLS) * TWIDTH * 4
    131128                           + (idx / TCOLS) * TWIDTH * TCOLS * 4 * THEIGHT;
    132129
     130            if(!sws)
     131                sws = sws_getContext(ctx->width, ctx->height, ctx->pix_fmt,
     132                                     TWIDTH, THEIGHT, PIX_FMT_RGB32,
     133                                     SWS_BICUBIC, NULL, NULL, NULL);
     134
    133135            sws_scale(sws, frame->data, frame->linesize, 0, ctx->height,
    134136                      &start, &pitch);
    135137
    136             /* Now check whether the new image is really different
    137              * from the previous one (> 8% pixel changes) */
    138             if(idx == 0)
    139             {
    140                 idx++;
    141             }
    142             else
     138            if(idx > 0)
    143139            {
    144140                uint8_t *prev;
    145                 int x, y, t, a, b, changed = 0;
    146141
    147142                if(idx % TCOLS)
     
    151146                                 - TWIDTH * TCOLS * 4 * THEIGHT;
    152147
    153                 for(y = 0; y < THEIGHT; y++)
    154                     for(x = 0; x < TWIDTH; x++)
    155                     {
    156                         int ok = 0;
    157 
    158                         for(t = 0; t < 3; t++)
    159                         {
    160                             int offset = y * TWIDTH * TCOLS + x;
    161                             a = start[offset * 4 + t];
    162                             b = prev[offset * 4 + t];
    163 
    164                             if(a < b - 5 || a > b + 5)
    165                             {
    166                                 ok = 1;
    167                                 break;
    168                             }
    169                         }
    170 
    171                         changed += ok;
    172                     }
    173 
    174                 if(changed > TWIDTH * THEIGHT * 8 / 100)
    175                     idx++;
     148                /* Now check whether the new image is really different
     149                 * from the previous one (> 10% pixel changes) */
     150                if(similar(start, prev))
     151                    good = 0;
     152            }
     153
     154            if(good)
     155            {
     156                idx++;
    176157            }
    177158        }
     
    183164}
    184165
     166static int similar(uint8_t *img1, uint8_t *img2)
     167{
     168    int x, y, t, a, b, changed = 0;
     169
     170    for(y = 1; y < THEIGHT - 1; y++)
     171        for(x = 1; x < TWIDTH - 1; x++)
     172        {
     173            int offset = y * TWIDTH * TCOLS + x;
     174            int ok = 0;
     175
     176            for(t = 0; t < 3; t++)
     177            {
     178                a = 2 * img1[offset * 4 + t];
     179                a += img1[(offset - TWIDTH * TCOLS - 1) * 4 + t];
     180                a += img1[(offset - TWIDTH * TCOLS) * 4 + t];
     181                a += img1[(offset - TWIDTH * TCOLS + 1) * 4 + t];
     182                a += img1[(offset - 1) * 4 + t];
     183                a += img1[(offset + 1) * 4 + t];
     184                a += img1[(offset + TWIDTH * TCOLS - 1) * 4 + t];
     185                a += img1[(offset + TWIDTH * TCOLS) * 4 + t];
     186                a += img1[(offset + TWIDTH * TCOLS + 1) * 4 + t];
     187                a /= 10;
     188
     189                b = 2 * img2[offset * 4 + t];
     190                b += img2[(offset - TWIDTH * TCOLS - 1) * 4 + t];
     191                b += img2[(offset - TWIDTH * TCOLS) * 4 + t];
     192                b += img2[(offset - TWIDTH * TCOLS + 1) * 4 + t];
     193                b += img2[(offset - 1) * 4 + t];
     194                b += img2[(offset + 1) * 4 + t];
     195                b += img2[(offset + TWIDTH * TCOLS - 1) * 4 + t];
     196                b += img2[(offset + TWIDTH * TCOLS) * 4 + t];
     197                b += img2[(offset + TWIDTH * TCOLS + 1) * 4 + t];
     198                b /= 10;
     199
     200                if(a < b - 8 || a > b + 8)
     201                {
     202                    ok = 1;
     203                    break;
     204                }
     205            }
     206
     207            changed += ok;
     208        }
     209
     210    return changed < (TWIDTH * THEIGHT * 10 / 100);
     211}
     212
Note: See TracChangeset for help on using the changeset viewer.