Quick way to output a picture in C++ - c++

I'm coding a physical simulation on 2d array and I'm now thinking that I could benefit from having a graphical output. My system is an array of cells (up to 2048*2048 of them) taking binary values, until now I used a prompt or text file output of '+' and '-' but it's not efficient for 2048*2048 lattice and maybe outputting in an image would be quicker and neater. Still, I've never done that. Ideally a library allowing me to write blue and red pixels/cell while parsing my lattice would get the job done. Are there some pre-existing not too long tools for doing it in c++?
Edit: I think that I just found what I was looking for: png++
After no more than 10 lines of coding I got the following output:
All I was asking for! Thank you for the suggestions ;)

You can easily get away without using an external imaging library by outputting a very simple format such as PGM or PBM. Refer to the wikipedia page on Netbpm for more details, but you're essentially outputting all the values as either ASCII or binary numbers, then any image viewer or editor that supports PGM (many of which do) can open and display them. Even if you don't have an editor, something like ImageMagick can easily convert it to a PNG or any other more accessible format.
I've used this technique in the past to quickly visualize 2D data, as you're intending to.

C++ does not have native support for graphics. You need an additional C++ library.
Personally, I suggest you to use Qt, which is free, powerful and cross-platform.

Related

Creating PDF from PS programmatically on embedded platform

Is there a library/tool that can be used in C/C++ that would convert the PS (post script) file to .PDF file, on embedded platform (proprietary operating system, no windows, no linux)?
I was looking for some kind of library that could be ported to our OS. I have found basically only Ghostscript, but issue there is with the license, if i understood it correctly, we would have to make our source public, which is not possible for us...
Maybe a little bit more background, we are trying to find format that will be easily viewable by user. We already have our output in PS for other reasons (printer). But now we want to provide this output in file by itself, so we are trying to find feasible file format. We are considering the PS itself, but usual user does not have PS viewer, so that's why I am trying to find something to convert this to PDF. So perhaps alternative question could be, is there some another format that can be easily acquired from PS, such that "regular" user can view it?
The main complexity for converting PostScript to something else comes from the fact, that PostScript is a programming language and PostScrip files in fact are programs executed on the printer.
In contrast to PostScript, PDF is not a programming language. When converting PostScript to PDF (or anything else), you actually have to run the PostScript program and record the graphic primitive calls, executed during the execution of the PostScript program.
This general approach is needed, when you want to convert PostScript programs from any source to PDF.
But you wrote, that you are creating the PostScript code yourself. Perhaps your PostScript program is just a linear sequence of calls to drawing primitives and does not use anything like subroutines or control structures.
If not, it might be easy to change your generator to do those computation at creation time,that currently are performed at print time. You would end up in a linear sequence of calls to drawing primitives.
When there are no more computations done at print-time, it should not be too hard to directly create PDF instead of PostScript. This answer mentions an open source PDF generation library, that uses an MIT style license.
The AGPL licence for Ghostscript would require you to make your source open, yes. However Ghostscript is dual licenced, in addition to the AGPL licence you can purchase a commercial licence, which doesn't require you to open source your own code.
Rather than converting to PDF you can, of course, also simply use Ghostscript to render the PostScript to a bitmap, its usually pretty easy to wrap a viewer around that.
I should point out that there are other companies offering commercial licences for PostScript interpreters which are capable of creating PDF files and/or rendering PostScript. Adobe is the obvious one, there's also Global Graphics.
These days there are not many players left in the field, if you want to handle PostScript, and the AGPL or similar licences won't suit you, then you will need to go commercial.

Can we load, display and manipulate image's matrix without using any library in c++?

