Creating and displaying a bitmap - c++

What would be the easiest, most cross-platform way to create a bitmap (2D array of integers, or a quad-tree) and display it on the screen? I would also like to be able to save it as a file.
Thanks

It has to be said -- the easiest and most cross platform approach is probably to use printf, with something like:
// y and x loops would surround this...
unsigned char grayscaleValue = /* something */;
printf("%c",grayscaleValue < 128 ? " " : "X");
You could use more than two brightness values.
I also like both Qt and Juce; they're both relatively straightforward cross platform GUI toolkits. They can both be got up and running in an evening or two... the ascii printout (and its variations) can be done in an hour.

Related

XLib font size too small?

currently I'm using this font in my C++ program:
-misc-fixed-medium-r-normal--12-*-*-*-*-*-iso8859-15
where '12', the size, is also the font size I'm using currently with Linux Mint 18-1.
But when I draw in my program a string it is shown very small! It looks like it has a size of '6'!
Do I need to double the font size for my program, or something like that?
TIA
Regards
Earlybite
I was searching some hours the internet, also here, but I couldn't find an solution. Also in my "pre-version" of my program, I couldn't find the difference, because *there was a normal drawing with XLib and DrawString.
I also noticed, that even size = 40 hadn't a difference to e.g. size = 20. So there had to be a difference in coding.
So I went through the pre-version code line by line and at least I found that little line: XSetFont().
Which makes drawing strings normal.
E.g. like that:
XSetFont(mDisplay, vGC, this->mFontPtr.fid); // <-- HERE!
vGCVal.foreground = mXForeColorA->X_Color.pixel;
XChangeGC(mDisplay,vGC, GCForeground, &vGCVal);
XDrawString(mDisplay, vPix, vGC, x, y, nDrawString.c_str(), (int) nDrawString.length());

Basic terminal output using C++ - Questions

Well, the question may sound a bit too vague but here's 2 things I need to do and I'd definitely need some input on this :
Output something (e.g. using cout) with color (note: My TERM environment variable is set to xterm-color if that makes any difference; also, is there any uniform way to output colored text that's compatible with both pure mac and *nix terminals in general, so that the code is portable)
Output something at the same position on the terminal screen. OK, this may sound confusing too. Let's take a terminal app which simply outputs a progress percentage. It normally won't start a new line for that. The new value is shown at the very same spot. How is this doable? (Being a once Borland Pascal guy from the good old DOS days, the only thing I could think of is something to do with accessing video memory directly... or not?)
So... any ideas?
You probably want to use ncurses library. And ANSI escape codes can also be used for coloring.
1)
You can try Color cout , but that is not protable. I tried (ANSI escape codes) something like
cout << "\033[1;31mbold red text\033[0m\n";
cout << "\33[0;31m" << "Enter Your String here" << "\33[0m" << std::endl ;
You can also look at
How do I output coloured text to a Linux terminal?
2)
Are you looking for something like watch or top like app which are showing output at the same spot.

How do I save number of columns in Terminal with C++ to a variable?

