Convert Yuv420 to rgb and show on qt pixmap - c++

I want to convert a yuv picture into RGB to display on a QT pixmap. What I have right now is that I see a empty pixmap.
int convertYUVtoRGB(int y, int u, int v) {
int r,g,b;
r = y + (int)(1.402f*v);
g = y - (int)(0.344f*u +0.714f*v);
b = y + (int)(1.772f*u);
r = r>255? 255 : r<0 ? 0 : r;
g = g>255? 255 : g<0 ? 0 : g;
b = b>255? 255 : b<0 ? 0 : b;
return 0xff000000 | (b<<16) | (g<<8) | r;
}
unsigned char * convertYUV420_NV21toRGB8888(unsigned char data[78080], int width, int height) {
int size = width*height;
int offset = size;
unsigned char * pixels = new unsigned char[size];
int u, v, y1, y2, y3, y4;
// i percorre os Y and the final pixels
// k percorre os pixles U e V
for(int i=0, k=0; i < size; i+=2, k+=2) {
y1 = data[i ]&0xff;
y2 = data[i+1]&0xff;
y3 = data[width+i ]&0xff;
y4 = data[width+i+1]&0xff;
u = data[offset+k ]&0xff;
v = data[offset+k+1]&0xff;
u = u-128;
v = v-128;
pixels[i ] = convertYUVtoRGB(y1, u, v);
pixels[i+1] = convertYUVtoRGB(y2, u, v);
pixels[width+i ] = convertYUVtoRGB(y3, u, v);
pixels[width+i+1] = convertYUVtoRGB(y4, u, v);
if (i!=0 && (i+2)%width==0)
i+=width;
}
return pixels;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
unsigned char * buffer;
unsigned char * image = NULL;
QPixmap pixmap;
QImage img(320, 122, QImage::Format_ARGB32_Premultiplied);
img.fill(QColor(Qt::white).rgb());
ifstream is;
is.open ("bunny.yuv", ios::binary );
// is.seekg (0, ios::end);
// length = is.tellg();
// is.seekg (0, ios::beg);
buffer = new unsigned char[78081];
is.read((char * )buffer,78080);
is.close();
/*
for (int x = 0; x < 10; ++x) {
for (int y = 0; y < 10; ++y) {
img.setPixel(x, y, qRgb(0, 0, 0));
}
}
*/
for(int i = 0; i < 78080; i++)
qDebug() << buffer[i]<< endl;
image = convertYUV420_NV21toRGB8888(buffer,320,122);
QByteArray byteImage((const char *)image);
pixmap.loadFromData(byteImage);
QLabel myLabel;
myLabel.setPixmap(pixmap);
myLabel.setGeometry(20,100,320,122);
myLabel.show();
return a.exec();
}
EDIT: I've added the right format and parenthesses. Still the same problem.
Kind regards,

And when you have done the calculation in Jason's post - the best format for the pixmap is QImage::Format_ARGB32_Premultiplied - the format is Blue byte Green byte Red byte 0xff

This won't do what you think it does:
r = y + (int)1.402f*v;
g = y - (int)(0.344f*u +0.714f*v);
b = y + (int)1.772f*u;
As you've missed parentheses and so cast your float constants to ints before the calculation.
And then having converted your YUV data into RGB packed into an int, you store that int into a char...
So:
unsigned char * pixels = new unsigned char[size];
Should probably be:
unsigned int* pixels = new unsigned int[size];
And obviously that affects the return type of the function too. You might also want to change the type of your YUV -> RGB function to unsigned as well, just for consistency.

In convertYUV420_NV21toRGB8888, pixels is an unsigned char array but you're trying to assign a int to it. All you will get is the low byte, and most of the bytes in your output will remain unset. You need to completely rethink the way you're writing the output.

Related

how do I fill the gaps between pixel in bmp file after loading the pixel array into the array with padded rows

