Load a PNG image from a USB stick with stb_image - c

I'm asking here because the documentation concerning the aforementioned library is horribly lacking. As written in the title, I want to load a PNG image from a USB stick and then decode it using stb_image but I don't know how to go about it.
To add a bit of context, I'm using a discovery board from ST, the STM32F769I and running FreeRTOS.
I thought first that simply calling "stbi_load" with the full path ("0:MYIMAGE.PNG") would suffice but it doesn't.
Therefore, I suppose another function must be used for that purpose but which one ? stbi_load_from_memory ? I don't have it very clear. Or should I first call a FatFS function like f_open() before being able to call any decoding function?
Any help is more than appreciated. It'd be very helpful if you could write here a simple example that does the job.
Thanks in advance.

Related

How to store data from a .TIFF file to then modify it with my C program

I know few about this and i'm trying to keep building upon it. My goal is to do image stacking with some criteria using C language, as i came upon some cool ideas i think i should be capable of doing with my photos. My C background should be enough to understand what i may need. That being said...
So far i've learned how to read an existing .TIFF file and save it into a char array. The problem is i don't know in which way its data is contained so that i can then be able to analize individual pixels and modify them, or build another .TIFF file from data i previously read.
I've read some things about (a so called) libtiff.h which may be usefull but i can't find where to get it, neither how to install it.
Does anyone know how a .TIFF file data is stored so that i can read it and apply changes to it?
Also,
Does anyone have any experience with handling image files and editing in C? Where did you learn it from?
Do you know of any place i could search for information/tutorials?
Any help will be very usefull,
Thanks in advance.
You can do an enormous amount of very sophisticated processing on TIFFs, or any one of 190+ other formats with ImageMagick without any need to understand TIFF format or write any C. Try searching on Stack Overflow for [imagemagick]
If you want to do processing yourself, consider https://cimg.eu
Another option might be to convert your TIFFs to NetPBM which is much, much simpler to read and write in C. That would be as follows with ImageMagick:
magick INPUT.TIFF -compress none OUTPUT.PPM

How to retrieve graphic adapter & monitor name that is attached to a Xorg screen, programmatically?

Obviously, this information is available in xorg.conf so I could try to parse this file. But is there a way to achieve this using Xlib calls (+ extensions) only?
Thanks,
PMJ
It must be possible, because I know the program xdpyinfo can do it. At first, I was going to suggest executing that from within your program and parsing the output. That shouldn't be necessary, though, since the source of xdpyinfo is freely available.
It looks like if you have a (Display*) variable (and you will, because pretty much every X11 function call requires one), you can call these wonderful macros to get interesting data, including ServerVendor and VendorRelease. That should cover the "graphic adapter" portion of your quest.
As for the monitor name, according to xdpyinfo.c, this is governed by XF86VidModeGetMonitor() which is part of an X11 extension. This returns a XF86VidModeMonitor structure which will reveal vendor, model, and other juicy data.
Run xdpyinfo-- if that program can query the data, so can your program.

How to display an image using c?

I want to display an image using c in linux platform.And now i have read the image file ( gif)into an array of char. But I don't know what to do next to display it? Can anybody help me?
This is a quite complicated question since you're not being very specific.
C in itself doesn't have a concept of an "image" or even a "display". You need to mix in some environment-specific details that add such concepts, for instance you could work with the Framebuffer in (console) Linux, the X11 window environment, GNOME/GTK+, KDE or perhaps Enlightenment.
All of these give radically different answers.
You need a graphic toolkit to do that. Plain old C language is not enough.
You can try these toolkits:
QT
GTK
There are many others that will also do what you want, but these two are among the most populars I guess.

Display pixel on screen in C

