How do you turn a .png file into a 2-d matrix? - c

I am working on a project for a Bio-medical Imaging course (Note: it is a non-programming course, so asking for programming help is not cheating. Asking for conceptual help with planning would be cheating.) where I need to manipulate an image using different mathematical transforms. I am writing in C so it can be as fast as possible. I have finished the code for the mathematical transforms, but I have realized that I do not know how to turn a grayscale .png file into a 2-d matrix/array to compute with, and I do not know how to display a .png file in C. Can anyone help me?
I'm trying to turn the "image.png" image into a 2d array where each entry in the array has a value between 0 - 255 and corresponds with each pixel in "image.png". I also want to turn a 2d array where each entry corresponds to a pixel in the image and has a value between 0 - 255 into a new "image_two.png" file.
I'm a somewhat new programmer. I have a solid base in python programming, but C is new for me. I have done a lot of research and I have found a lot of people talking about using "this library" or "that library", or also "this library", but how do I use a downloaded library in C? It's unfamiliar territory for me as a python programmer :(
I'm using Ubuntu 12.04
To reiterate:
How do you read a grayscale .png image as a 2-d array/matrix in C?
How do you display a 2-d array/matrix as a grayscale image in C?
How do you use a downloaded library in C code (specifically for the two questions above)? I found out how to use these libraries.
EDIT: I am still having trouble figuring out how to create a grayscale 2d array out of a .png file and how to make a .png file out of a grayscale 2d matrix. Can anyone else help?

You can use a more general purpose image handling library and you might find it easier to use. I recommend FreeImage http://freeimage.sourceforge.net/. See the pixel access section of the manual to get access to the pixel data. You can then work with it directly or copy it into your own matrix.

To install a library in Linux, typically you will use the package manager. For example, in Debian (this includes Ubuntu) you might do:
$ apt-cache search libpng
You'll decide which package to install based on the results of running this command and then you will run
$ sudo apt-get install <package-name>
This command will likely install png.h in a location that is already included in gcc's search path. This means that to use png.h in your program, all you have to do is include it.
#include <png.h>
Skip to chapter 3 in the libpng manual for a walkthrough on reading a png file.

Related

How to make LLDB able to print STL container's content?

I am now able to let my LLDB debugger print vector content, however, the output of unordered_map content is far from satisfaction. The output does not contain any key and value.
Here is a screenshot of what my current output is:
Is there a way to support LLDB debugger to do that?
It looks like you are using the libcpp version of the STL (the one from gcc)? The lldb data formatter coverage for the libcpp is not as complete as the libcxx (clang's STL implementation) data formatters. There is a formatter for libcpp's map, but not unordered_map.
You could file an ER requesting that support be added at http://bugs.llvm.org or have a go at adding the support yourself. Or you can make a formatter for it in Python for your own use. That process is described here (the whole page describes how variable formatting works in lldb, then this is the section on doing it in Python):
https://lldb.llvm.org/use/variable.html#python-scripting
Support was recently added for many STL types from libstdcpp:
https://reviews.llvm.org/D113760
https://reviews.llvm.org/D112537
https://reviews.llvm.org/D112180

Image output in C

Quick question, is there a way to show an image(ex. bmp) from file using C? It's not in graphics.h apparently, and I can't use Allegro because it does not support Borland(or so I've read). I need to use the very old compiler for a school project. I would like to ask if anyone had any experience of doing this using other libraries? If yes, which library was it? Thanks a lot.
I hope you have visual (windows) borland like Borland C++ builder 3++ or turbo C++ not the MS DOS one. in that case it is quite easy because you can use bitmap which is part of VCL so no additional include is needed.
here you can find some hints on rendering under borland
now how to visualize picture from file to your window:
// this will create and load your bitmap
Graphics::TBitmap *bmp=new Graphics::TBitmap;
bmp->LoadFromFile("image.bmp");
bmp->HandleType=bmDIB;
bmp->PixelFormat=pf32bit;
// on paint you can draw your image to form,paintbox,another bitmap or whatever...
Form1->Canvas->Draw(0,0,bmp); // also you can use stretch draw or copy rectangle GDI functions
// before exiting delete the bmp
delete bmp;
[Notes]
You can also save image by bmp->SaveToFile("out.bmp"); In case you need jpg then add:
#include <jpeg.hpp>
TJPEGImage *jpg=new TJPEGImage;
jpg->LoadFromFile("image.jpg");
bmp->Assign(jpg);
delete jpg;
this will load jpg to your bmp also you can save jpg as well in the same way. Beware older Borlands has a bug in TJPEGImage and will crash if the jpg resolution is too big**

draw call graph for C source codes using cppDepend

I have a problem with using cppDepend tool. I have a source code which is written in C language and I need to draw Its dependency or call graph. cppDepend's compiler's source code extension is set to c;cpp;cxx;cc but when I want to open source codes in C, the file chooser box only let me to choose source codes in C++.
What should I do????
In Linux? Why don't you use other options? Hast to be cppDepend? Look at cflow, callgrind (my favorite), or even gperf (which outputs a callgraph in graph form!)

Converting a .fig file to a .obj file in MATLAB

I have a 3D plot in MATLAB, a .fig file, that I need to export as a .obj file so that I can manipulate it in a graphic design program. The program needs to have the file as x y z coordinates so a .dxf file would also work. Has anyone ever had to do something like this? I admit I'm not that experienced using MATLAB but would appreciate any suggestions.
There is a "saveobjmesh" function available that might do what you need:
http://www.nada.kth.se/~asa/Ray/matlabobj.html
The web page has got examples of how to use it.

Imagettftext like C function

I need to implement a functionality similar to PHP's imagettftext function. I need to enter a text and output a bmp image based on that. I already looked at freetype but it converts character by character and is not suitable to convert the whole text to an image. I am stuck at the moment. How can I access the source code of imagettftext function or is there another library inc to do that?
OK. I solved the question. In GD library there is a function to do (but requires FreeType) that and then you can save the image in PNG format.

Resources