weird image while trying to compress YUV image to jpeg using libjpeg - c++

I am using Qt,OpenCV and libJpeg to compress a YUV422 image, but the output is not correct.
if I convert the yuv to rgb and then compress, I get a correct output, but what I understand that Jpeg is using YUV internally , that's why I want to remove redundancy.
Here is my code:
bool ipl2jpeg(IplImage *frame, unsigned char **outbuffer, long unsigned int *outlen) {
unsigned char *outdata = (uchar *) frame->imageData;
struct jpeg_compress_struct cinfo ;
struct jpeg_error_mgr jerr;
JSAMPROW row_ptr[1];
int row_stride;
*outbuffer = NULL;
*outlen = 0;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo, outbuffer, outlen);
cinfo.image_width = frame->width;
cinfo.image_height = frame->height;
cinfo.input_components = frame->nChannels;
cinfo.in_color_space = JCS_YCbCr;
jpeg_set_defaults(&cinfo);
jpeg_set_quality (&cinfo,100 , true);
jpeg_start_compress(&cinfo, TRUE);
row_stride = frame->width *2;// frame->nChannels;
while (cinfo.next_scanline < cinfo.image_height) {
/* jpeg_write_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could pass
* more than one scanline at a time if that's more convenient.
*/
row_ptr[0] = &outdata[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return true;
}
What is wrong ??
Here is the YUV image

the image file provided (frame-3.raw) is 640x480 in YUYV format
the following code
JSAMPROW row_pointer[1];
row_pointer[0] = row_buf;
while (cinfo.next_scanline < cinfo.image_height) {
unsigned i, j;
unsigned offset = cinfo.next_scanline * cinfo.image_width * 2;
for (i = 0, j = 0; i < cinfo.image_width*2; i += 4, j += 6) {
row_buf[j + 0] = buf[offset + i + 0]; // Y
row_buf[j + 1] = buf[offset + i + 1]; // U
row_buf[j + 2] = buf[offset + i + 3]; // V
row_buf[j + 3] = buf[offset + i + 2]; // Y
row_buf[j + 4] = buf[offset + i + 1]; // U
row_buf[j + 5] = buf[offset + i + 3]; // V
}
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
to be put in between jpeg_start_compress() and jpeg_finish_compress() creates a JPEG file correctly without color space conversion (but with upsampling)
jason_s's code looks correct as well

I don't think libjpeg will accept subsampled image data as input. So, you'll have to un-subsample it first.
You could allocate a temporary row buffer, and convert each row, something like this:
for (i=0; i<frame->width; i++) {
tmpbuf[i*3+0] = outdata[cinfo.next_scanline * row_stride + i*2];
tmpbuf[i*3+1] = outdata[cinfo.next_scanline * row_stride + (i-i%2)*2+1];
tmpbuf[i*3+2] = outdata[cinfo.next_scanline * row_stride + (i-i%2)*2+3];
}
row_ptr[0] = tmpbuf;

Related

Generate Image from generated byte array in UWP vc++

Reference with this Question & answer by #Decade Moon
How can i use that method for generate image from byte array instead of image file.
i tried like below but nothing works. no image are shown
std::vector<char> data= std::vector<char>(imgx->Height * imgx->Width * 4);
int offset;
for (int row = 0; row < imgx->Height; row++)
{
for (int col = 0; col < imgx->Width; col++)
{
offset = (row * (int)(imgx->Width * 4)) + (col * 4);
data[offset] = 0x58; // Red
data[offset + 1] = 0x58; // Green
data[offset + 2] = 0x58; // Blue
data[offset + 3] = 0x58; // Alpha
}
};
My approach is little bit different from the reply you reffered to, but it works pretty well.
#include <wrl.h>
#include <robuffer.h>
using namespace Windows::UI::Xaml::Media::Imaging;
using namespace Windows::Storage::Streams;
using namespace Microsoft::WRL;
typedef uint8 byte;
byte* GetPointerToPixelData(IBuffer^ pixelBuffer, unsigned int *length)
{
if (length != nullptr)
{
*length = pixelBuffer ->Length;
}
// Query the IBufferByteAccess interface.
ComPtr<IBufferByteAccess> bufferByteAccess;
reinterpret_cast<IInspectable*>(pixelBuffer)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));
// Retrieve the buffer data.
byte* pixels = nullptr;
bufferByteAccess->Buffer(&pixels);
return pixels;
}
MainPage::MainPage()
{
InitializeComponent();
auto bitmap = ref new WriteableBitmap(50, 50);
image->Source = bitmap;
unsigned int length;
byte* sourcePixels = GetPointerToPixelData(bitmap->PixelBuffer, &length);
const unsigned int width = bitmap->PixelWidth;
const unsigned int height = bitmap->PixelHeight;
create_async([this, width, height, sourcePixels] {
byte* temp = sourcePixels;
// generate RED - BLUE gradient
for(unsigned int k = 0; k < height; k++) {
for (unsigned int i = 0; i < (width * 4); i += 4) {
int pos = k * (width * 4) + (i);
temp[pos] = (byte)(0xFF * k / (float)height); // B
temp[pos + 1] = 0x0; // G
temp[pos + 2] = 0xFF - (byte)(0xFF * k / (float)height); // R
temp[pos + 3] = 0xFF; // A
}
}
});
}