How would I change a pixel on a display, in C?
Assume NOTHING: I am using a linux machine from console to do this. I do not want to use GUI toolkits or frameworks to draw the pixel. I do not want to draw the pixel in a window. I want to draw the pixel directly to the screen.
EDIT: I have a screen. I'm on a laptop running linux from console. I'd prefer a solution not using X as I'd rather learn how X works than how to use X.
If theres more information, ask, but don't assume. I'm not trying to build a GUI, and that was the main purpose of blocking assumptions as I don't want people to assume I'm doing things the long way when in reality I'm just tinkering.
EDIT 2: You may use any X11 related libraries provided that you can explain how they work.
If we really assume nothing, can we even assume that X is running? For that matter, can we even assume that there is a video card? Perhaps Linux is running headless and we're accessing it over a serial console.
If we are allowed to assume a few things, let's assume that Linux has booted with framebuffer support. (It's been a couple years since I worked with Linux framebuffers, I may get some of the details wrong.) There will be a device created, probably /dev/fb or /dev/fb0. Open that file and start writing RGB values at an offset, and the screen will change, pretty much regardless of anything: text console, graphical console, full-fledged desktop envrionment, etc. If you want to see if framebuffer support is working, do dd if=/dev/zero of=/dev/fb on the command line, and the display should go all black.
C doesnt have any graphics capabilities - you'd need to use a third party library for this.
You cannot assume a display in C. There is literally no way to do what you ask.
Edit: Okay, you have a display, but again, there's not a whole lot you can get from there. The point is that there are a TON of competing standards for graphics displays, and while some of them (VGA interfaces, for example) are standardized, a lot of the others (display driver interfaces, for example) are NOT. Much of what X (and other display device drivers, such as Windows or the like) do, is have specific interface code for how to talk to the display drivers; they abstract out the complexity of dealing with the display drivers. The windowing systems, though, have HUGE libraries of complicated and specific code for dealing with the display drivers; the fact that these things are relatively transparent is an indication of just how much work they've put into these things over time.
Very primitive and making a lot of assumptions:
fd = open("/dev/fb0", O_RDWR);
lseek(fd, 640*y+x, SEEK_SET);
write(fd, "\377\377\377\377", 4);
In reality, you would use mmap rather than write, and use the appropriate ioctl to query the screen mode rather than assuming 640xHHH 32bpp. There are also endian issues, etc.
So in real reality, you might use some sort of library code that handles this kind of thing for you.
I suppose you could paint to the terminal program that you are using as your console. All you have to do is figure out which one that is and look it up.
Whoops I assumed a terminal. :P
I think what you are looking for is information on how to write to the frame buffer. The easiest way would be to use SDL and render to the frame buffer, or else use GTK+ with DirectFB, although that goes against your edict on not using toolkits or frameworks.

C code for loading bitmap

Does anybody know a good C sample that loads bitmaps and handles all the cases: rle, b/w bitmaps, so on?
Code should be cross-platform.
Thanks.
I would suggest using a library like SDL image
If you are looking for a minimal bmp loader this link will give you all you need to know about the BMP format, data structures and sample code without any library dependency to load:
http://paulbourke.net/dataformats/bmp/.
It also contains code to see the loaded BMP in a open gl texture, so pretty much all you need...
Chris Backhouse made a functional little BMP loader (with an eye to using them as OpenGL textures). It's C++, not C, and he admits it's not cross platform. However, it's small and easy to understand, so I thought I'd add the link here:
http://users.ox.ac.uk/~orie1330/bmploader.html
You need some external library to do this (I recommend ImageMagick). The ImageMagick web site also includes documentation and examples.
Check out for OpenCV Library developed by Intel .
If you are tied to the BMP file format, it's pretty simple to look at the header yourself and get the pixels. See this google search. One of the more interesting matches is here. The most counter-intuitive part is that every line of pixels is 4-byte aligned. Also, watch out for compressed BMPs... (My experience is that many third-party tools have trouble with compressed BMPs, so maybe some libraries you encounter will also..)
If you aren't tied to the BMP file format, I recommend libpng. The manual provides some sample code which is pretty clear.
As others suggested you might want to use an external library like SDL. If you want to learn something and do it yourself, see my answer to this very similar question: Getting RGB values for each pixel from a 24bpp Bitmap for conversion to GBA format in C where you'll find C code which prints out each pixel, and have a look at the wikipedia page about bmp files, because it's very good.

Resources