0

我刚刚使用带有位图锁定位的示例在我的 c++ 项目中创建了一个新函数:

void GetFrameData(Bitmap ^b)
    {

        typedef System::Drawing::Rectangle R;
        R rect = R(0,0,b->Width,b->Height);
        System::Drawing::Imaging::BitmapData^ bmpData = b->LockBits( rect, System::Drawing::Imaging::ImageLockMode::ReadWrite, b->PixelFormat );
        // Get the address of the first line.
        IntPtr ptr = bmpData->Scan0;

        // Declare an array to hold the bytes of the bitmap. 
        // This code is specific to a bitmap with 24 bits per pixels. 
       int bytes = Math::Abs(bmpData->Stride) * b->Height;
       array<Byte>^rgbValues = gcnew array<Byte>(bytes);

       // Copy the RGB values into the array.
      System::Runtime::InteropServices::Marshal::Copy( ptr, rgbValues, 0, bytes );

      // Set every third value to 255.   
     for ( int counter = 2; counter < rgbValues->Length; counter += 3 )
      rgbValues[ counter ] = 255;

     // Copy the RGB values back to the bitmap
    System::Runtime::InteropServices::Marshal::Copy( rgbValues, 0, ptr, bytes );

    // Unlock the bits.
    b->UnlockBits( bmpData );
    }

该函数从 CSHARP 获取位图文件,不知何故我需要使用此锁定位将像素从位图复制到此函数:

void Encoder_start(char *filename)
{
    printf("Encode video file %s\n", filename);
    /* open it */
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }
//    f = fopen(filename, "wb");
    errn = fopen_s(&f,filename, "wb");

    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }


    frame = avcodec_alloc_frame();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(1);
    }

    frame->format = c->pix_fmt;
    frame->width  = c->width;
    frame->height = c->height;
    /* the image can be allocated by any means and av_image_alloc() is
     * just the most convenient way if av_malloc() is to be used */
//    ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,
  //                       c->pix_fmt, 32);

     frame->data[0] = (uint8_t*)av_malloc(1000000);
     frame->data[1] = (uint8_t*)av_malloc(1000000);
     frame->data[2] = (uint8_t*)av_malloc(1000000);
     frame->data[3] = (uint8_t*)av_malloc(1000000);
     frame->data[4] = (uint8_t*)av_malloc(1000000);
     frame->data[5] = (uint8_t*)av_malloc(1000000);
     frame->data[6] = (uint8_t*)av_malloc(1000000);
     frame->data[7] = (uint8_t*)av_malloc(1000000);

     if (frame->data[7]==0) {
    //if (ret < 0) {
        fprintf(stderr, "Could not allocate raw picture buffer\n");
        exit(1);
    }

     total_frame_counter=0;
}



void Encoder_push_frame()
{
    /* encode 1 second of video */
   // for(i=0;i<25;i++) {
        av_init_packet(&pkt);
        pkt.data = NULL;    // packet data will be allocated by the encoder
        pkt.size = 0;
        fflush(stdout);
        /* prepare a dummy image */
        /* Y */
        for(y=0;y<c->height;y++) {
            for(x=0;x<c->width;x++) {
                frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
            }
        }
        /* Cb and Cr */
        for(y=0;y<c->height/2;y++) {
            for(x=0;x<c->width/2;x++) {
                frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
                frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
            }
        }
        frame->pts = total_frame_counter;

        total_frame_counter++;

        /* encode the image */
        ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding frame\n");
            exit(1);
        }
        if (got_output) {
            printf("Write frame %3d (size=%5d)\n", total_frame_counter, pkt.size);
            fwrite(pkt.data, 1, pkt.size, f);
            av_free_packet(&pkt);
        }
   // }

}

我不确定如何填充以及我将每个位置设置为 1000000 的 av_malloc 以进行测试。

并且 Encoder_push_frame 函数应该获取像素,或者也许它与 av_malloc 的想法是用来自位图的像素填充 push_frame 函数中的图像。

我尝试了很多方法,但它根本不起作用。

编辑

我正在使用这段代码:

void GetFrameData(Bitmap ^b)
    {
        auto bmpData = b->LockBits( System::Drawing::Rectangle(0,0,b->Width,b->Height), ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb );

    // The beginning of the bitmap data in memory
    char* top = bmpData->Scan0.ToPointer();

    // find bitmap data for the top row (if bottom-up bitmap, this is at the end)
    if (bmpData->Stride < 0) {
        top += Stride * (1 - b->Height);
    }

    for( int y = 0; y < b->Height; ++y ) {
        RGBTRIPLE* row = (RGBTRIPLE*)(top + y * bmpData->Stride);

        // saturate red channel
        for( int x = 0; x < b->Width; ++x ) {
            row[x].rgbtBlue = 255;
            row[x].rgbtGreen = 255;
            row[x].rgbtRed = 255;
    }

    // Unlock the bits.
    b->UnlockBits( bmpData );
    }

在 Rectangle 中,我必须将其更改为: System::Drawing::Rectangle 如果不是,那么我在 Rectangle 上有一个错误,它与另一个 Rectangle 发生冲突。但是现在改变它之后,我在这一行的 bmpData 上有一个错误:

char* top = bmpData->Scan0.ToPointer();

IntelliSense:“void *”类型的值不能用于初始化“char *”类型的实体

第二个错误出现在 Stride 上:

top += Stride * (1 - b->Height);

错误:智能感知:标识符“步幅”未定义

我该如何解决这个错误。

和:

将 for 循环更改为:

for( int x = 0; x < b->Width; ++x ) {
            row[x].rgbtBlue = 255;
            row[x].rgbtGreen = 255;
            row[x].rgbtRed = 255;

我改变它的原因是你做了 row[x].R = 255; 但是 R 不存在/未定义,所以我应该怎么做才能解决它?(我想我做错了)。

4

1 回答 1

0

不要费心将数据复制Scan0到新数组中。位图数据不受垃圾收集器移动的影响,C++ 完全能够使用指针转换来处理数据。

就像是

#include <windows.h>
void GetFrameData(Bitmap ^b)
{
    using System::Drawing::Imaging::ImageLockMode;
    using System::Drawing::Imaging::PixelFormat;
    using System::Drawing::Rectangle;
    auto bmpData = b->LockBits( Rectangle(0,0,b->Width,b->Height), ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb );

    // The beginning of the bitmap data in memory
    char* top = (char*)bmpData->Scan0.ToPointer();

    // find bitmap data for the top row (if bottom-up bitmap, this is at the end)
    if (bmpData->Stride < 0) {
        top += bmpData->Stride * (1 - b->Height);
    }

    for( int y = 0; y < b->Height; ++y ) {
        RGBTRIPLE* row = (RGBTRIPLE*)(top + y * bmpData->Stride);

        // saturate red channel
        for( int x = 0; x < b->Width; ++x ) {
           row[x].rgbtRed = 255;
    }

    // Unlock the bits.
    b->UnlockBits( bmpData );
}

现在,您可以在填写 av_format 图像时使用这些 RGB 值来计算 Y/Cr/Cb 值。(或者只是告诉 av_format 图像是 RGB)

于 2013-04-28T20:41:40.667 回答