I've been searching quite a lot, and all I've had are answers for C, not C++. I'm using Linux, so I won't be able to use windows.h
What I need to do is get the number of columns in the terminal window it's running, then print something in the middle with ncurses. How can I achieve this?
If you're going to be using ncurses, just use the facility (section 6.3.4) that exists in the library to do it:
int main(void) {
int rows, cols;
initscr();
getmaxyx(stdscr, rows, cols); // you now have the max for both axis
}
It's important to note that you should be refreshing these values (and the screen) upon receiving a SIGWINCH signal, or your windows will look rather odd if someone changes the height or width of their terminal program.
There is no C++ version of ncurses because it's not needed, but many prefer to create their own wrapper around it to get easier access to the functionality they want in the context of their application. The ncurses.h header will check to see if C++ is being used, and adjust accordingly:
#ifdef __cplusplus
extern "C" {
To use it, simply link it and use it procedurally, or use it in whatever class you have dealing with terminal I/O in your program.
Here is how :
int columns=system("tput cols");
You need to #include<stdlib.h>.
Then you can use mvprintw(y, x, "your text here") ; to print text wherever you want.
Note tput lines gives the number of rows, in case you want it too.
Note that i am ignoring the line I've been searching quite a lot, and all I've had are answers for C, not C++.. This is (one way of) how you do it, C or C++.
You may refer to this as an example.

WxTextCtrl unable to load large texts

I've read about the solutuon written here on a post a year ago
wx.TextCtrl.LoadFile()
Now I have a windows application that will generate color frequency statistics that are saved in 3D arrays. Here is a part of my code as you will see on the code below the printing of the statistics is dependent on a slider which specifies the threshold.
void Project1Frm::WxButton2Click(wxCommandEvent& event)
{
char stat[32] ="";
int ***report = pGLCanvas->GetPixel();
float max = pGLCanvas->GetMaxval();
float dist = WxSlider5->GetValue();
WxRichTextCtrl1->Clear();
WxRichTextCtrl1->SetMaxLength(100);
if(dist>0)
{
WxRichTextCtrl1->AppendText(wxT("Statistics\nR\tG\tB\t\n"));
for(int m=0; m<256; m++){
for(int n=0; n<256; n++){
for(int o=0; o<256; o++){
if((report[m][n][o]/max)>=(dist/100.0))
{
sprintf(stat,"%d\t%d\t%d\t%3.6f%%\n",m,n,o,report[m][n][o]/max*100.0);
WxRichTextCtrl1->AppendText(wxT(stat));
}
}
}
}
}
else if(dist==0) WxRichTextCtrl1->LoadFile("histodata.txt");
}
The solution I've tried so far is that when I am to print all the statistics I'll get it from a text file rather than going through the 3D array... I would like to ask if the Python implementation of the segmenting can be ported to C++ or are there better ways to deal with this problem. Thank you.
EDIT:
Another reason why I used a text file instead is that I observed that whenever I do sprintf only [with the line WxRichTextCtrl1->AppendText(wxT(stat)); was commented out] the computer starts to slow down.
-Ric
Disclaimer: My answer is more of an alternative than a solution.
I don't believe that there's any situation in which a user of this application is going to find it useful to have a scrolled text window containing ~16 million lines of numbers. It would be impossible to scroll to one specific location in the list that the user might need to see easily. This is all assuming that every single number you output here has some significance to the user of course (you are showing them on the screen for a reason). Providing the user with controls to look up specific, fixed (reasonable) ranges of those numbers would be a better solution, not only in regards to a better user experience, but also in helping to resolve your issue here.
On the other hand, if you still insist on one single window containing all 64 million numbers, you seem to have a very rigid data structure here, which means you can (and should) take advantage of using a virtual grid control (wxGrid), which is intended to work smoothly even with incredibly large data sets like this. The user will likely find this control easier to read and find the section of data they are looking for.

How to print subscripts/superscripts on a CLI?

I'm writing a piece of code which deals with math variables and indices, and I'd need to print subscripts and superscripts on a CLI, is there a (possibly cross-platform) way to do that? I'm working in vanilla C++.
Note: I'd like this to be cross-platform, but since from the first answers this doesn't seem to be possible I'm working under MacOS and Ubuntu Linux (so bash).
Thank you
Since most CLIs are really only terminals (pretty dumb ones mostly but sometimes with color), the only cross-platform way I've ever done this is by allocating muliple physical lines per virtual line, such as:
2
f(x) = x + log x
2
It's not ideal but it's probably the best you're going to get without a GUI.
Following you extra information as to what platforms you're mainly interested in:
With Ubuntu at least, gnome-terminal runs in UTF-8 mode by default so the following code shows how to generate the superscripts and subscripts:
#include <stdio.h>
static char *super[] = {"\xe2\x81\xb0", "\xc2\xb9", "\xc2\xb2",
"\xc2\xb3", "\xe2\x81\xb4", "\xe2\x81\xb5", "\xe2\x81\xb6",
"\xe2\x81\xb7", "\xe2\x81\xb8", "\xe2\x81\xb9"};
static char *sub[] = {"\xe2\x82\x80", "\xe2\x82\x81", "\xe2\x82\x82",
"\xe2\x82\x83", "\xe2\x82\x84", "\xe2\x82\x85", "\xe2\x82\x86",
"\xe2\x82\x87", "\xe2\x82\x88", "\xe2\x82\x89"};
int main(void) {
int i;
printf ("f(x) = x%s + log%sx\n",super[2],sub[2]);
for (i = 0; i < 10; i++) {
printf ("x%s x%s ", super[i], sub[i]);
}
printf ("y%s%s%s z%s%s\n", super[9], super[9], super[9], sub[7], sub[5]);
return 0;
}
The super and sub char* arrays are the UTF-8 encodings for the Unicode code points for numeric superscripts and subscripts (see here). The given program will output my formula from above (on one line instead of three), then another test line for all the choices and a y-super-999 and z-sub-75 so you can see what they look like.
MacOS doesn't appear to use gnome-terminal as a terminal program but references here and here seem to indicate the standard terminal understands UTF-8 (or you could download and install gnome-terminal as a last resort).
I'd need to print subscripts and superscripts on a CLI, is there a cross-platform way to do that?
Only if you have a Unicode-capable terminal, which is far from guaranteed. Unicode defines a limited number of sub- and superscript ‘compatibility characters’, you certainly can't use it on any old letter:
₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎ₐₑₒₓ
⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾ⁿⁱ
Even then you're reliant on there being a glyph for it in the console font, which is also far from guaranteed. Superscript 2 and 3 are likely to exist as they're present in ISO-8859-1; the others may well not work.