I wrote a program in c++ to draw the pixel of bmp image into the console using SetPixel windows function, but after loading the pixel array into the array the image got printed on the console with gaps between the pixels. Thanks in advance for your help!
This is the output of the printed image on the console.
This is the original Image I provided to it.
As you can see here the image width also changes after the print on the console.
// bmp bitmap
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
using namespace std;
#pragma pack(1)
struct BitmapFileHeader {
unsigned short type;
unsigned int size;
unsigned short reserved1;
unsigned short reserved2;
unsigned int offset;
};
#pragma pack(0)
unsigned char grayScale(unsigned char r, unsigned char g, unsigned char b) {
return ((r + g + b) / 3);
}
int main() {
char *data;
FILE *filePointer;
int **ImageArray;
BitmapFileHeader *bmp = (struct BitmapFileHeader*)malloc(sizeof(struct BitmapFileHeader));
BITMAPINFOHEADER *BitmapInfoHeader = (BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER));
HWND console = GetConsoleWindow();
HDC context = ::GetDC(console) ;
filePointer = fopen("tom.bmp", "rb");
if(!filePointer) {
perror("");
}
fread(reinterpret_cast<BitmapFileHeader*>(bmp), sizeof(BitmapFileHeader), 1, filePointer);
fread(reinterpret_cast<BITMAPINFOHEADER*>(BitmapInfoHeader), sizeof(BITMAPINFOHEADER), 1, filePointer);
if(BitmapInfoHeader->biSize == 40 && BitmapInfoHeader->biCompression == BI_BITFIELDS) {
printf("This types of image uses Extra bit masks\n");
}
// row pading
int RowSize = ((BitmapInfoHeader->biBitCount * BitmapInfoHeader->biWidth + 31) / 32) * 4;
int PixelArraySize = RowSize * BitmapInfoHeader->biHeight;
int height = BitmapInfoHeader->biHeight * 5;
int width = BitmapInfoHeader->biWidth * 5;
printf("RowSize: %d PixelArraySize: %d\n", RowSize, PixelArraySize);
ImageArray = (int**)malloc(sizeof(int*)*height);
// memory allocation
for(int i = 0; i < height; i++)
ImageArray[i] = (int*)malloc(sizeof(int)*width);
data = (char*)malloc(PixelArraySize);
fseek(filePointer, bmp->offset, SEEK_SET);
// set image into array
for(int ii = 0; ii < height; ii+=3) {
fread(data, RowSize, 3, filePointer);
for(int jj = 0; jj < width; jj+=3) {
ImageArray[ii][jj] = grayScale(data[jj+2], data[jj+1], data[jj]);
SetPixel(context, -jj+1000, -ii+500, RGB(data[jj+2], data[jj+1], data[jj]));
}
}
fclose(filePointer);
return 0;
}
here is the code, which I wrote.
A pixel is described by three bytes, one for each RGB channel. You are dealing with two indices here: The index of the pixel in the row data and the position of the pixel in width direction. You place the pixel and access the row data with the same index.
So:
for (int jj = 0; jj < width; jj++) { // jj: position
int kk = 3 * jj; // kk: data index
ImageArray[ii][jj] = grayScale(data[kk + 2], data[kk + 1], data[kk]);
SetPixel(context, -jj + 1000, -ii + 500, RGB(data[kk + 2], data[kk + 1], data[kk]));
}
The vertical gaps, i.e. the blank lines, come from incrementing by 3, where you should just increment by 1. (You have no "data index" here, because you read your data row-wide for the current row ii.)
If you want to enlarge your image, as the multiplication of width and height by 5 suggests, you must add a third index: You now have two positions, the source and target positions. This will be easier if you separate your loops: Create ImageArray of the source image in a first nested loop, then draw your scaled target image to the console with a loop over the target oordinates:
int scale = 5;
int ww = scale * w;
int hh = scale * h;
// read ImageArray
for (int y = 0; y < h; y++) {
fread(data, RowSize, 3, filePointer);
for (int x = 0; x < w; x++) {
ImageArray[y][x] = ...;
SetPixel(context, -jj+1000, -ii+500, RGB(data[jj+2], data[jj+1], data[jj]));
}
}
for (int yy = 0; yy < hh; yy++) {
fread(data, RowSize, 3, filePointer);
for (int xx = 0; xx < ww; xx++) {
int x = xx / scale;
int y = yy / scale;
SetPixel(context, yy, xx, ImageArray[y][x]);
}
}
(Here, single letters re source values, double leters are target values.)

