Save image with ocaml graphics - graphics

i want to save the picture generated by ocaml graphics in a file (png or jpeg).
Thank you.

I'm going to assume that you are talking about the Graphics module in ocaml.
You should notice that the Graphics module isn't for creating and processing images. You can of course call Graphics.dump_image if you've already gone ahead and wrote something that produces the Graphics.image type. This will produce a color array array, where color is a packed integer in the format, 0xRRGGBB.
After some other conversion functions you should call upon camlimages to produce your image, and there is a code sample from another question.

Related

How does paraview read a vtk file?

I have a general question as to how paraview reads a ASCII UNSTRUCTURED_GRID.
Does it sort through the cells and then points? or points then cells?
Paraview is built on the VTK library and both of them are open source, so if you are a programmer, you should be able to get all your answers by reading the code. What I assume (but I can be wrong, check sources of paraview to be sure) paraview uses for reading ASCII unstructured grids is vtkUnstructuredGridReader, source code of which you can find here: https://github.com/Kitware/VTK/blob/master/IO/Legacy/vtkUnstructuredGridReader.cxx (and here is the parent class that implements a lot of the functionality the reader uses https://github.com/Kitware/VTK/blob/master/IO/Legacy/vtkDataReader.cxx). Look at the RequestData method, the variable "output" is the outputted unstructured data.
To briefly answer your initial question, it looks like points are read before cells.

How can I show an image from raw pixel data in GTK using Haskell?

I would like to create an image in Haskell using the Rasterific library and then display that image in a GTK window; the Rasterific library lets me generate an RBGA-formatted 32-bit pixel depth image, but I am having trouble figuring out how I can take this raw image and display it in a window or drawarea or whatever in GTK.
(I've spent a lot of time looking through the documentation, but I've been having a hard time seeing how to fit the parts together, especially since the Haskell documentation is often non-existent and at some point the cairo library gets involved in a way that's not entirely clear to me.)
I wrote a package called AC-EasyRaster-GTK for this exact purpose.
It's a wrapper around gtk2hs. That library gives all the necessary parts, but it's not actually all that easy to figure out. So I wrote a library so I wouldn't have to keep looking this stuff up!
ib_new gives you a new image buffer, ib_write_pixel lets you write a pixel, and ib_display will start the GTK event loop, display the bitmap in a window, and block the calling thread until the user clicks close. Sadly, there's no easy way to chuck an entire array at GTK. (It demands a particular pixel order, which varies by platform...)
I'm sure there's a better way to do this, but I'm not finding it either. You can iterate over all the pixels in the original image using something like forM_ (range ((0,0),(w,h))) and draw them onto a Cairo drawing using something like this: (The Cairo calls are correct but I'm just guessing about the Rasterific functions)
drawPixel color x y = do
setSourceRGBA (red color) (green color) (blue color) (alpha color)
rectangle x y 1 1
paint

How can I create a 3D model file from geometric shapes?