is it possible to do changes to image's matrix without using any library in c++? to load and display image as well?
Sure. Grab a copy of the specification for whatever image format you're interested and write the read/write functions yourself.
Note that to write display functionality without an external library you'll likely need to run your code in kernel mode to get to the frame buffer memory, but that can certainly be done.
Not that you'd necessarily want to do it that way...
Like any typical file, an image file is simply made up of bytes; there is nothing special about an image file.
In my opinion, the most difficult part of reading/writing image files without the use of a library is understanding the file format. Once you understand the format, all you need to do is define appropriate data structures and read the image data into them (for more advanced formats you may have to do some extra work e.g. decompression).
The simplest image format to work with would have to be PPM. It's a pretty bad format but it's nice and easy to read in and write back to a file.
http://netpbm.sourceforge.net/doc/ppm.html
Apart from that, bitmaps are also pretty simple to work with. Like Drew said, just download a copy of the specification and work from there.
As for displaying images, I think you're best off using a library or framework unless you want to see how it's done for the sake of learning.

Any good postscript drawing libraries?

I need to draw some pictures for my LaTeX documents, and I've found that hand-made PostScript seems to be a good fit (I want to do stuff programatically, need math functions, etc.). I've also tried TikZ but that just seemed overcomplicated and hard to use.
However, using plain standard PostScript is a bit painful since there aren't really any standard functions for drawing shapes (e.g. not even rectangles).
Is there any PostScript library that would include functions for common shapes and make life a bit easier? Seems to me this problem should be fairly common.
Or should I skip PostScript and move on to some superior system? Which one?
A few people and many PostScript drivers define their own set of procedures for drawing shapes. A PostScript driver may output the following shortcuts:
/bd{bind def} bind def
/cp{closepath}bd
/gs{gsave}bd
/gr{grestore}bd
/m{moveto}bd
/rm{rmoveto}bd
/l{lineto}bd
/rl(rlineto}bd
/s{stroke}bd
/f{fill}bd
/sf{gs s gr f}bd
/xx{exch}bd
/rect {4 2 roll m 1 index 0 rl 0 xx rl neg 0 rl cp} bd
Then, a rectangle would be drawn like this:
0 0 100 100 rect sf
The cumbersomeness of this does make PostScript particularly hard to deal with. MetaPost may be a better fit if you your drawings are programmatically/mathematically generated. MetaPost generates encapsulated PostScript (which you can include in your LaTeX document) but it is more suitable for drawing images with algebraic definitions.
I like using matplotlib. It can generate both postscript and PDF directly, it's in python, and it can also do pretty sophisticated plots (hence its name). If you want to hack PostScript directly you'll be able to use psticks in LaTeX, but you'll need to run-trip everything through dvi2ps and then ps2pdf to make PDFs. Do you really want PostScript or PDFs? I think that you want PDFs, right?
OK, I've decided that Asymptote is the best thing since sliced bread. Handles drawing both graphs and arbitrary figures really well, and has a vast number of extension modules (including MetaPost compatibility if you care about that). Additionally it typesets text using LaTeX which is just incredibly cool. As an added bonus it even outputs directly to PDF (or EPS).
I still think it's a bit sad there's no good libraries of routines for good ol' PostScript though.
I have used Asymptote (for graphs though) but I found it tiresome to learn yet another custom language. If you're familiar with Python, you can give PyX a try. Its feature set is similar to that of Asymptote. For example, it can also use LaTeX for typesetting text/math.
Another option is Enthought Enable, but that is probably less suited.
I've had good results constructing images directly in postscript. One helpful convention I've found is to treat objects like glyphs in a font. So each object expects the currentpoint to be set at, say, the bottom left corner, and leaves the currentpoint at the bottom right. The you can put them in an array and forall through it: each object leaves the currentpoint ready for the next one.
Generate SVG, then use something like iText and/or Inkscape to programmatically convert to PDF/PS. I built a publishing stack this way and it worked out really nice.
There are lots of postscript libraries
look here
http://www.ericlindsay.com/computer/printing.htm
and here
http://www.tinaja.com/post01.shtml
and here
http://seit.unsw.adfa.edu.au/staff/sites/gfreeman/qs.html

