Relation between lineSize, width, height in FFMPEG - c++11

I am confused between linesize, height, width in AVFrame.
As per my understanding, linesize is the strides, which ideally should be the width of the image, right ?
However, the value of width and linesize are not matching.
AVFrame pFrame;
cout<<"PFrame Linesize :"<<pFrame->data.linesize[0]<<endl;
cout<<"PFrame Width :"<<pFrame->width<<endl;
Output :
PFrame Linesize : 64
PFrame width : 12
My frame is of dimension 12*12.
According to answers to this post, linesize should be same as width. But I am unable to understand why they are different here.

I'm citing the docs here:
* For video, size in bytes of each picture line.
* For audio, size in bytes of each plane.
*
* For audio, only linesize[0] may be set. For planar audio, each channel
* plane must be the same size.
*
* For video the linesizes should be multiples of the CPUs alignment
* preference, this is 16 or 32 for modern desktop CPUs.
* Some code requires such alignment other code can be slower without
* correct alignment, for yet other it makes no difference.
*
* #note The linesize may be larger than the size of usable data -- there
* may be extra padding present for performance reasons.
So linesize for video is
width * bytes per pixel + line padding
It takes you from the start of one row of image / frame data to the start of the next row.

Related

What is the size of ppvBits returned by CreateDIBSection?

I am trying to capture the screen. I call CreateDIBSection using a BITMAPINFO initialized with the screen dimensions, number of planes = 1, bpp = 32, mode = rgb. It then returns me a pointer to the bitmap data in ppvBits. But how do I determine how many bytes are in this buffer? It doesn't fill out the biSizeImage for me. Should I be computing it from the bpp and dimensions? Something like width * height * (bpp/8)?
The docs for BITMAPINFOHEADER explain how to calculate the "stride" - that is the number of bytes from one row to the next.
For uncompressed RGB formats, the minimum stride is always the image
width in bytes, rounded up to the nearest DWORD. You can use the
following formula to calculate the stride:
stride = ((((biWidth * biBitCount) + 31) & ~31) >> 3)
To calculate the total number of bytes required for a bitmap you multiply the stride by the number of rows.
Note the requirement for bitmap rows to be DWORD-aligned - this is why the stride calculation is rounded up.

turbojpeg's tjDecompressToYUV2 data format

I would like to convert a JPEG image into Y'UV420p using turbo jpeg. I think that this uses some 2x2 blocks with there being twice the information in the Y' that there is in each of the U and V components but I haven't been able to find an example that does this.
How do I extract these individual coponents from tjDecompressToYUV2 and what is the format of the buffer that is allocated for say an image whose dimensions are a multiple of a power of 2?
Say I have an image that is 1024 wide by 512 pixels high. How would I extract each Y' U and V value from the following code:
constexpr auto height = 512u;
constexpr auto width = 1024u;
unsigned char buffer[height * width * 3 / 2];
...
tjDecompressToYUV2(jpegDecompressor, jpegImage, jpegSize, buffer, width, 2, height, TJFLAG_FASTDCT);
...
ie. How are the components extracted from buffer?
Try using the tjDecompressToYUV function which gives you the 3 colorspaces on 3 different output buffers. The Y buffer should contain one byte (eight bit value) for the Y component. I have never used the chroma components so I cannot tell you how they are stored.
What is it that you are trying to do, as there might be another way to solve this problem?

Algorithm for scaling image based on another image size and also preserve its aspect ratio

I have 2 images, Image 1 and Image 2.
Image 1 has a size of 512(width) x 515(height).
Then Image 2 with size of 256(width) x 256(height).
Image 2 is will be used as a watermark and will be placed on top of Image 1.
I want Image 2 size to be dependent on Image 1 size. Image 2 can resize up or down depending on the size of Image 1.
The new size(width & height) of Image 2 should be 20 percent size of Image 1 and the-same time preserve its aspect ratio.
What's the algorithm to find the new size(width & height) of Image 2?
Right now, I use (20%/100) * 512 to resize it, but this does not preserve Image 2 aspect ratio.
If the two images don't have the same aspect ratio then it's mathematically impossible to scale both width and height by 20% and preserve the aspect ratio.
So, chose an axis that you will use to scale by, and scale the other one to the size that preserves the aspect ratio.
e.g, using width:
new_image1_width = 512 * (20 / 100) = 102.4
Then compute the new height to preserve the aspect ratio:
original_aspect_ratio = image2_width / image2_height = 256 / 256 = 1
new_image1_height = 102.4 / original_aspect_ratio = 102.4
Or do it the other way (this time multiplying by the ratio):
new_image1_height = 515 * (20 / 100) = 103
original_aspect_ratio = image2_width / image2_height = 256 / 256 = 1
new_image1_width = 103 * original_aspect_ratio = 103
If you have to handle arbitrary image sizes and arbitrary scale factors, you will need two switch between the two ways depending on what you want the rule to be. E.g. you could always go with the smaller of the two, or use a ratio-adjusted height unless this gives you a height larger than image 1 height, and in that case use the second way, or vice versa.

Choose best image size from a list for given screen dimensions

Given the following possible screen sizes:
720x480
1280x720
1920x1080
3840×2160
And a range of image sizes similar to the following (which may vary to some degree, and the maximum size can be anything up to the limit of available memory):
Square 75 75
Large Square 150 150
Thumbnail 100 75
Small 240 180
Small 320 320 240
Medium 500 375
Medium 640 640 480
Medium 800 800 600
Large 1024 768
Large 1600 1600 1200
Large 2048 2048 1536
Original 3264 2448
And that some images may not be available in "Original" size and may not be larger 1024x768
I need to choose the best image for the current screen dimension.
I'm unsure how to approach this. The language will be Brightscript, but I'm really looking for a selection algorithm, or at least some suggestions on how to write the selection algorithm.
I need to choose the best image for the current screen dimension
It depends on what one means by "the best". One could try to optimize for width, others for height, or trying for the both at the same time (that would be trying to minimize the remaining area ). Let's optimize the remaining area. Here's a pseudo-code:
given screen s0
initialize best_image := None
best_remaining_area := INF
for image in image_list:
if s0.height < image.height or s0.width < image.width:
continue
remaining_area = s0.height * s0.width - image.height * image.width
if remaining_area < best_remaining_area:
best_remaining_area = remaining_area
best_image = image
return best_image

Image processing storage of image

I am unable to understand the following question, could any one explain me what exactly is it asking?
Suppose that an image of dimension 5*6 inches has detail to the
frequency of 600 dots per inch in each direction. How many samples are
required to preserve the information in the image? if the dynamic
range of the pixel values is between 0 and 200, how many megabytes do
we need to store the whole image without compression?
I tried to solve this way, but just first part I did I do not know this is correct or not:
5*6*600 = 18000
I think 18000 are total no of pixels required to preserve the information of image
but do not know this is correct or not but how to find megabytes for storage?
Well, it's 600 dpi in both the horizontal and the vertical.
Next you have to consider the bit depth. Looks like 200 values - I'm going to assume that's per channel. So 3 bytes per pixel.
( 5 * 600 ) * ( 6 * 600 ) = 10,800,000 pixels
10,800,000 * 3 = 32,400,000 bytes
32,400,000 / 1024 = 31,640.625 kilobytes
31,640.625 / 1024 = 30.899047852 megabytes

Resources