I have to render opencv Mat to using DirectX 11.
D2D1_BITMAP_PROPERTIES1 bitmapProperties = D2D1::BitmapProperties1(
D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
96,
96
);
D2D1_SIZE_U s = D2D1::SizeU(640, 480);
hr = m_d2DeviceContext->CreateBitmap(s, bitmapProp, &m_streamBitmap);
I have to copy opencv matrix to m_streamBitmap.
pImage is a color image with 3 channels.
m_streamBitmap->CopyFromMemory(NULL, reinterpret_cast<BYTE*>(pImage.data),pImage.cols()*3);
This gives distorted image as result.
Can anyone guide me with this.
Thanks in advance ..
Your CopyFromMemory call is copying a three byte color bitmap into a four byte color bitmap. CopyFromMemory does not do format conversion, meaning the format of the source and destination bitmaps must match exactly. In addition, DXGI does not support a three byte color format, so the source bitmap must be modified to four bytes per pixel, even if the fourth is not used. If you cannot change the format of the source bitmap to match the Direct2D bitmap, then you will need to write a conversion function that will expand the bitmap elements to four bytes.
I've got a problem converting a pdflatex-generated PDF image to a PNG image using the standalone package.
The pixelated rendering of the text in the converted image (PDF->PS->PNG via gs and ImageMagick?) is awfully blurry and inferior in quality (sharpness, crispness etc.) to the screen-dumped original PDF.
I have checked out these StackExchange posts:
Standalone diagrams with TikZ?
TikZ to non-PDF
and been guided in the setup of my workflow by the standalone package manual. But after considerable experimental adjustment of the various conversion settings in the code below, I have been unable to improve the quality of the outputted PNG image.
A sample of the settings I have played with:
density (increase the dpi)
size (increase/decrease the dimensions)
the TikZ picture width/height dimensions (no optimum found, but if too small the PNG image width does not equal that specified among the documentclass parameters)
using the command={} option, I have also played with options such as -quality and - set colorspace RGB (though I didn't really know what I was doing here)
Another approach I have taken is to try to set the TikZ picture width and height dimensions (in cm) in such a way that they agree with the conversion dimensions given among the documentclass parameters (using a dpi + pixels -> cm converter).
None of this worked! So any help in converting from PDF to PNG using the standalone package that preserves the sharpness and crispness of the rendered text in the image would be hugely appreciated.
For reference the versions of the various systems/applications I'm using are:
Windows 7
MiKTeX 2.9
TeXnicCenter
gs 9.09
ImageMagick 6.8.6 Q16 (32-bit)
standalone package installed using MiKTeX package manager late Aug 2013
\documentclass[preview,convert={density=300,size=900x300,outext=.png}]{standalone}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{every x tick label/.style={at={(1,0)}, yshift=-0.15cm, xshift=-0.0cm, inner sep=0pt, font=\normalsize}}
\begin{tikzpicture}
\begin{axis}[
no markers, domain=-2.1*pi:2.1*pi, samples=1000,
width=30.0cm,
height=10.0cm,
axis x line*=middle,
x axis line style={densely dotted, opacity=0.75},
axis y line*=middle,
y axis line style={densely dotted, opacity=0.75},
ymin=-1.1,
ymax=1.1,
xtick={-6.28318530717959, -5.65486677646163, -5.02654824574367, -4.71238898038469, -4.39822971502571, -3.76991118430775, -3.14159265358979, -2.51327412287183, -1.88495559215388, -1.5707963267949, -1.25663706143592, -0.628318530717959, 0, 0.628318530717959, 1.25663706143592, 1.5707963267949, 1.88495559215388, 2.51327412287183, 3.14159265358979, 3.76991118430775, 4.39822971502571, 4.71238898038469, 5.02654824574367, 5.65486677646163, 6.28318530717959},
xticklabels={$-2\pi$, $-\frac{9\pi}{5}$, $-\frac{8\pi}{5}$, $-\frac{3\pi}{2}$, $-\frac{7\pi}{5}$, $-\frac{6\pi}{5}$, $-\pi$, $-\frac{4\pi}{5}$, $-\frac{3\pi}{5}$, $-\frac{\pi}{2}$, $-\frac{2\pi}{5}$, $-\frac{\pi}{5}$, $0$, $\frac{\pi}{5}$, $\frac{2\pi}{5}$, $\frac{\pi}{2}$, $\frac{3\pi}{5}$, $\frac{4\pi}{5}$, $\pi$, $\frac{6\pi}{5}$, $\frac{7\pi}{5}$, $\frac{\pi}{2}$, $\frac{8\pi}{5}$, $\frac{9\pi}{5}$, $2\pi$},
ytick=\empty,
enlargelimits=false, clip=true, axis on top]
\addplot [line width=0.5,cyan!50!black] {sin(deg(5*x))*cos(deg(x)};
\end{axis}
\end{tikzpicture}
\end{document}
In order to investigate this problem I first created a PDF from your posted tikz/tex code (after copying it into a tikz.tex file):
pdflatex tikz.tex
pdflatex tikz.tex
The resulting PDF does contain the illustration as a vector graphic, not a raster image. Hence, pdfimages -list will NOT detect it.
Then I tested two ways to convert the resulting PDF file to a PNG:
Using ImageMagick's convert (which employs Ghostscript behind your back as a 'delegate' to process the PDF input)
Using Ghostscript directly
1. Using convertwith -density 720
I've used this command to create a PNG from the PDF:
convert -density 720 tikz.pdf tikz1.png
Here is the result:
Why did I use -density 720? Because 720 PPI is the default resolution which Ghostscript uses when creating PDFs (unless you override this default setting by providing your own via -rNxM on the gs command line)...
The resulting image has a size of 374 kB (the PDF had 49 kB) and a width x height dimension of 8060 x 2390 pixels. Any pixelization (which will happen whenever you create a PNG!) is not immediately visible at that resolution.
The runtime for a loop running this command 10 times was 47 seconds.
2. Using Ghostscript directly
To achieve the direct PNG conversion with a Ghostscript command I used:
gs -o tikz-gs.png -sDEVICE=pngalpha \
-dAlignToPixels=0 -dGridFitTT=2 \
-dTextAlphaBits=4 -dGraphicsAlphaBits=4 \
tikz.pdf
Here is the resulting PNG:
It's file size is 308 kB, with dimensions of 8060 x 2390 pixels.
The runtime for a loop running this command 10 times was 17 seconds.
Analysis
PDF input file: Sizes of components
After looking at the source code of the PDF file after uncompressing all objects, I came up with the following statistics:
Total size of 5 embedded Type1 fonts................................ 38615 Bytes
Total size of v`/Contents` stream (mainly used by vector drawing)... 32630 Bytes
Rest of PDF structure ("overhead", if you want)..................... 5827 Bytes
---------------------------------------------------------------------------------
Total size of PDF (after uncompressing objects)..................... 77072 Bytes
The fonts are Type 1 (i.e. PostScript) fonts, according to the output of pdffonts. They are all embedded as subsets:
pdffonts tikz.pdf
name type encoding emb sub uni object ID
-------------------------- ------------ ---------------- --- --- --- ---------
FXXUVH+CMSY10 Type 1 Builtin yes yes no 7 0
BCSIZL+CMR10 Type 1 Builtin yes yes no 8 0
SFJZUV+CMMI10 Type 1 Builtin yes yes no 9 0
WPSSUY+CMR7 Type 1 Builtin yes yes no 10 0
SYHYOI+CMMI7 Type 1 Builtin yes yes no 11 0
Because...
...Fonts (unless they are raster fonts) are a different way to very efficiently code vector shapes for glyphs depicting text characters,
...Fonts + Vector drawing compose more than 90% of the total PDF size,
...there is no way in hell that lets you create a PNG raster image from the (compressed) PDF sized 49 kB (uncompressed size was 75 kB) that isn't larger by a few times than the original PDF file if you want to avoid directly visible "pixelization" and "blur".
Even if you use a resolution of 720 PPI (which creates a 308 kB-sized PNG), you'll still see pixelization once you start zooming in. Such pixelization does not occur with the PDF (because all its shapes are defined as vectors).
The following three images are screenshots:
top, from the tikz.pdf file at a high zoom level (~1000%),
center, from the tikz.png created with 720 PPI (at a similar zoom level),
bottom, from the tikz72.png created with 72 PPI (at a similar zoom level):
The text sizes used for the annotation of the coordinate axis are only around 10 points. If you rasterize those, you'll get clearly visible pixelization at any resolution below 400 PPI, maybe even above...
My Ghostscript is a self-compiled 9.17 GIT PRERELEASE. My ImageMagick is 6.9.0-0 Q16 x86_64.
I am trying to accomplish something a bit backwards from everyone else. Given an array of sensor data, I wish to print a graph plot of it. My test bench uses a stepper motor to move the input shaft of a sensor, stop, get ADC value of sensor's voltage, repeat.
My current version 0.9 bench does not have a graphical output. The proper end solution will. Currently, I have 35 data points, and I'm looking to get 90 to 100. The results are simply stored in an int array. The index is linear, so it's not a complicated plot, but I'm having problems conceptualizing the plot from bottom-left to top-right to display to the operator. I figure on the TFT screen, I can literally translate an origin and then draw lines from point to point...
Worse, I want to also print out this to a thermal printer, so I'll need to translate this into a sub-384 pixel wide graph. I'm not too worried about the semantics of communicating the image to the printer, but how to convert the array to an image.
It gets better: I'm doing this on an Arduino Mega, so the libraries aren't very robust. At least it has a lot of RAM for the code. :/
Here's an example of when I take my data from the Arduino test and feed it into Excel. I'm not looking for color, but I'd like the graph to appear and this setup not be connected to a computer. Or the network. This is the ESC/POS printer, btw.
The algorithm for this took three main stages:
1) Translate the Y from top left to bottom left.
2) Break up the X into word:bit values.
3) Use Bresenham's algorithm to draw lines between the points. And then figure out how to make the line thicker.
For my exact case, the target bitmap is 384x384, so requires 19k of SRAM to store in memory. I had to ditch the "lame" Arduino Mega and upgrade to the ChipKIT uC32 to pull this off, 32k of RAM, 80 MHz cpu, & twice the I/O!
The way I figured out this was to base my logic on Adafruit's Thermal library for Arduino. In their examples, they include how to convert a 1-bit bitmap into a static array for printing. I used their GFX library to implement the setXY function as well as their GFX Bresenham's algorithm to draw lines between (X,Y)s using my setXY().
It all boiled down to the code in this function I wrote:
// *bitmap is global or class member pointer to byte array of size 384/8*384
// bytesPerRow is 384/8
void setXY(int x, int y) {
// integer divide by 8 (/8) because array size is byte or char
int xByte = x/8;
// modulus 8 (%8) to get the bit to set
uint8_t shifty = x%8;
// right shift because we start from the LEFT
int xVal = 0x80 >> shifty;
// inverts Y from bottom to start of array
int yRow = yMax - y;
// Get the actual byte in the array to manipulate
int offset = yRow*bytesPerRow + xByte;
// Use logical OR in case there is other data in the bitmap,
// such as a frame or a grid
*(bitmap+offset)|=xVal;
}
The big point is to remember with an array, we are starting at the top left of the bitmap, going right across the row, then down one Y row and repeating. The gotchya's are in translating the X into the word:bit combo. You've got to shift from the left (sort-of like translating the Y backwards). Another gotchya is one-off error in bookkeeping for the Y.
I put all of this in a class, which helped prevent me from making one big function to do it all and through better design made the implementation easier than I thought it would be.
Pic of the printout:
Write-up of the project is here.
I have a typedef structure containing the YUV components that I took from a video frame.
I need to find a way to write these YUV components into a (.YUV) file extension so I could use it for my application in C language.
Does anyone know, How can I can do it?
Best Regards
Well, one of the more common yuv-formats (or rather YCbCr) is YV12
Its is also known as plane-separated 4:2:0 8bpp. Where the 4:2:0 denoted the subsampling used for the color components. 8bpp means the valid range is [0-255] (not realy true, but lets not go there right now...)
It consists of width x height bytes luma-samples (the Y-part) followed by width x height / 4 Cb-data and the same amount of Cr-data. Total number of bytes for one frame is width x height * 3 / 2
So basically all you need to do is to write the Y-component as uint8, followed by Cb-data and Cr-data.
If you need some example code in c to look at, check my yuv-viewer on github.
Hope it helps, if not, drop me a comment.
You will need to learn how to write to a file in binary mode. Take a look at these examples. Once that is clear, all you need to do is dump the hex values of the YUV channels into file.
I'm using a Custom s'print DPT100-S thermal printer to made a receipt printing application.
It is able to print graphics using 384 pixels in one line. This data has to be passed on to the printer using 48 bytes (48x8=384). So, each 'bit' represents one dot to be printed (bit will be '0' for white and '1' for black).
So, I need to create a program which will read a monochrome BMP generated in Windows Paint(or any other program) and convert it into this bit format using a C program in Linux.
Please guide me.
Pseudo code:
Read BMP
For each row in BMP
For each group of 8 pixels in row
output_byte = 0
For each pixel in current group of 8
output_byte <<= 1 // shift output_byte left by one bit
output_byte |= (pixel != 0) // set rightmost bit in output_byte
// according to input pixel value
Save output_byte in bitmap
Take a look at halftoning.
A quick Google will get you references and Java applet like here: http://www.markschulze.net/halftone/index.html
If you don't have to create your own program and you are happy to use off the shelf software, try ImageMagick's convert command: http://www.imagemagick.org/Usage/quantize/#halftone
e.g.
convert myfile.jpg -colorspace Gray -ordered-dither h4x4a printable-file.bmp
This link has a software called LCD assistant which does the same thing as you need. You have to use paint to convert any image to bitmap and then import that bmp image into the software. The output you can choose to be 384 X xyz. You get the output pixels in HEX.