Display the next frame of sprite GDI - gdi+

I've a sprite which contains a lot of images. I see the BitBlt only display the position and imageWidth and imageHeight.
If I want to display a bitmap but only from Width[24] to Width[48] not from the beginning of that bitmap
Thanks for reading this :)

Here is the BitBlt signature:
BOOL BitBlt(
__in HDC hdcDest,
__in int nXDest,
__in int nYDest,
__in int nWidth,
__in int nHeight,
__in HDC hdcSrc,
__in int nXSrc,
__in int nYSrc,
__in DWORD dwRop
);
To copy only part of the source onto your destination DC, you would use nXSrc = 24 and nWidth = 24 (to match your example of copying from columns 24 through 48).

Related

How to retrieve an int array that is stored in a table using PROGMEM?

I'm new to Arduino and currently learn to use PROGMEM to store variables so that I can save dynamic memory. I have 13 variables including these three below that I store using PROGMEM.
Here are some of example of variables that I store and use it in my functions :-
const unsigned int raw_0[62] PROGMEM = {2600,850,400,500,400,500,450,850,450,850,1350,850,450,450,400,500,400,450,450,400,450,450,450,450,400,450,900,850,900,850,900,450,450,850,900,850,900,850,450,450,900,450,400,450,400,900,450,450,450,400,450,450,450,450,400,450,450,450,450,400,450,};
const unsigned int raw_1[60] PROGMEM = {2600,850,450,450,450,450,450,850,450,850,1350,850,500,400,450,400,450,450,450,450,400,450,450,450,400,450,900,850,900,900,850,450,450,850,850,900,900,900,400,450,900,450,450,400,450,850,450,450,450,450,400,450,450,450,450,400,450,450,850,};
const unsigned int raw_a[100] PROGMEM = {3500,1700,400,450,450,1250,450,400,450,400,450,400,500,400,450,400,450,400,450,400,450,450,400,400,500,400,450,400,450,1300,400,450,450,400,450,400,450,400,450,400,450,400,500,350,500,400,450,400,450,1300,400,400,500,400,450,400,450,400,450,450,400,450,450,400,450,400,450,400,450,400,450,450,400,450,450,400,450,1250,450,400,450,400,500,400,450,400,450,400,450,400,450,400,450,1300,450,400,450,1250,450,};
Here is the table that store the variables. I learn this approach from Arduino website; https://www.arduino.cc/en/Reference/PROGMEM .
const unsigned int* const myTable[13] PROGMEM = {
raw_0,
raw_1,
raw_2,
raw_3,
raw_4,
raw_5,
raw_6,
raw_7,
raw_8,
raw_9,
raw_a,
raw_b,
raw_c};
My problem is, how do I retrieve these variables using PROGMEM such as raw_1 and raw_a ?
This is what I did but it did not work :-
unsigned int * ptr = (unsigned int *) pgm_read_word (&myTable [1]);
irsend.sendRaw(ptr,62,38);
Most of examples that I found, they use String or char datatype but in my case, I use array integer.
The ptr is also pointer to PROGMEM, so you have to read the value (or values in this case) by pgm_read_word. The IR library doesn't support that at all (I hope it's the correct one).
Anyway sendRaw implementation is this:
void IRsend::sendRaw (const unsigned int buf[], unsigned int len, unsigned int hz)
{
// Set IR carrier frequency
enableIROut(hz);
for (unsigned int i = 0; i < len; i++) {
if (i & 1) space(buf[i]) ;
else mark (buf[i]) ;
}
space(0); // Always end with the LED off
}
And all used methods are public, so you can implement your own function to do the same:
void mySendRaw (IRsend & dev, const unsigned int buf[], unsigned int len, unsigned int khz)
{
// Set IR carrier frequency
dev.devenableIROut(khz);
for (unsigned int i = 0; i < len; i++) {
if (i & 1) dev.space(pgm_read_word(buf+i));
else dev.mark (pgm_read_word(buf+i));
}
dev.space(0); // Always end with the LED off
}
// And usage:
mySendRaw(irsend, (const uint16_t*)pgm_read_word(myTable+1), 62, 38);
However the size of arrays should be stored somewhere too, so you can use something like:
byte cmd = 1;
mySendRaw(irsend, (const uint16_t*)pgm_read_word(myTable+cmd), pgm_read_word(myTableLenghts+cmd), 38);

Any ideas, how to encode QR data into png image?

