switch (colorMode) {
case BGR:
// Determine required buffer size and allocate buffer
numBytes = avpicture_get_size(PIX_FMT_BGR24, width, height);
buffer = new BytePointer(av_malloc(numBytes));
// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill(pFrameRGB, buffer, PIX_FMT_BGR24, width, height);
// Convert the image into BGR format that OpenCV uses
img_convert_ctx = sws_getContext(
pCodecCtx.width(), pCodecCtx.height(), pCodecCtx.pix_fmt(),
width, height, PIX_FMT_BGR24, SWS_BILINEAR, null, null, null);
if (img_convert_ctx == null) {
throw new Exception("Cannot initialize the conversion context.");
}
return_image = IplImage.createHeader(width, height, IPL_DEPTH_8U, 3);
break;
case GRAY:
numBytes = avpicture_get_size(PIX_FMT_GRAY8, width, height);
buffer = new BytePointer(av_malloc(numBytes));
avpicture_fill(pFrameRGB, buffer, PIX_FMT_GRAY8, width, height);
// Convert the image into GRAY format that OpenCV uses
img_convert_ctx = sws_getContext(
pCodecCtx.width(), pCodecCtx.height(), pCodecCtx.pix_fmt(),