Bitmap blur in c++

iam writing a program to represent a blur efect on a bitmap.
Blur efect code:
for (xx = 0; xx < bitmapInfoHeader.biWidth; xx++)
{
for (yy = 0; yy <bitmapInfoHeader.biHeight; yy++)
{
avgB = avgG = avgR = 0;
Counter = 0;
for (x = xx; x < bitmapInfoHeader.biWidth && x < xx + blurSize; x++)
{
for (y = yy; y < bitmapInfoHeader.biHeight && y < yy + blurSize; y++)
{
avgB += bitmapImage[x *3 + y*bitmapInfoHeader.biWidth * 3 + 0]; //bitmapimage[x][y];
avgG += bitmapImage[x *3 + y*bitmapInfoHeader.biWidth * 3 + 1];
avgR += bitmapImage[x *3 + y*bitmapInfoHeader.biWidth * 3 + 2];
Counter++;
}
}
avgB = avgB / Counter;
avgG = avgG / Counter;
avgR = avgR / Counter;
bitmapImage[xx * 3 + yy*bitmapInfoHeader.biWidth * 3 + 0] = avgB;
bitmapImage[xx * 3 + yy*bitmapInfoHeader.biWidth * 3 + 1] = avgG;
bitmapImage[xx * 3 + yy*bitmapInfoHeader.biWidth * 3 + 2] = avgR;
}
}
And my output function which take the blured Array "bitmapImage" to a new file:
out = fopen(file,"wb");
fwrite(&bitmapInfoHeader, sizeof(char), sizeof(BITMAPINFOHEADER), out);
fwrite(&bitmapFileHeader, sizeof(char), sizeof(BITMAPFILEHEADER), out);
fseek(out, sizeof(char)*bitmapFileHeader.bfOffBits, SEEK_SET);
fwrite(bitmapImage, sizeof(char), bitmapInfoHeader.biSizeImage, out);
The reading function:
BITMAPINFOHEADER bitmapInfoHeader;
FILE *filePtr,*out;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage = nullptr;
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
return NULL;
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
//bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
bitmapImage = new unsigned char[bitmapInfoHeader.biSizeImage];
fread(bitmapImage, sizeof(char), bitmapInfoHeader.biSizeImage,filePtr);
*size = bitmapInfoHeader.biSizeImage;
When i compile the program a new bmp file is created but the image viewer cant show it and thorw an error.
So what am doing wrong here? is the blur algorytm good?

C++: BMP rotate image