How do I write a Perl script to filter out digital pictures that have been doctored?

Last night before going to bed, I browsed through the Scalar Data section of Learning Perl again and came across the following sentence:
the ability to have any character in a string means you can create, scan, and manipulate raw binary data as strings.
An idea immediately hit me that I could actually let Perl scan the pictures that I have stored on my hard disk to check if they contain the string Adobe. It seems by doing so, I can tell which of them have been photoshopped. So I tried to implement the idea and came up with the following code:
#!perl
use autodie;
use strict;
use warnings;
{
local $/="\n\n";
my $dir = 'f:/TestPix/';
my #pix = glob "$dir/*";
foreach my $file (#pix) {
open my $pic,'<', "$file";
while(<$pic>) {
if (/Adobe/) {
print "$file\n";
}
}
}
}
Excitingly, the code seems to be really working and it does the job of filtering out the pictures that have been photoshopped. But problem is many pictures are edited by other utilities. I think I'm kind of stuck there. Do we have some simple but universal method to tell if a digital picture has been edited or not, something like
if (!= /the origianl format/) {...}
Or do we simply have to add more conditions? like
if (/Adobe/|/ACDSee/|/some other picture editors/)
Any ideas on this? Or am I oversimplifying due to my miserably limited programming knowledge?
Thanks, as always, for any guidance.
Your best bet in Perl is probably ExifTool. This gives you access to whatever non-image information is embedded into the image. However, as other people said, it's possible to strip this information out, of course.
I'm not going to say there is absolutely no way to detect alterations in an image, but the problem is extremely difficult.
The only person I know of who claims to have an answer is Dr. Neal Krawetz, who claims that digitally altered parts of an image will have different compression error rates from the original portions. He claims that re-saving a JPEG at different quality levels will highlight these differences.
I have not found this to be the case, in my investigations, but perhaps you might have better results.
No. There is no functional distinction between a perfectly edited image, and one which was the way it is from the start - it's all just a bag of pixels in the end, after all, and any other metadata you can remove or forge all you want.
The name of the graphics program used to edit the image is not part of the image data itself but of something called meta data - which may be stored in the image file but, as others have noted, is neither required (so some programs may not store it, some may allow you an option of not storing it) nor reliable - if you forged an image, you might have forged the meta data as well.
So the answer to your question is "no, there's no way to universally tell if the pic was edited or not, although some image editing software may write its signature into the image file and it'll be left there by carelessness of the editing person.
If you're inclined to learn more about image processing in Perl, you could take a look at some of the excellent modules CPAN has to offer:
Image::Magick - read, manipulate and write of a large number of image file formats
GD - create colour drawings using a large number of graphics primitives, and emit the drawings in various formats.
GD::Graph - create charts
GD::Graph3d - create 3D Graphs with GD and GD::Graph
However, there are other utilities available for identifying various image formats. It's more of a question for Super User, but for various unix distros you can use file to identify many different types of files, and for MacOSX, Graphic Converter has never let me down. (It was even able to open the bizarre multi-file X-ray of my cat's shattered pelvis that I got on a disc from the vet.)
How would you know what the original format was? I'm pretty sure there's no guaranteed way to tell if an image has been modified.
I can just open the file (with my favourite programming language and filesystem API) and just write whatever I want into that file willy-nilly. As long as I don't screw something up with the file format, you'd never know it happened.
Heck, I could print the image out and then scan it back in; how would you tell it from an original?
As other's have stated, there is no way to know if the image was doctored. I'm guessing what you basically want to know is the difference between a realistic photograph and one that has been enhanced or modified.
There's always the option of running some extremely complex image recognition algorithm that would analyze every pixel in your image and do some very complicated stuff to determine if the image was doctored or not. This solution would probably involve AI which would examine millions of photos that are both doctored and those that are not and learn from them. However, this is more of a theoretical solution and isn't very practical... you would probably only see it in movies. It would be extremely complex to develop and probably take years. And even if you did get something like this to work, it probably still wouldn't be 100% correct all the time. I'm guessing AI technology still isn't at that level and could take a while until it is.
A not-commonly-known feature of exiftool allows you to recognize the originating software through an analysis of the JPEG quantization tables (not relying on image metadata). It recognizes tables written by many applications. Note that some cameras may use the same quantization tables as some applications, so this isn't a 100% solution, but it is worth looking into. Here is an example of exiftool run on two images, the first was edited by photoshop.
> exiftool -jpegdigest a.jpg b.jpg
======== a.jpg
JPEG Digest : Adobe Photoshop, Quality 10
======== b.jpg
JPEG Digest : Canon EOS 30D/40D/50D/300D, Normal
2 image files read
This will work even if the metadata has been removed.
There is existing software out there which uses various techniques (compression artifacting, comparison to signature profiles in a database of cameras, etc.) to analyze the actual image data for evidence of alteration. If you have access to such software and the software available to you provides an API for external access to these analysis functions, then there's a decent chance that a Perl module exists which will interface with that API and, if no such module exists, it could probably be created rather quickly.
In theory, it would also be possible to implement the image analysis code directly in native Perl, but I'm not aware of anyone having done so and I expect that you'd be better off writing something that low-level and processor-intensive in a fully-compiled language (e.g., C/C++) rather than in Perl.
http://www.impulseadventure.com/photo/jpeg-snoop.html
is a tool that does the job almost good
If there has been any cloning , there is a variation in the pixel density..or concentration which sometimes shows up.. upon manual inspection
a Photoshop cloned area will have even pixel density(my meaning is variation of Pixels wrt a scanned image)