All I need is simple (Q)string to put it as embedded image like:
<img src="data:image/png;base64,iVBORw...">
I use: #include <qrencode.h> (linux -> apt-get install libqrencode-dev)
This is my code:
QRcode *qr=QRcode_encodeString(QString("my test string").toStdString().c_str(), 1, QR_ECLEVEL_L, QR_MODE_8,1);
QByteArray *ba = new QByteArray();
for (unsigned int y=0; y<qr->width;y++)
{
int yy=y*qr->width;
for (unsigned int x=0; x<qr->width;x++)
{
int xx=yy+x;
const unsigned char b=qr->data[xx];
///WHAT TO DO NOW??? IS IT CORRECT?
ba->push_back(b);
qDebug()<<"Char "<<b;
if(b &0x01)
{
qDebug()<<"Point +++";
}
}
}
qDebug()<<ba->toBase64();
Any ideas, how to encode qr->data into a png image?
I did it! :)
First version, without scaling
#include<QString>
#include<QDebug>
#include<QByteArray>
#include<QBuffer>
#include<QImage>
#include<QImageWriter>
#include<QPixmap>
#include<QPainter>
#include<QColor>
#include<QPointF>
#include<QRectF>
//ustawiam kolory
QColor bialy = Qt::white;
QColor czarny = Qt::black;
//PNG
QByteArray ImageAsByteArray;
QBuffer ImageBuffer(&ImageAsByteArray);
ImageBuffer.open(QIODevice::WriteOnly);
QRcode *qr=QRcode_encodeString(QString("afya.pl").toStdString().c_str(), 1, QR_ECLEVEL_L, QR_MODE_8,1);
QPixmap p(qr->width,qr->width);
QPainter pa;
pa.begin(&p);
pa.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing |QPainter::TextAntialiasing| QPainter::Antialiasing);
pa.setPen(bialy);
pa.setBrush(bialy);
//czyścimy tło
QPointF a=QPointF(0.0,0.0);
QPointF b=QPointF(p.width(),p.height());
pa.drawRect(QRectF(a,b));
pa.setPen(czarny);
for (unsigned int y=0; y<qr->width;y++)
{
int yy=y*qr->width;
for (unsigned int x=0; x<qr->width;x++)
{
int xx=yy+x;
const unsigned char b=qr->data[xx];
if(b &0x01){
a=QPointF(y,x);
pa.drawPoint(a);
}
}
}
p.save(&ImageBuffer,"PNG");
qDebug()<<ImageAsByteArray.toBase64();
}

Save byte array as .png/.jpg file in QT

This is what I have so far:
QFile file(fileName);
file.open(QIODevice::WriteOnly);
QPixmap pixmap = QPixmap.loadFromData((const uchar *) imageBuffer_pointer, (sizeof(imageRows) * sizeof(imageCols));
pixmap.save(&file, "JPG");
pixmap.save(&file, "PNG");
But it only produces 0 byte image files
It is much easier to just give the save() function a file name as a first parameter:
QPixmap pixmap = QPixmap.loadFromData((const uchar *) imageBuffer_pointer,
(sizeof(imageRows) * sizeof(imageCols));
pixmap.save(fileName, "JPG");

Make buffer of cv mat Point to the buffer of QImage

I would like to make the buffer of cv::mat point to the buffer of QImage but not copy
the data of QImage into the cv::mat.
cv::Mat const reference_qimage_to_mat(QImage const &img, int format)
{
cv::Mat mat(img.height(), img.width(), format);
for(int i = 0; i != mat.rows; ++i)
{
//pseudo code, wouldn't work
//mat.ptr(i) = img.scanLine(i);
}
return mat;
}
I try to search the answer by google but I could only find how to copy
the data of QImage into cv::mat.Thanks
the cv::Mat object is simply a header for the image data, so you can do it upon your object construction:
cv::Mat mat(img.height(), img.width(), type, img.bits());
where type depends on your data, CV_8UC1 for single channel, CV_8UC3 for RGB, etc.

How to convert LPTSTR to QString

Hi can anyone help me to convert LPTSTR to QString
You will see in the docs that Qstring provides static function to convert from both ascii and Unicode strings:
QString fromAscii ( const char *
ascii, int len = -1 )
QString fromLatin1 ( const char *
chars, int len = -1 )
QString fromUtf8 ( const char * utf8,
int len = -1 )
QString fromLocal8Bit ( const char *
local8Bit, int len = -1 )
QString fromUcs2 ( const unsigned
short * str )
Check whether you are using ascii or unicode and pick your poison.
QString::fromWCharArray is what worked for me.
To convert QString to LPTSTR or LPCTSTR:
QString src;
LPTSTR dest=(LPTSTR)src.utf16();
to convert from LPTSTR or LPCTSTR to QString :
src=QString::fromUtf16(dest);
The solution expects that your LPTSTR array is null-terminated.
STRRET str; // it had been filled by a winapi function
// str.pOleStr is LPWSTR, i.e. wchar_t* field of the union STRRET
// LPWSTR is the same as LPTSTR (see further)
QString result{QString::fromWCharArray(str.pOleStr)};
As shown below, STRRET is a union, one of representations of which (named pOleStr) has type wchar_t*.
As far as I understand, when one gets this STRRET filled by a Winapi function (like e.g. IShellFolder::GetDisplayNameOf()), it's a normal null-terminated wide string. So it can be supplied to QString::fromWCharArray(const wchar_t *string). The function will preserve the input wchar_t-array as it is and will just create a new copy inside a new QString. So overall, I think the method in my answer is quite safe as far as you know that your const wchar_t * is ended with a \0-wide-character.
// shtypes.h
typedef struct _STRRET
{
UINT uType;
/* [switch_is][switch_type] */ union
{
/* [case()][string] */ LPWSTR pOleStr;
/* [case()] */ UINT uOffset;
/* [case()] */ char cStr[ 260 ];
} DUMMYUNIONNAME;
} STRRET;
// winnt.h
typedef _Null_terminated_ WCHAR *NWPSTR, *LPWSTR, *PWSTR;
typedef LPWSTR PTSTR, LPTSTR;
Use QString::fromUcs2 to convert strings.
This is woking fine
QString str("ddddd");
LPCTSTR lstr = (LPCTSTR)str.data();

Resources