⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imlib2.c

📁 ffmpeg的完整源代码和作者自己写的文档。不但有在Linux的工程哦
💻 C
📖 第 1 页 / 共 2 页
字号:

        if (rgbtxt)
            f = fopen(rgbtxt, "r");
        else
        {
            f = fopen("/usr/share/X11/rgb.txt", "r");
            if (!f)
                f = fopen("/usr/lib/X11/rgb.txt", "r");
        }
        if (!f) {
            fprintf(stderr, "Failed to find RGB color names file\n");
            return -1;
        }
        while (fgets(buff, sizeof(buff), f)) {
            int r, g, b;
            char colname[80];

            if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
                strcasecmp(colname, color) == 0) {
                ci->r = r;
                ci->g = g;
                ci->b = b;
                /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
                done = 1;
                break;
            }
        }
        fclose(f);
        if (!done) {
            fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
            return -1;
        }
    } else if (ci->eval_colors) {
        if (!(ci->eval_r = ff_parse(ci->expr_R, const_names, NULL, NULL, NULL, NULL, &error))){
            av_log(NULL, AV_LOG_ERROR, "Couldn't parse R expression '%s': %s\n", ci->expr_R, error);
            return -1;
        }
        if (!(ci->eval_g = ff_parse(ci->expr_G, const_names, NULL, NULL, NULL, NULL, &error))){
            av_log(NULL, AV_LOG_ERROR, "Couldn't parse G expression '%s': %s\n", ci->expr_G, error);
            return -1;
        }
        if (!(ci->eval_b = ff_parse(ci->expr_B, const_names, NULL, NULL, NULL, NULL, &error))){
            av_log(NULL, AV_LOG_ERROR, "Couldn't parse B expression '%s': %s\n", ci->expr_B, error);
            return -1;
        }
    }

    if (ci->expr_A) {
        if (!(ci->eval_a = ff_parse(ci->expr_A, const_names, NULL, NULL, NULL, NULL, &error))){
            av_log(NULL, AV_LOG_ERROR, "Couldn't parse A expression '%s': %s\n", ci->expr_A, error);
            return -1;
        }
    } else {
        ci->a = 255;
    }

    if (!(ci->eval_colors || ci->eval_a))
        imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);

    /* load the image (for example, credits for a movie) */
    if (ci->fileImage) {
        ci->imageOverlaid = imlib_load_image_immediately(ci->fileImage);
        if (!(ci->imageOverlaid)){
            av_log(NULL, AV_LOG_ERROR, "Couldn't load image '%s'\n", ci->fileImage);
            return -1;
        }
        imlib_context_set_image(ci->imageOverlaid);
        ci->imageOverlaid_width  = imlib_image_get_width();
        ci->imageOverlaid_height = imlib_image_get_height();
    }

    if (!(ci->eval_x = ff_parse(ci->expr_x, const_names, NULL, NULL, NULL, NULL, &error))){
        av_log(NULL, AV_LOG_ERROR, "Couldn't parse x expression '%s': %s\n", ci->expr_x, error);
        return -1;
    }

    if (!(ci->eval_y = ff_parse(ci->expr_y, const_names, NULL, NULL, NULL, NULL, &error))){
        av_log(NULL, AV_LOG_ERROR, "Couldn't parse y expression '%s': %s\n", ci->expr_y, error);
        return -1;
    }

    return 0;
}

static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
{
    CachedImage *cache;

    for (cache = ci->cache; cache; cache = cache->next) {
        if (width == cache->width && height == cache->height)
            return cache->image;
    }

    return NULL;
}

static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
{
    CachedImage *cache = av_mallocz(sizeof(*cache));

    cache->image = image;
    cache->width = width;
    cache->height = height;
    cache->next = ci->cache;
    ci->cache = cache;
}