Ok guys, it's the third time I'm posting the same question (previous are here and here).
Now at this time I will try to explain what's my problem:
So first them all, I need to rotate a .bmp image and it's not rotate correctly. But I don't need to rotate a random image with extension .bmp, I need to rotate this one. I've tried with many other images and all of them was rotated correctly, except mine.
In this moment my code it works just for 180-degree, how could make it to works on any degree which is multiple of 90-degree (I need to rotate my image just with 90, 180 or 270 degrees, not more).
I don't need any kind of external library for this code like CImage, OpenCV, ImageMagik and so on... I need to make this code to work.
So yeh, that's it. And here you can find my actual result.
CODE:
#include <array>
using namespace std;
struct BMP {
int width;
int height;
unsigned char header[54];
unsigned char *pixels;
int row_padded;
int size_padded;
};
void writeBMP(string filename, BMP image) {
string fileName = "Output Files\\" + filename;
FILE *out = fopen(fileName.c_str(), "wb");
fwrite(image.header, sizeof(unsigned char), 54, out);
unsigned char tmp;
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width * 3; j += 3) {
//Convert(B, G, R) to(R, G, B)
tmp = image.pixels[j];
image.pixels[j] = image.pixels[j + 2];
image.pixels[j + 2] = tmp;
}
}
fwrite(image.pixels, sizeof(unsigned char), image.size_padded, out);
fclose(out);
}
BMP readBMP(string filename) {
BMP image;
string fileName = "Input Files\\" + filename;
FILE *in = fopen(fileName.c_str(), "rb");
fread(image.header, sizeof(unsigned char), 54, in); // read the 54-byte header
// extract image height and width from header
image.width = *(int *) &image.header[18];
image.height = *(int *) &image.header[22];
image.row_padded = (image.width * 3 + 3) & (~3); // ok size of a single row rounded up to multiple of 4
image.size_padded = image.row_padded * image.height; // padded full size
image.pixels = new unsigned char[image.size_padded]; // yeah !
if (fread(image.pixels, sizeof(unsigned char), image.size_padded, in) == image.size_padded) {
unsigned char tmp;
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width * 3; j += 3) {
//Convert (B, G, R) to (R, G, B)
tmp = image.pixels[j];
image.pixels[j] = image.pixels[j + 2];
image.pixels[j + 2] = tmp;
}
}
}
fclose(in);
return image;
}
BMP rotate(BMP image, double degree) {
BMP newImage = image;
unsigned char *pixels = new unsigned char[image.size_padded];
int height = image.height;
int width = image.width;
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
pixels[(x * width + y) * 3 + 0] = image.pixels[((height - 1 - x) * width + (width - 1 - y)) * 3 + 0];
pixels[(x * width + y) * 3 + 1] = image.pixels[((height - 1 - x) * width + (width - 1 - y)) * 3 + 1];
pixels[(x * width + y) * 3 + 2] = image.pixels[((height - 1 - x) * width + (width - 1 - y)) * 3 + 2];
}
}
newImage.pixels = pixels;
return newImage;
}
int main() {
BMP image = readBMP("Input-1.bmp");
image = rotate(image, 180);
writeBMP("Output.bmp", image);
return 0;
}
You have major memory leak. pixels = new unsigned char[size]; must be freed otherwise there is potentially several megabytes leak with every rotation. You have to rewrite the function to keep track of memory allocations.
When you rotate the image by 90 or 270 of the image, the widht/height of image changes. The size may change too because of padding. The new dimension has to be recorded in header file.
In C++ you can use fopen, but std::fstream is preferred.
Here is an example which works in Windows for 24bit images only. In Big-endian systems you can't use memcpy the way I used it below.
Note, this is for practice only. As #datenwolf explained you should use a library for real applications. Most standard libraries such Windows GDI library (basic drawing functions) offer solution for these common tasks.
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
bool rotate(char *src, char *dst, BITMAPINFOHEADER &bi, int angle)
{
//In 24bit image, the length of each row must be multiple of 4
int padw = 4 - ((bi.biWidth * 3) % 4);
if(padw == 4) padw = 0;
int padh = 4 - ((bi.biHeight * 3) % 4);
if(padh == 4) padh = 0;
int pad2 = 0;
if(padh == 1 || padh == 3) pad2 = 2;
bi.biHeight += padh;
int w = bi.biWidth;
int h = bi.biHeight;
if(angle == 90 || angle == 270)
{
std::swap(bi.biWidth, bi.biHeight);
}
else
{
bi.biHeight -= padh;
}
for(int row = 0; row < h; row++)
{
for(int col = 0; col < w; col++)
{
int n1 = 3 * (col + w * row) + padw * row;
int n2 = 0;
switch(angle)
{
case 0: n2 = 3 * (col + w * row) + padw * row; break;
case 90: n2 = 3 * ((h - row - 1) + h * col) + pad2 * col; break;
case 180: n2 = 3 * (col + w * (h - row - 1)) + padw * (h - row - 1); break;
case 270: n2 = 3 * (row + h * col) + pad2 * col; break;
}
dst[n2 + 0] = src[n1 + 0];
dst[n2 + 1] = src[n1 + 1];
dst[n2 + 2] = src[n1 + 2];
}
}
for(int row = 0; row < bi.biHeight; row++)
for(int col = 0; col < padw; col++)
dst[bi.biWidth * 3 + col] = 0;
bi.biSizeImage = (bi.biWidth + padw) * bi.biHeight * 3;
return true;
}
int main()
{
std::string input = "input.bmp";
std::string output = "output.bmp";
BITMAPFILEHEADER bf = { 0 };
BITMAPINFOHEADER bi = { sizeof(BITMAPINFOHEADER) };
std::ifstream fin(input, std::ios::binary);
if(!fin) return 0;
fin.read((char*)&bf, sizeof(bf));
fin.read((char*)&bi, sizeof(bi));
int size = 3 * (bi.biWidth + 3) * (bi.biHeight + 3);
char *src = new char[size];
char *dst = new char[size];
fin.read(src, bi.biSizeImage);
//use 0, 90, 180, or 270 for the angle
if(rotate(src, dst, bi, 270))
{
bf.bfSize = 54 + bi.biSizeImage;
std::ofstream fout(output, std::ios::binary);
fout.write((char*)&bf, 14);
fout.write((char*)&bi, 40);
fout.write((char*)dst, bi.biSizeImage);
}
delete[]src;
delete[]dst;
return 0;
}
The BMP file format is a complicated, convoluted beast and there's no such thing as a "simple" BMP file reader. The code you have there makes certain hard coded assumptions on the files you're trying to read (24bpp true color, tightly packed, no compression) that it will flat (on its face) when it encounters anything that isn't that specific format. Unfortunately, for you, the majority of BMP files out there is not of that kind. To give you an idea of what a fully conforming BMP reader must support have a look at this page:
http://entropymine.com/jason/bmpsuite/bmpsuite/html/bmpsuite.html
And the code you have up there does not even check if there's a valid file magic bytes signature and if the header is valid. So that's your problem right there: You don't have a BMP file reader. You have something that actually spits out pixels if you're lucky enough the feed it something that by chance happens to be in the right format.