I am writing a program that will output 3D model files based on simple geometric shapes (e. g. rectangular prisms & cylinders) with known coordinates in 3-dimensional space. As an example, imagine creating a 3D model of stonehenge. this question suggests that OBJ files are the easiest to generate, but I'm struggling to find a good tutorial or easy-to-use library for doing so.
Can anyone either
(1) describe step-by-step how to create a simple file OR
(2) point me to a tutorial that describes how to do so
Notes:
* Using a GUI-based program to draw such files is not an option for me
* I have no prior experience with 3D modeling
* Other formats such as WRL or DAE would work for me as well
EDIT:
I do not need to use textures, just combinations of simple geometric shapes positioned in 3D space.
I strongly recommend to use some ASCII exchange format there are many out there I usually use these:
*.x DirectX object (it is a C++ source code)
this one is easiest to implement !!! But there are not many tools that can handle them. If you do not want to spend too much time coding then this is the right choice. Just copy the templates (at the start) from any *.x file to get started.
here some specs
*.iges common and importable on most CAD/CAM platform (Catia included)
this one is a bit complicated but for export purposes it is not that bad. It supports Volume operation like +,-,&,^ which are VERY HARD to implement properly but you do not have to use them :)
*.dxf AutoCAD exchange format
this one is even more complicated then IGES. I do not recommend to use it
*.ac AC3D
I first saw this one in flight gear.
here some specs
at first look it is quite easy but the sub-object implementation is really tricky. Unless you use it you should be fine.
This approach is easily verifiable in note pad or by loading to some 3D model viewer. Chose one that is most suitable for your needs and code save/load function to your Apps internal model class/struct. This way you will be compatible with other software and eliminate incompatibility problems which are native to creating 'almost known' binary formats like 3ds,...
In your case I would use IGES (Initial Graphics Exchange Specification)
For export you do not need to implement all just few basic shapes so it would not be too difficult. I code importers which are much much more complicated. Mine IGES loader class is about 30KB of C++ source code look here for more info
You did not provide any info about your 3D mesh model structure and capabilities
like what primitives you use, are your object simple or in skeleton hierarchy, are you using textures, and more ... so it is impossible to answer
Anyway export often looks like this:
create header and structure of target file format
if the format has any directory structure fill it and write it (IGES)
for sub-objects do not forget to add transformation matrices ...
write the chunks you need (points list, faces list, normals, ...)
With ASCII formats you can do this inside String variable so you can easily insert into or modify. Do all thing in memory and write the whole thing to file at the end which is fast and also add capability to work with memory instead of files. This is handy if you want to pack many files to single package file like *.pak or send/receive files through IPC or LAN ...
[Edit1] more about IGES
fileformat specs
I learned IGES from this pdf ... Have no clue where from I got it but this was first valid link I found in google today. I am sure there is some non registration link out there too. It is about 13.7 MB and original name IGES5-3_forDownload.pdf.
win32 viewer
this is free IGES viewer. I do not like the interface and handling but it works. It is necessary to have functional viewer for testing yours ...
examples
here are many tutorial files for many entities there are 3 sub-links (igs,peek,gif) where you can see example file in more ways for better understanding.
exporting to IGES
you did not provide any info about your 3D mesh internal structure so I can not help with export. There are many ways to export the same way so pick one that is closest to your App 3D mesh representation. For example you can use:
point cloud
rotation surfaces
rectangle (QUAD) surfaces
border lines representation (non solid)
trim surface and many more ...

Retrieve the pixel values of an image with Haskell

Is there a way or a library available that can load an image (jpeg, png, etc) and assign the pixel values of that image into a list or matrix? I'd like to do some experiments with image and pattern recognition.
A little nudge in the right direction would be appreciated.
You can use JuicyPixels, a native Haskell library for image loading. This is rather easy to convert to REPA as well (manually or with JuicyPixesl-repa).
I've used the repa-devil package for this in the past. It lets you work with a bunch of formats using Developer's Image Library (DevIL). You can read and write all the formats you are likely to care about.
The actual image data is given as a Repa array. This is a great library for array operations and makes it very easy to write parallel code.
Try the repa library
.Also there is a small tutorial here
Here is a new Haskell Image Processing library, which uses JuicyPixels for encoding, provides interface for you to read and write all of the supported formats in a very easy manner and manipulate them in any way you can imagine. Just as a simple example on how easy it is:
>>> img <- readImageRGB "image.jpg"
>>> writeImage "image90.png" $ rotate90 img
Above will read a JPG image in RGB color space, rotate it 90 degrees clockwise and save it as a PNG image.
Oh yeah, it also can use Repa, so you will get parallel processing for free as well.
GTK supports loading and saving JPEG and PNG. [AFAIK, no other formats though.] There is a Haskell binding named Gtk2hs. It supports vector graphics very well, but bitmap graphics, while supported, isn't especially easy to figure out. So I wrote AC-EasyRaster-GTK, which wraps GTK in a more friendly interface. (It still needs Gtk2hs though.) The only real down-side is that Gtk2h is a bit fiddly to set up on Windows. (And it's arguably overkill to install an entire GUI toolkit just to load and save image files.)
I gather the "GD" library supports writing several image formats, and is quite small and simple. I believe Hackage has Haskell bindings for GD as well. I haven't tried this personally.
There is a file format called PPM which is deliberately designed to be ridiculously easy to implement (it's a tiny header and then an array of pixels), and consequently there's at least a dozen packages on Hackage which implement it (including my own AC-PPM). There are also lots of programs out there which can display and/or convert images in this format.

How to serve an image of the screen as an input to a program, in Haskell

I'd like to write a function (I'm using Haskell) that's capable of retrieving the actual content of the screen, as viewed by the user, in order to pre-elaborate (grayscaling and/or some stencil convolution through the repa package) and feed it to a neural network or some other AI architecture.
The type would be something like watch :: IO img
Is there a library that provides functions like this? And if I needed not the entire screen but only the visual output of a program, is there other way than hand-cutting the image?
Grabbing the screen is platform dependant. And you'll need to find a C library that does this, then write a Haskell binding to that low level functionality. On some systems you may be able to open the frame buffer as a file as grab the image.
So, the answer is the same as for C, but you write a binding from Haskell to that function.
Maybe you can have a look at the source code of scrot

Resources