How to correct horizontal flip function to prevent it from altering images' colors

stb_image.h provides a method to flip an image vertically and it works fine. I tried to implement an horizontal flip aka mirror but it alters the image colors.
On pictures that only have 3 colors you could get bluish or reddish or even magenta colored pictures instead of their actual colors. It's the same if we're talking about JPEG or PNG images, you get the same strange results. Curiously if you flip that very same image vertically, its colors look normal.
I've tried testing pretty much any function you could find here and the code I'm providing you with has been the only one that got me close to my actual goal.
// Function I've been trying to implement to enable Horizontal Flip
static void stbi_horizontal_flip(void *image, int w, int h, int bytes_per_pixel)
{
size_t line_bytes = (size_t)w * bytes_per_pixel;
stbi_uc temp[line_bytes];
stbi_uc *bytes = (stbi_uc *)image;
Debug() << line_bytes;
for (int col = 0; col < h; col++) {
stbi_uc *line = bytes + col * line_bytes;
memcpy(&temp, line, line_bytes);
for (int row = 0; row < line_bytes; row++) {
line[row] = temp[line_bytes - row - bytes_per_pixel];
}
}
stbi_horizontally_flip_on_load = false;
}
// stb_image's function for Vertical Flip
static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
{
int row;
size_t bytes_per_row = (size_t)w * bytes_per_pixel;
stbi_uc temp[2048];
stbi_uc *bytes = (stbi_uc *)image;
for (row = 0; row < (h>>1); row++) {
stbi_uc *row0 = bytes + row * bytes_per_row;
stbi_uc *row1 = bytes + (h - row - 1) * bytes_per_row;
size_t bytes_left = bytes_per_row;
while (bytes_left) {
size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp);
memcpy(temp, row0, bytes_copy);
memcpy(row0, row1, bytes_copy);
memcpy(row1, temp, bytes_copy);
row0 += bytes_copy;
row1 += bytes_copy;
bytes_left -= bytes_copy;
}
}
}
static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{
stbi__result_info ri;
void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);
if (result == NULL) return NULL;
if (ri.bits_per_channel != 8) {
STBI_ASSERT(ri.bits_per_channel == 16);
result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
ri.bits_per_channel = 8;
}
// #TODO: move stbi__convert_format to here
if (stbi_horizontally_flip_on_load) {
int channels = req_comp ? req_comp : *comp;
stbi_horizontal_flip(result, *x, *y, channels * sizeof(stbi_uc));
}
if (stbi__vertically_flip_on_load) {
int channels = req_comp ? req_comp : *comp;
stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc));
}
return (unsigned char *) result;
}
STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
{
unsigned char *result;
stbi__context s;
stbi__start_file(&s,f);
result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
if (result) {
// need to 'unget' all the characters in the IO buffer
fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
}
return result;
}
STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
{
FILE *f = stbi__fopen(filename, "rb");
unsigned char *result;
if (!f) return stbi__errpuc("can't fopen", "Unable to open file");
result = stbi_load_from_file(f,x,y,comp,req_comp);
fclose(f);
return result;
}
STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
{
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
}
void Gosu::load_image_file(Gosu::Bitmap& bitmap, const string& filename)
{
Buffer buffer;
load_file(buffer, filename);
load_image_file(bitmap, buffer.front_reader());
}
void Gosu::load_image_file(Gosu::Bitmap& bitmap, Reader input)
{
bool needs_color_key = is_bmp(input);
stbi_io_callbacks callbacks;
callbacks.read = read_callback;
callbacks.skip = skip_callback;
callbacks.eof = eof_callback;
int x, y, n;
stbi_uc* bytes = stbi_load_from_callbacks(&callbacks, &input, &x, &y, &n, STBI_rgb_alpha);
if (bytes == nullptr) {
throw runtime_error("Cannot load image: " + string(stbi_failure_reason()));
}
bitmap.resize(x, y);
printf("Channels %d, Gosu Color size %d, unsigned char size %d, bytes array size %d",
n, sizeof(Gosu::Color), sizeof(stbi_uc), sizeof(bytes));
// Output: Channels 3 or 4, Gosu Color size 4, unsigned char size 1, bytes array 8
memcpy(bitmap.data(), bytes, x * y * sizeof(Gosu::Color));
stbi_image_free(bytes);
if (needs_color_key) apply_color_key(bitmap, Gosu::Color::FUCHSIA);
}```
// Output: Channels 3 or 4, Gosu Color size 4, unsigned char size 1, bytes array 8
That is what I got back from stb_image, but I'd prefer to get an 8bit array instead. Even so what actually matters is to get rid of that unexpected color change.
Thanks to Igor's comment I could focus on my immediate problem and not long after I came up with the code I've posted below.
What I've been wondering since I finally could flip the images horizontally was why the other methods I found either on the web or as part of image processors' code didn't work as expected. O_o? Sometimes I copied and pasted them only changing some variables' names or types to match stb_image's and they still failed either to compile or display a decent result.
By the way, I tried before to subtract positions to the right value to no avail but it made me think some of them could be used as nice color blend effects. XD
// Horizontal Flip by Kyonides Arkanthes shared under GPLv2 or v3
static void stbi_kyon_horizontal_flip(void *image, int w, int h, int bytes_per_pixel)
{
size_t line_bytes = (size_t)w * bytes_per_pixel;
stbi_uc temp[line_bytes];
stbi_uc *bytes = (stbi_uc *)image;
int lpos, rpos;
for (int col = 0; col < h; col++) {
stbi_uc *line = bytes + col * line_bytes;
memcpy(&temp, line, line_bytes);
for (int row = 0; row < w; row++) {
lpos = row * bytes_per_pixel;
rpos = line_bytes - row * bytes_per_pixel - 1;
line[lpos] = temp[rpos - 3];
line[lpos + 1] = temp[rpos - 2];
line[lpos + 2] = temp[rpos - 1];
line[lpos + 3] = temp[rpos];
}
}
stbi_kyon_horizontally_flip_on_load = false;
}```
You just reversed the order of RGBA, you try to use this, I tested, the effect is normal.
for (int row = 0; row < Qimg2.width(); row++) {
lpos = row * bytes_per_pixel;
rpos = line_bytes - row * bytes_per_pixel - 1;
line[lpos] = temp[rpos - 2];
line[lpos + 1] = temp[rpos - 1];
line[lpos + 2] = temp[rpos - 3];
line[lpos + 3] = temp[rpos];
}