void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
{
    ContextInfo *ci = (ContextInfo *) ctx;
    AVPicture picture1;
    Imlib_Image image;
    DATA32 *data;

    image = get_cached_image(ci, width, height);

    if (!image) {
        image = imlib_create_image(width, height);
        put_cached_image(ci, image, width, height);
    }

    imlib_context_set_image(image);
    data = imlib_image_get_data();

    avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGB32, width, height);

    // if we already got a SWS context, let's realloc if is not re-useable
    ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
                                width, height, pix_fmt,
                                width, height, PIX_FMT_RGB32,
                                sws_flags, NULL, NULL, NULL);
    if (ci->toRGB_convert_ctx == NULL) {
        av_log(NULL, AV_LOG_ERROR,
               "Cannot initialize the toRGB conversion context\n");
        return;
    }

// img_convert parameters are          2 first destination, then 4 source
// sws_scale   parameters are context, 4 first source,      then 2 destination
    sws_scale(ci->toRGB_convert_ctx,
             picture->data, picture->linesize, 0, height,
             picture1.data, picture1.linesize);

    imlib_image_set_has_alpha(0);

    {
        int wid, hig, h_a, v_a;
        char buff[1000];
        char tbuff[1000];
        char *tbp = ci->text;
        time_t now = time(0);
        char *p, *q;
        int y;

        double const_values[]={
            M_PI,
            M_E,
            ci->frame_number,         // frame number (starting at zero)
            height,                   // frame height
            width,                    // frame width
            ci->imageOverlaid_height, // image height
            ci->imageOverlaid_width,  // image width
            ci->x,                    // previous x
            ci->y,                    // previous y
            0
        };

        if (ci->file) {
            int fd = open(ci->file, O_RDONLY);

            if (fd < 0) {
                tbp = "[File not found]";
            } else {
                int l = read(fd, tbuff, sizeof(tbuff) - 1);

                if (l >= 0) {
                    tbuff[l] = 0;
                    tbp = tbuff;
                } else {
                    tbp = "[I/O Error]";
                }
                close(fd);
            }
        }

        if (tbp)
            strftime(buff, sizeof(buff), tbp, localtime(&now));
        else if (!(ci->imageOverlaid))
            strftime(buff, sizeof(buff), "[No data]", localtime(&now));

        ci->x = ff_parse_eval(ci->eval_x, const_values, ci);
        ci->y = ff_parse_eval(ci->eval_y, const_values, ci);
        y = ci->y;

        if (ci->eval_a) {
            ci->a = ff_parse_eval(ci->eval_a, const_values, ci);
        }

        if (ci->eval_colors) {
            ci->r = ff_parse_eval(ci->eval_r, const_values, ci);
            ci->g = ff_parse_eval(ci->eval_g, const_values, ci);
            ci->b = ff_parse_eval(ci->eval_b, const_values, ci);
        }

        if (ci->eval_colors || ci->eval_a) {
            imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);
        }

        if (!(ci->imageOverlaid))
        for (p = buff; p; p = q) {
            q = strchr(p, '\n');
            if (q)
                *q++ = 0;

            imlib_text_draw_with_return_metrics(ci->x, y, p, &wid, &hig, &h_a, &v_a);
            y += v_a;
        }

        if (ci->imageOverlaid) {
            imlib_context_set_image(image);
            imlib_blend_image_onto_image(ci->imageOverlaid, 0,
                0, 0, ci->imageOverlaid_width, ci->imageOverlaid_height,
                ci->x, ci->y, ci->imageOverlaid_width, ci->imageOverlaid_height);
        }

    }

    ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
                                    width, height, PIX_FMT_RGB32,
                                    width, height, pix_fmt,
                                    sws_flags, NULL, NULL, NULL);
    if (ci->fromRGB_convert_ctx == NULL) {
        av_log(NULL, AV_LOG_ERROR,
               "Cannot initialize the fromRGB conversion context\n");
        return;
    }
// img_convert parameters are          2 first destination, then 4 source
// sws_scale   parameters are context, 4 first source,      then 2 destination
    sws_scale(ci->fromRGB_convert_ctx,
             picture1.data, picture1.linesize, 0, height,
             picture->data, picture->linesize);

    ci->frame_number++;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -