is there a way to change the text's font in Cimg? - visual-c++

i wanted to know if i can draw a text in Cimg graphic library draw_text function and change the font of the text to another font ?

You can't load own fonts in CImg, but you can see example https://github.com/tttzof351/CImgAndFreetype for load custom fonts using freetype and render text on bitmap using cimg.

No. CImg's text drawing is very simplistic.
CImg<T>& draw_text(const int x0, const int y0,
const char *const text,
const int, const tc *const background_color,
const float opacity, const CImgList<t>& font, ...)
font is just a CImgList of letters (i.e. font[letter-'a'] is an image of "letter"). Either make your own or use one of the built-in options:
static const CImgList<T>& font(const unsigned int font_height,
const bool variable_size=true);
or
static CImgList<T> _font(const unsigned int *const font,
const unsigned int w, const unsigned int h,
const bool variable_size)
where font here is one of the predefined fonts at the top of CImg.h such as font12x24.

Assuming you mean this Cimg library, a couple of the overloads of draw_text take parameters named "font". Those seem like a reasonable starting point...

Related

How can I change app's thumbnail in the taskbar? SFML 2.4

Someone know how can I change or make a thumbnail in SFML 2.4?
Solution can be quickly found on SFML documentation page, here.
sf::RenderWindow::setIcon() method can give the application window an icon, however the actual icon must be represented by a pointer to an array of pixels.
This can be achieved by creating a .rc header file and a .c file containing the pixel array. The array can be created using GIMP's "C-Source image dump" ability.
Example:
The .rc file:
//icon.rc
IDR_APP_ICON ICON "icon.ico"
GLUT_ICON ICON "icon.ico"
The .c file:
//icon.c
/* GIMP RGBA C-Source image dump (icon.c) */
static const struct {
unsigned int width;
unsigned int height;
unsigned int bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */
unsigned char pixel_data[32 * 32 * 4 + 1];
} gimp_image = {
32, 32, 4,
"k\177h\377k\177h\377\377\377\377\0\377\377\377\0\377\377\377\0\204`\236\201"`
//The array pixel array would continue here until }; closing the struct.
The generated struct object can be then passed as an argument to the setIcon() method.
sf::RenderWindow::setIcon(gimp_image.width, gimp_image.height, gimp_image.pixel_data);
And icon.c file should be included in your main.cpp file or wherever it is you set the icon to the RenderWindow.

Why can't I render onto a Skia canvas holding a 8 bit Grayscale bitmap

I am trying to do some basic drawing with skia. Since I'm working on grayscale images I want to use the corresponding color type. The minimal Example I want to use is:
int main(int argc, char * const argv[])
{
int width = 1000;
int heigth = 1000;
float linewidth = 10.0f;
SkImageInfo info = SkImageInfo::Make(
width,
heigth,
SkColorType::kAlpha_8_SkColorType,
SkAlphaType::kPremul_SkAlphaType
);
SkBitmap img;
img.allocPixels(info);
SkCanvas canvas(img);
canvas.drawColor(SK_ColorBLACK);
SkPaint paint;
paint.setColor(SK_ColorWHITE);
paint.setAlpha(255);
paint.setAntiAlias(false);
paint.setStrokeWidth(linewidth);
paint.setStyle(SkPaint::kStroke_Style);
canvas.drawCircle(500.0f, 500.0f, 100.0f, paint);
bool success = SkImageEncoder::EncodeFile("B:\\img.png", img,
SkImageEncoder::kPNG_Type, 100);
return 0;
}
But the saved image does not contain the circle that was drawn. If I replace kAlpha_8_SkColorType with kN32_SkColorType I get the expected result. How can I draw the circle onto a 8 bit grayscale image? I'm working with Visual Studio 2013 on a 64bit Windows machine.
kN32_SkColorType type result
kAlpha_8_SkColorType result
You should use kGray_8_SkColorType than kAlpha_8_SkColorType.
The kAlpha_8_SkColorType used for bitmap mask.

J2ME - How to copy area into Image in Canvas?

I want to copy any area in Canvas with x and y coordinates and then paste it to any Image.
In example:
Image image = Image.createImage(30, 20);
image.drawImage(canvas);
It is not possible to copy data from Canvas nor GameCanvas.
To achieve what you want, you have to use doublebuffering. Meaning, you have to use an Image bufferImg as your buffer to draw on, and then draw that bufferImg onto your Canvas.
That way, you can copy from the Image onto another Image like this:
bufferImg.getRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height);
and then
g.drawRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height, boolean processAlpha);
where g is the Graphics object of the 2nd Image you want to draw onto.
Using a doublebuffer like this will slow things down a bit on some devices of course, but you can't do it any other way if you want to be able to "extract" portions of the screen.

C++ Error C2061

I was trying to set the BackColor of my form in C++, and I a syntax error, to do with the 'FromArgb' statement, when using the code:
this->BackColor = gcnew Color::FromArgb(0,0,15);
What should I do?
You haven't given us enough context to answer, but I'll hazard a guess that FromArgb is a function, not a type, in which case it doesn't make sense to new (or gcnew) it.
If that's the case, and assuming BackColor is a Color object and not a pointer, and that FromArgb returns a Color by value, then you want
this->BackColor = Color::FromArgb(0,0,15);
If that doesn't work, please let us know exactly what BackColor and FromArgb are.
Color is a public value class Color - hence gcnew is wrong.
Also the number of arguments do not match:
public: static Color FromArgb(
unsigned char a,
unsigned char r,
unsigned char g,
unsigned char b)

Convert a string of bytes to cv::mat

I need to implement a function that receives a string containing the bytes of an image (received via boost socket connection) and converts the info into an OpenCV cv::Mat.
I also know the width and height of the image and its size in bytes. My function looks like this:
void createImageFromBytes(const std::string& name, std::pair<int,int> dimensions, const std::string& data)
{
int width,height;
width = dimensions.first;
height = dimensions.second;
//convert data to cv::Mat image
std::string filepng = DATA_PATH"/" + name +".png";
imwrite(filepng, image);
}
Which is the best method for doing this? Does OpenCV has a constructor for Mat from a string?
OpenCV Mat has a constructor from vector<byte>, but this is not so intuitive. You need to convert from string to vector this way first:
std::vector<byte> vectordata(data.begin(),data.end());
Then you can create a cv::Mat from the vector:
cv::Mat data_mat(vectordata,true);
You also need to decode the image (check documentation for which types are allowed, png, jpg, depending on the OpenCV version)
cv::Mat image(cv::imdecode(data_mat,1)); //put 0 if you want greyscale
Now you can check if the resulting size of the image is the same as the one you sent:
cout<<"Height: " << image.rows <<" Width: "<<image.cols<<endl;
Easy to trip here as the image may have null characters and any c function handling string will see null as string end
Read the image
cv::Mat image;
image = cv::imread("../test/image.png", CV_LOAD_IMAGE_COLOR);
Convert to Bytes (this is just working code, not checked for leaks)
int dataSize = image.total() * image.elemSize();
//convert to bytes
std::vector<char> vec(dataSize);
memcpy(&vec[0], reinterpret_cast<char *>(image.data), dataSize);
std::string test2(vec.begin(), vec.end());
Test and see if conversion works
//test
cv::Mat data_mat(height,width,CV_8UC3,const_cast<char*>(test2.c_str()));
imwrite("out2.png", data_mat);
If the data in the string is raw pixels (rather than a Jpeg/png etc) you can create the cv::mat directly
// assuming an RGB image in bytes
cv::Mat mat(height,width,CV_8UC3,string.data());
Here is my improved solution of Jav_Rock, the problem is that is not clear to use vector (byte type is not defined in c++, i didn't found that), instead of that, use vector, here is a example code
int func(char * pfile){
string strfile = pfile;
std::vector<unsigned char> vectordata(strfile.begin(),strfile.end());
Mat data_mat(vectordata, true);
Mat graySacleFrame = imdecode(data_mat, 0); //PGM image
...
}

Resources