I'm wondering if there is a way to cout an image. A very basic question, which I don't know exists, and very well may not exist. Thanks for any help
I'm wondering if there is a way to cout an image. A very basic question, which I don't know exists, and very well may not exist. Thanks for any help
Impossible.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
cout is for printing text to the screen. Nothing to do with pictures. cout an image doesn't make any sense, atleast to me.
Nope. The cout object wouldn't know how to handle an image. It wasn't designed for that. It was designed to be a output stream (or sequence if you like) of characters.
You can however pretend you draw images with it...
http://www.adrianxw.dk/SoftwareSite/...Consoles4.html
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Lol, try it! Read an image into a buffer and cout that to the screen. You'll get some interesting stuff but I'll guarantee it won't be a picture . -plays air guitar-
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
Well, you can overload operator<<
Code:#include <windows.h> #include <iostream> class Bitmap { public: Bitmap(const char* file) { m_hImage = (HBITMAP)LoadImage(GetModuleHandle(0),file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE); } bool operator!() { return m_hImage==0?true:false; } void Draw() { HDC hdc = GetDC(GetConsoleWindow()); BITMAPINFO bmi = {0}; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); if (!GetDIBits(hdc,m_hImage,0,0,0,&bmi,DIB_RGB_COLORS)) std::cerr<<"Failed to get bitmap info"; int width = bmi.bmiHeader.biWidth; int height = bmi.bmiHeader.biHeight; char* bits = new char[width*height*bmi.bmiHeader.biBitCount+sizeof(BITMAPINFOHEADER)]; memcpy(bits, &bmi.bmiHeader, sizeof(BITMAPINFOHEADER)); if(!GetDIBits(hdc, m_hImage, 0, height, bits+bmi.bmiHeader.biSize, (LPBITMAPINFO)bits, DIB_RGB_COLORS)) std::cerr<<"Failed to get bits: "<<GetLastError(); if (!SetDIBitsToDevice(hdc,0,0,width,height,0,0,0,height,bits+bmi.bmiHeader.biSize,(LPBITMAPINFO)bits,DIB_RGB_COLORS)) std::cerr<<"Failed to draw"<<GetLastError(); ReleaseDC(GetConsoleWindow(),hdc); } private: HBITMAP m_hImage; }; std::ostream& operator<<(std::ostream& os, Bitmap& image) { image.Draw(); return os; } int main() { Bitmap image("image.bmp"); if(!image) { std::cerr<<"Failed to load image"; return 1; } std::cout<<image; }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
mburt,
The sad news is:
There are no graphics, color, sound, mouse, or networking in Standard ANSI/ISO C++. Once you step outside of Standard C++, your code is no longer portable to other systems. (Or, at least not as-portable... There are cross-platform libraries.) Most real world programs contain non-standard code. That's why most commercial programs are only available for one platform!
The good news is:
If you're using a Windows compiler on a Windows system, all of that stuff is included in the WinAPI. It's fairly easy to display a bitmap. It get's a tiny bit trickier if you need to scale (re-size) the bitmap. If you want to display a JPEG, or other compressed image format, it has to be decoded first. That might require a CODEC or additional custom code.
In general if your computer can do it, either your compiler can do it with it's included "extra" libraries, or your compiler can do it along with a 3rd-party library.
Well then. Now that everyone has rained on my parade....
Okay, I'll see what I can do.
For some reason Dev-C++ won't compile this....Code:#include <windows.h> #include <iostream> class Bitmap { public: Bitmap(const char* file) { m_hImage = (HBITMAP)LoadImage(GetModuleHandle(0),file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE); } bool operator!() { return m_hImage==0?true:false; } void Draw() { HDC hdc = GetDC(GetConsoleWindow()); BITMAPINFO bmi = {0}; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); if (!GetDIBits(hdc,m_hImage,0,0,0,&bmi,DIB_RGB_COLORS)) std::cerr<<"Failed to get bitmap info"; int width = bmi.bmiHeader.biWidth; int height = bmi.bmiHeader.biHeight; char* bits = new char[width*height*bmi.bmiHeader.biBitCount+sizeof(BITMAPINFOHEADER)]; memcpy(bits, &bmi.bmiHeader, sizeof(BITMAPINFOHEADER)); if(!GetDIBits(hdc, m_hImage, 0, height, bits+bmi.bmiHeader.biSize, (LPBITMAPINFO)bits, DIB_RGB_COLORS)) std::cerr<<"Failed to get bits: "<<GetLastError(); if (!SetDIBitsToDevice(hdc,0,0,width,height,0,0,0,height,bits+bmi.bmiHeader.biSize,(LPBITMAPINFO)bits,DIB_RGB_COLORS)) std::cerr<<"Failed to draw"<<GetLastError(); ReleaseDC(GetConsoleWindow(),hdc); } private: HBITMAP m_hImage; }; std::ostream& operator<<(std::ostream& os, Bitmap& image) { image.Draw(); return os; } int main() { Bitmap image("image.bmp"); if(!image) { std::cerr<<"Failed to load image"; return 1; } std::cout<<image; }
It says:
'GetConsoleWindow' undeclared (first use this function)
My bad. I accidentally left out the very first line:
You'll need windows 2000 or later to run it, thoughCode:#define _WIN32_WINNT 0x0500
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
Good lord... I can't seem to get this to work in Dev-C++.
Here is what I have:
And it says:Code:#define _WIN32_WINNT 0x0500 #include <windows.h> #include <iostream> class Bitmap { public: Bitmap(const char* file) { m_hImage = (HBITMAP)LoadImage(GetModuleHandle(0),file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE); } bool operator!() { return m_hImage==0?true:false; } void Draw() { HDC hdc = GetDC(GetConsoleWindow()); BITMAPINFO bmi = {0}; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); if (!GetDIBits(hdc,m_hImage,0,0,0,&bmi,DIB_RGB_COLORS)) std::cerr<<"Failed to get bitmap info"; int width = bmi.bmiHeader.biWidth; int height = bmi.bmiHeader.biHeight; char* bits = new char[width*height*bmi.bmiHeader.biBitCount+sizeof(BITMAPINFOHEADER)]; memcpy(bits, &bmi.bmiHeader, sizeof(BITMAPINFOHEADER)); if(!GetDIBits(hdc, m_hImage, 0, height, bits+bmi.bmiHeader.biSize, (LPBITMAPINFO)bits, DIB_RGB_COLORS)) std::cerr<<"Failed to get bits: "<<GetLastError(); if (!SetDIBitsToDevice(hdc,0,0,width,height,0,0,0,height,bits+bmi.bmiHeader.biSize,(LPBITMAPINFO)bits,DIB_RGB_COLORS)) std::cerr<<"Failed to draw"<<GetLastError(); ReleaseDC(GetConsoleWindow(),hdc); } private: HBITMAP m_hImage; }; std::ostream& operator<<(std::ostream& os, Bitmap& image) { image.Draw(); return os; } int main() { Bitmap image("image.bmp"); if(!image) { std::cerr<<"Failed to load image"; return 1; } std::cout<<image; }
"undefined reference to GetDIBits"
Good one.Originally Posted by JaWiB
>>"undefined reference to GetDIBits"<<
You need to link with the gdi32 library: project menu --> project options, select the 'parameters' tab, and add -lgdi32 to the 'linker' field.
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
Well I wasn't thinking 'good one' but it is a different type of example for operator overloading. Not sure I would use an operator like that, but hey the OP did ask if it was possible.
Nice post Jawib.
It amused me; 'twas an interesting thing to do with a DOS window and a clever answer to the question.Originally Posted by Bubba
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
We'll be cout-ing WAV files and MP3s next...
But IMO this is all just fun curio, it just adds unneeded complexity into your code. Best to achieve this using standard function calls; it is faster, and simpler to comprehend and (very importantly) maintain.
Last edited by jafet; 08-24-2006 at 10:35 AM.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}