How can I flip a buffer vertically during an YUV to RGB conversion

EDIT : The question was not clear sorry, I updated and added details.
I have a buffer with image data (YUV format) that I convert to an RGB format. The problem is, I would like to flip the image vertically (Invert the Y-position).
What I'm able to do for the moment is convert my YUV data to RGB data in a buffer, then flip this buffer vertically.
Here is the working code for this :
unsigned char* DeckLinkCaptureDelegate::convertYUVtoRGB(void* frameBytes)
{
unsigned char *mycopy = new unsigned char[height*width*3];
unsigned char *flippedCopy = new unsigned char[height*width*3];
unsigned char* pData = (unsigned char *) frameBytes;
//Conversion from YUV to RGB
for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
{
unsigned char v = pData[j];
unsigned char y = pData[j+1];
unsigned char u = pData[j+2];
mycopy[i+2] = 1.0*y + 8 + 1.402*(v-128); // r
mycopy[i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
mycopy[i] = 1.0*y + 1.772*(u-128) + 0; // b
y = pData[j+3];
mycopy[i+5] = 1.0*y + 8 + 1.402*(v-128); // r
mycopy[i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
mycopy[i+3] = 1.0*y + 1.772*(u-128) + 0;
}
//Vertical flip
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
for (int k = 0; k < 3; ++k) {
flippedCopy[(i + j * width) * 3 + k] = mycopy[(i + (height - 1 - j) * width) * 3 + k];
}
}
}
return flippedCopy;
}
What I would like to do to gain performance is flip the buffer DURING the conversion from YUV to RGB. I had no idea how to do it and Yusuf answer helped me, so here is what I have for the moment :
unsigned char* DeckLinkCaptureDelegate::convertYUVtoRGB(void* frameBytes)
{
unsigned char *mycopy = new unsigned char[height*width*3];
unsigned char* pData = (unsigned char *) frameBytes;
int k = height - 1;
for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
{
unsigned char v = pData[j];
unsigned char y = pData[j+1];
unsigned char u = pData[j+2];
mycopy[(width*k*3) + i+2] = 1.0*y + 8 + 1.402*(v-128); // r
mycopy[(width*k*3) + i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
mycopy[(width*k*3) + i] = 1.0*y + 1.772*(u-128) + 0; // b
y = pData[j+3];
mycopy[(width*k*3) + i+5] = 1.0*y + 8 + 1.402*(v-128); // r
mycopy[(width*k*3) + i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
mycopy[(width*k*3) + i+3] = 1.0*y + 1.772*(u-128) + 0;
if (<i multiple of (width*3)-1>){
k = k - 2;
}
}
return mycopy;
}
If I'm correct, this should work, assuming the if condition is right. But I don't know how to express this if condition, since i is incremented by 6 each time, so I might "skip" the right moment to decrement k
I hope I'm clear enough. Thanks
I assume that width is even, otherwise "reduce in new line"-if will be complicated, then you must use 2 loops. I didn't tested, but should look like this;
unsigned char* DeckLinkCaptureDelegate::convertYUVtoRGB(void* frameBytes)
{
unsigned char *mycopy = new unsigned char[height*width*3];
unsigned char* pData = (unsigned char *) frameBytes;
unsigned int k = height - 1;
for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
{
unsigned char v = pData[j];
unsigned char y = pData[j+1];
unsigned char u = pData[j+2];
mycopy[(width*k*3) + i+2] = 1.0*y + 8 + 1.402*(v-128); // r
mycopy[(width*k*3) + i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
mycopy[(width*k*3) + i] = 1.0*y + 1.772*(u-128) + 0; // b
y = pData[j+3];
mycopy[(width*k*3) + i+5] = 1.0*y + 8 + 1.402*(v-128); // r
mycopy[(width*k*3) + i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
mycopy[(width*k*3) + i+3] = 1.0*y + 1.772*(u-128) + 0;
if (mod(i, width*3) == 0) //reduce in new line (i am not sure how to reduce it, you should think about here)
k = k - 2;
}
return mycopy;
}
And tag your question as imageprocessing etc not only as c++

Converting a data buffer (YUV) to QImage (RGBA)

I'm trying to convert an image buffer with YUV data to a QImage (RGBA8888 format).
Here is the conversion from the buffer to an IplImage :
void DeckLinkCaptureDelegate::convertFrameToOpenCV(void* frameBytes, IplImage * m_RGB){
if(!m_RGB) m_RGB = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
unsigned char* pData = (unsigned char *) frameBytes;
for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
{
unsigned char u = pData[j];
unsigned char y = pData[j+1];
unsigned char v = pData[j+2];
//fprintf(stderr, "%d\n", v);
m_RGB->imageData[i+2] = 1.0*y + 8 + 1.402*(v-128); // r
m_RGB->imageData[i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
m_RGB->imageData[i] = 1.0*y + 1.772*(u-128) + 0; // b
y = pData[j+3];
m_RGB->imageData[i+5] = 1.0*y + 8 + 1.402*(v-128); // r
m_RGB->imageData[i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
m_RGB->imageData[i+3] = 1.0*y + 1.772*(u-128) + 0;
}
}
The goal would be to replace IplImage * m_RGB to a QImage object. The format of the QImage can be change later with a Qt function to convert QImage format. I've managed to get a QImage from the IplImage but I'm trying to bypass this step to completely remove the IplImage step.
The problem is, I'm having a hard time understanding the code above so I don't know how I could do it for a QImage.
Could you help me do it or at least understand the code above ?
Thanks.
EDIT : Here is what I have for the moment but it's not working.
void DeckLinkCaptureDelegate::convertFrameToOpenCV(void* frameBytes, QImage m_RGB){
//if(!m_RGB) m_RGB = QImage(width, height, QImage::Format_RGB888);
unsigned char* pData = (unsigned char *) frameBytes;
for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
{
unsigned char u = pData[j];
unsigned char y = pData[j+1];
unsigned char v = pData[j+2];
//fprintf(stderr, "%d\n", v);
m_RGB.bits()[i+2] = 1.0*y + 8 + 1.402*(v-128); // r
m_RGB.bits()[i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
m_RGB.bits()[i] = 1.0*y + 1.772*(u-128) + 0; // b
y = pData[j+3];
m_RGB.bits()[i+5] = 1.0*y + 8 + 1.402*(v-128); // r
m_RGB.bits()[i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
m_RGB.bits()[i+3] = 1.0*y + 1.772*(u-128) + 0;
}
}
Create another unsigned char array(say pDataRGB), convert yuv(pData) into rgb(pDataRGB), and then create QImage like-
QImage *m_RGB = new QImage(pDataRGB, width, height, QImage::Format_RGB888);
If you want to use only Qimage then setPixel data as following:-
m_RGB->setPixel(i, j, qRgb(r, g, b));