Decode JPEG to obtain uncompressed data

I want to decode JPEG files and obtain uncompressed decoded output in BMP/RGB format.I am using GNU/Linux, and C/C++.
I had a look at libjpeg, but there seemed not to be any good documentation available.
So my questions are:
Where is documentation on libjpeg?
Can you suggest other C-based jpeg-decompression libraries?
The documentation for libjpeg comes with the source-code. Since you haven't found it yet:
Download the source-code archive and open the file libjpeg.doc. It's a plain ASCII file, not a word document, so better open it in notepad or another ASCII editor.
There are some other .doc files as well. Most of them aren't that interesting though.
Unfortunately I cannot recommend any other library besides libjpeg. I tried a couple of alternatives, but Libjpeg always won. Is pretty easy to work with once you have the basics done. Also it's the most complete and most stable jpeg library out there.
MagickWand is the C API for ImageMagick:
http://imagemagick.org/script/magick-wand.php
I have not used it, but the documentation looks quite extensive.
You should check out Qt's QImage. It has a pretty easy interface that makes this task really easy. Setup is pretty simple for every platform.
If Qt is overkill, you can try Magick++ http://www.imagemagick.org/Magick++/. It supports similar operations and is also well suited for that sort of task. The last time I used it, I struggled a bit with dependencies for it on Windows, but don't recall much trouble on Linux.
For Magick++'s Image class, the function you probably want is getConstPixels.
I have code that you can copy ( or just use as a reference ) for loading a jpeg image using the libjpeg library.
You can browse the code here: http://code.google.com/p/kgui/source/browse/trunk/kguiimage.cpp
Just look for the function LoadJPGImage.
The code is setup to handle c++ binding of my DataHandle class to it for loading the image, that way the image can be a file or data already in memory or whatever.
A slightly out of the box solution is to acquire a copy of the netpbm tools, which transform images from pretty much any format to any other format via one of several very simple intermediate formats. They work well from the shell, and are most often used in pipes to read some arbitrary image, perform an operation on it, and write it out to some other format.
The pbm formats can be as simple as a plain ASCII header followed by the RGB data in ASCII or binary. They are intended to be simple enough to use without required a library to implement.
JPEG is supported in netpbm by read and write filters that are implemented on top of libjpeg.