Finding a small bmp file in a bigger bmp file

I want to find a small bmp file from another bigger bmp file (the bigger one is captured from screen and called Sample.bmp , the small bmp file is called Button.bmp . Thing is the when comparing the images the file can't be found anywhere.
the compare code :
for (int i=0;i<SCREEN_WIDTH-width;++i)
{
for (int j=0;j<SCREEN_HEIGHT-height;++j)
{
boolean isequal = true;
for(int qqq=i;qqq<i+width;++qqq)
{
for (int kkk=j;kkk<j+height;++kkk)
{
if (PI[qqq][kkk]!=NPI[qqq-i][kkk-j]) isequal = false;
if (isequal == false)
{
qqq = i + width + 1;
kkk = j + height + 1;
}
}
}
if (isequal==true)
{
MidX = i;
MidY = j;
return;
}
}
}
note : Screen_width and Screen_height are for the bigger image and width and height are for the smaller one
Full Code:
void readBMP()
{
int i;
FILE* f = fopen("Sample.bmp", "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height;
unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
for(int qq=0;qq<SCREEN_WIDTH;++qq)
for (int kk=0;kk<SCREEN_HEIGHT;++kk)
{
PI[qq][kk][0] = data[kk * width + qq];
PI[qq][kk][1] = data[kk * width + qq + 1];
PI[qq][kk][2] = data[kk * width + qq + 2];
}
}
void FindImageInScreen(char* FileName)
{
FILE* f = fopen(FileName, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height;
unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
for(int qq=0;qq<width;++qq)
for (int kk=0;kk<height;++kk)
{
NPI[qq][kk][0] = data[kk * width + qq];
NPI[qq][kk][1] = data[kk * width + qq + 1];
NPI[qq][kk][2] = data[kk * width + qq + 2];
}
for (int i=0;i<SCREEN_WIDTH-width;++i)
{
for (int j=0;j<SCREEN_HEIGHT-height;++j)
{
boolean isequal = true;
for(int qqq=i;qqq<i+width;++qqq)
{
for (int kkk=j;kkk<j+height;++kkk)
{
if (PI[qqq][kkk][0]!=NPI[qqq-i][kkk-j][0]) isequal = false;
if (isequal == false)
{
qqq = i + width + 1;
kkk = j + height + 1;
}
}
}
if (isequal==true)
{
MidX = i;
MidY = j;
return;
}
}
}
MidX = -1;
MidY = -1;
return;
}
definition of arrays (added because of request) , This is before functions execute :
PI = new unsigned int**[SCREEN_WIDTH];
for (int i=0;i<SCREEN_WIDTH;++i)
PI[i] = new unsigned int*[SCREEN_HEIGHT];
for (int i=0;i<SCREEN_WIDTH;++i)
for (int j=0;j<SCREEN_HEIGHT;++j)
PI[i][j] = new unsigned int[3];
NPI = new unsigned int**[SCREEN_WIDTH];
for (int i=0;i<SCREEN_WIDTH;++i)
NPI[i] = new unsigned int*[SCREEN_HEIGHT];
for (int i=0;i<SCREEN_WIDTH;++i)
for (int j=0;j<SCREEN_HEIGHT;++j)
NPI[i][j] = new unsigned int[3];
The First function executes then the second. and sorry for some bad programming because I did thousands of changes to make it work!
PI[qq][kk][0] = data[kk * width + qq];
From how PI and NPI are filled in, it appears that they are 3-dimensional arrays (it would help if you included their definition in the code sample). But
if (PI[qqq][kkk]!=NPI[qqq-i][kkk-j]) isequal = false;
which is only indexing 2 dimensions of each. PI[a][b] is the address of the array containing PI[a][b][0..2] and will certainly never match the address of NPI[x][y], so this statement is always returning false I expect.
Lets get you started. Here is a better LoadBMP.
Yours, among other thing, read the size, and uses SCREEN_HEIGHT anyway.
Using this for loading both images is probably easier.
#include <vector>
#include <cstdio>
#include <string>
using namespace std;
typedef unsigned char UC;
struct RGB { UC r,g,b; };
bool operator == ( const RGB& p1, const RGB& p2 ) { return p1.r==p2.r && p1.g==p2.g && p1.b==p2.b; }
struct BMP
{
int width;
int height;
vector<RGB> pixels;
RGB& Pix(int x,int y) { return pixels[ y*width + x ]; }
};
void LoadBMP( BMP& bmp, const char* filename )
{
FILE* f = fopen(filename, "rb");
UC info[54];
fread(info, 1, 54, f); // read the 54-byte header
// extract image height and width from header
bmp.width = *(int*) (info+18);
bmp.height = *(int*) (info+22);
// scanlines are always multiple of 4, padded with 0-3 bytes
int scanlinesize = 3*bmp.width;
while( scanlinesize % 4 ) ++scanlinesize;
int size = scanlinesize * bmp.height;
UC* data = new UC[size];
fread(data, 1, size, f);
fclose(f);
bmp.pixels.clear();
bmp.pixels.reserve(bmp.height*bmp.width);
for(int yy=0;yy<bmp.height;++yy)
{
UC* p = data+scanlinesize*yy;
for (int xx=0;xx<bmp.width;++xx)
{
RGB rgb;
rgb.b = *p++;
rgb.g = *p++;
rgb.r = *p++;
bmp.pixels.push_back(rgb);
}
}
delete[] data;
}

C++ syntax confusion - declaring unsigned char arrays

I have a function that takes two Image inputs (image and image2), mixing the color values pixel-by-pixel. Factor represents the percentage of the color value from image, so it follows that the remaining value (1-factor) is from image2. I have the pixel buffer represented by unsigned characters, and I'm having difficulty figuring out what syntax I need in order to access and set my values. I've tried a number of things, but right now it gives me the following error:
filters.C:91:41: error: scalar object ā€˜pā€™ requires one element in initializer
unsigned char *p = {red, green, blue};
The function:
void Filter::Execute()
{
if (input->GetWidth() == input2->GetWidth() && input->GetHeight() == input2->GetHeight())
{
int width = input->GetWidth();
int height = input->GetHeight();
output.ResetSize(width, height);
for (int w = 0; w < width; w++)
{
for (int h = 0; h < height; h++)
{
unsigned char red = input->GetPixel(w, h)[0]*factor+input2->GetPixel(w, h)[0]*(1-factor);
unsigned char green = input->GetPixel(w, h)[1]*factor+input2->GetPixel(w, h)[1]*(1-factor);
unsigned char blue = input->GetPixel(w, h)[2]*factor+input2->GetPixel(w, h)[2]*(1-factor);
unsigned char *p = {red, green, blue};
output.SetPixel(w, h, p);
}
}
}
}
And this is how I have my image class set up:
#include <image.h>
#include <stdlib.h>
Image::Image()
{
width = 0;
height = 0;
buffer = NULL;
}
Image::~Image()
{
if (buffer != NULL)
{
delete[] buffer;
}
}
void Image::ResetSize(int w, int h)
{
width = w;
height = h;
if (buffer != NULL)
{
delete[] buffer;
}
else
{
buffer = new unsigned char[3*width*height];
}
}
unsigned char * Image::GetPixel(int w, int h)
{
int index = h*width + w;
return buffer+3*index;
}
void Image::SetPixel(int w, int h, unsigned char *p)
{
int index = h*width + w;
buffer[3*index+0] = p[0];
buffer[3*index+1] = p[1];
buffer[3*index+2] = p[2];
}
What am I overlooking?
unsigned char * is not an array, it's a pointer. You want to declare it
unsigned char p[] = {red, green, blue};

data corruption while converting from char * to stringstream and then back to char*

So, i was trying my hands on XImage, created my first screen capture program and it worked fine. Now i wanted to send this XImage to another machine via thrift and save it there, but as there was some data reading problem from the client side, therefore i shifted the entire server side code to client side(for testing purpose. I will eventually move it back to server side)
Code :
transport->open();
Display *display = XOpenDisplay(NULL);
Window root = DefaultRootWindow(display);
XWindowAttributes gwa;
XGetWindowAttributes(display, root, &gwa);
int width = gwa.width;
int height = gwa.height;
XImage *image = XGetImage(display,root, 0,0 , width,height,AllPlanes, ZPixmap);
char *data = image->data;
int imgwidth = image->width;
int imgheight = image->height;
int depth = image->depth;
int offset = 0;
int xpad = 32; //may be 8 or 16 or 32
int format = ZPixmap;
int bytes_per_line = image->bytes_per_line;
sendImage image_send;
ostringstream s;
//s<<image->data;
s.write(image->data,(width)*(height));
image_send.width = imgwidth;
image_send.height = imgheight;
image_send.depth = depth;
image_send.offset = 2000;
image_send.data = s.str();
image_send.xpad = xpad;
image_send.format = format;
image_send.bytes_per_line = bytes_per_line;
//server side code
int width1 = image_send.width;
int height1 = image_send.height;
int depth1 = image_send.depth;
int offset1 = image_send.offset;
char* tmp = new char[image_send.data.length() + 1];
strcpy(tmp,image_send.data.c_str());
char *data1 = tmp;
int xpad1 = image_send.xpad;
int format1 = image_send.format;
int bytes_per_line1 = image_send.bytes_per_line;
//Initialising X Operations
//Display *display = XOpenDisplay(NULL);
//Window root = DefaultRootWindow(display);
//XWindowAttributes gwa;
//XGetWindowAttributes(display, root, &gwa);
int serverwidth = gwa.width;
int serverheight = gwa.height;
XImage *image1 = XCreateImage(display,DefaultVisual(display,DefaultScreen(display)),depth1, format1,offset1,data1,width1,height1,xpad1, bytes_per_line1);
unsigned char *array = new unsigned char[width * height * 3];
unsigned long red_mask = image1->red_mask;
unsigned long green_mask = image1->green_mask;
unsigned long blue_mask = image1->blue_mask;
CImg<unsigned char> pic(array,width,height,1,3);
for (int x = 0; x < width1; x++)
for (int y = 0; y < height1 ; y++)
{
unsigned long pixel = XGetPixel(image1,x,y);
unsigned char blue = pixel & blue_mask;
unsigned char green = (pixel & green_mask) >> 8;
unsigned char red = (pixel & red_mask) >> 16;
array[(x + width1 * y) * 3] = red;
array[(x + width1* y) * 3+1] = green;
array[(x + width1 * y) * 3+2] = blue;
pic(x,y,0,0) = red;
pic(x,y,0,1) = green;
pic(x,y,0,2) = blue;
}
pic.save_png("blah1.png");
As you can see i took the data part from the image pointer, wrote it into stream and then assigned that value to thrift object(image_send)
For the server side. i stored it into data1 using c_str() and then used XCreateImage using data1
This is the output that i get if i pass data1 as the source in XCreateImage
and this is the output i get if i use data(client side variable from original XImage)
As we can see there is a lot of blank space in the first image. On further inspection, i found that data1 contains a lot of null in the start where as data don't
So, i believe data gets corrupted while conversion from char * to string stream and then back to char *.
What suggestion would you give to correct this?
Update :
So, i was trying various things and it finally worked. The issue turns out to be buffer allocation and use of strcpy (i guess) which i changed to memcpy
int main() {
Display *display = XOpenDisplay(NULL);
Window root = DefaultRootWindow(display);
XWindowAttributes gwa;
XGetWindowAttributes(display, root, &gwa);
int width = gwa.width;
int height = gwa.height;
XImage *image = XGetImage(display,root, 0,0 , width,height,AllPlanes, ZPixmap);
char *data = image->data;
int imgwidth = image->width;
int imgheight = image->height;
int depth = image->depth;
int offset = 0;
int xpad = 32; //may be 8 or 16 or 32
int format = ZPixmap;
int bytes_per_line = image->bytes_per_line;
//sendImage image_send;
ostringstream s;
//s<<image->data;
s.write(data,(width)*(height) * 4);
string stemp = s.str();
//server side code
int width1 = width;
int height1 = height;
int depth1 = depth;
int offset1 = offset;
char* tmp = new char[stemp.length()];
//strcpy(tmp,stemp.c_str());
memcpy(tmp,stemp.data(),stemp.length());
char *data1 = tmp;
int xpad1 = 32;
int format1 = ZPixmap;
int bytes_per_line1 = bytes_per_line;
int serverwidth = gwa.width;
int serverheight = gwa.height;
XImage *image1 = XCreateImage(display,DefaultVisual(display,DefaultScreen(display)),depth1, format1,offset1,data1,serverwidth,serverheight,xpad1, bytes_per_line1);
unsigned char *array = new unsigned char[width * height];
unsigned long red_mask = image1->red_mask;
unsigned long green_mask = image1->green_mask;
unsigned long blue_mask = image1->blue_mask;
CImg<unsigned char> pic(array,width,height,1,3);
for (int x = 0; x < width1; x++) {
for (int y = 0; y < height1 ; y++)
{
unsigned long pixel = XGetPixel(image1,x,y);
unsigned char blue = pixel & blue_mask;
unsigned char green = (pixel & green_mask) >> 8;
unsigned char red = (pixel & red_mask) >> 16;
pic(x,y,0,0) = red;
pic(x,y,0,1) = green;
pic(x,y,0,2) = blue;
}
}
pic.save_png("blah.png");
return 0;
}
However there are couple of things that i don't understand
1) when i am writing the data to stream why do i have to make the size of buffer as (width * height * 4). Anything less will crop the image proportionally. If i understand correctly, shouldn't be buffer size just equal to width*height of the screen or is it structured more like array of Red,green,blue and depth?
2) My buffer size is about 4MB, however the image size is about 150K, is it due to encoding
3)Will Allocating and deallocating this much buffer size continuously, hamper performance?
Edit : For future purpose
The answer of first question is: we can get the same result using (bytes_per_lineheight) which co-incidentally gave the same result as widthheight*4
Image data is binary, you cannot manipulate it as if it were a C string. The first zero byte will ruin the party.
Use std::string::data() and/or memcpy, not std::string::str() or strcpy.
I don't know if it's the only problem. (width)*(height) looks suspicious to me. Do you have one-byte pixels?