Ignore:
Timestamp:
Aug 2, 2008, 2:01:00 AM (12 years ago)
Author:
Sam Hocevar
Message:
  • opencv.c: bring the OpenCV codec up to date.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libpipi/trunk/pipi/codec/opencv.c

    r2629 r2631  
    3535{
    3636    pipi_image_t *img;
    37     IplImage *priv = cvLoadImage(name, -1);
     37    IplImage *priv = cvLoadImage(name, 1);
    3838
    3939    if(!priv)
    4040        return NULL;
    4141
    42     img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
    43     memset(img, 0, sizeof(pipi_image_t));
     42    img = pipi_new(priv->width, priv->height);
    4443
    45     img->w = priv->width;
    46     img->h = priv->height;
    47 
    48     img->p[PIPI_PIXELS_RGBA32].pixels = priv->imageData;
    49     img->p[PIPI_PIXELS_RGBA32].w = priv->width;
    50     img->p[PIPI_PIXELS_RGBA32].h = priv->height;
    51     img->p[PIPI_PIXELS_RGBA32].pitch = priv->widthStep;
    52     img->last_modified = PIPI_PIXELS_RGBA32;
     44    img->p[PIPI_PIXELS_BGR24].pixels = priv->imageData;
     45    img->p[PIPI_PIXELS_BGR24].w = priv->width;
     46    img->p[PIPI_PIXELS_BGR24].h = priv->height;
     47    img->p[PIPI_PIXELS_BGR24].pitch = priv->widthStep;
     48    img->last_modified = PIPI_PIXELS_BGR24;
    5349
    5450    img->codec_priv = (void *)priv;
    55     img->codec_format = PIPI_PIXELS_RGBA32;
    56 
    57     return img;
    58 }
    59 
    60 pipi_image_t *pipi_new_opencv(int width, int height)
    61 {
    62     pipi_image_t *img;
    63     IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 4);
    64 
    65     if(!priv)
    66         return NULL;
    67 
    68     img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
    69     memset(img, 0, sizeof(pipi_image_t));
    70 
    71     img->w = priv->width;
    72     img->h = priv->height;
    73 
    74     img->p[PIPI_PIXELS_RGBA32].pixels = priv->imageData;
    75     img->p[PIPI_PIXELS_RGBA32].w = priv->width;
    76     img->p[PIPI_PIXELS_RGBA32].h = priv->height;
    77     img->p[PIPI_PIXELS_RGBA32].pitch = priv->widthStep;
    78     img->last_modified = PIPI_PIXELS_RGBA32;
    79 
    80     img->codec_priv = (void *)priv;
    81     img->codec_format = PIPI_PIXELS_RGBA32;
     51    img->codec_format = PIPI_PIXELS_BGR24;
    8252
    8353    return img;
     
    8959    iplimg = (IplImage *)img->codec_priv;
    9060    cvReleaseImage(&iplimg);
    91 
    92     free(img);
    9361}
    9462
    9563void pipi_save_opencv(pipi_image_t *img, const char *name)
    9664{
     65    if(!img->codec_priv)
     66    {
     67        IplImage *priv = cvCreateImage(cvSize(img->w, img->h),
     68                                       IPL_DEPTH_8U, 3);
     69
     70        /* FIXME: check pitch differences here */
     71        if(img->last_modified == PIPI_PIXELS_BGR24)
     72        {
     73            memcpy(priv->imageData, img->p[PIPI_PIXELS_BGR24].pixels,
     74                   3 * img->w * img->h);
     75            free(img->p[PIPI_PIXELS_BGR24].pixels);
     76        }
     77
     78        img->p[PIPI_PIXELS_BGR24].pixels = priv->imageData;
     79        img->p[PIPI_PIXELS_BGR24].w = priv->width;
     80        img->p[PIPI_PIXELS_BGR24].h = priv->height;
     81        img->p[PIPI_PIXELS_BGR24].pitch = priv->widthStep;
     82
     83        img->codec_priv = (void *)priv;
     84        img->codec_format = PIPI_PIXELS_BGR24;
     85    }
     86
    9787    pipi_getpixels(img, img->codec_format);
    9888    cvSaveImage(name, img->codec_priv);
Note: See TracChangeset for help on using the changeset viewer.