Image output in C - 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**

Related

Displaying Text with SDL_ttf without font file hard code

I have code in C LANGUAGE to display text on screen using SDL_ttf library in linux.
TTF_Font *font;
font = TTF_OpenFont("FreeSans.ttf", 20);
But in this I don't want to give file name as hard coded or file path. so when I execute this programme on other Linux machine it should run fine. I don't want to keep always this font file with my executable.
Is there any way to write portable code for TTF_OpenFont ?
or i can use something else that can solve my problem ?
To avoid hardcoding fonts you need to integrate SFL_TTF with fontconfig.
Or use fontconfig through a higher-level framework like pango http://sdlpango.sourceforge.net/

How to avoid having to reference an external XML file?

I am writing an app in C and using Glade3 to make the GUI (and GTK builder).
Glade 3 doesn't allow C code generation anymore (which I understand the reasons for), but now all applications need to have the XML file hanging around the compiled app like an annoying .dll file. Does anyone have any knowledge of project to create C code for it, or how to embed the XML file in the code itself? I imagine this could be done, but it would be a pain I bet.
What you probably want is:
gtk_builder_new_from_string(const gchar *string, gssize length);
https://developer.gnome.org/gtk3/3.10/GtkBuilder.html#gtk-builder-new-from-string
Just stringify the glade3 xml file (in GtkBuilder format) and include it as a string.
It is still possible to use glade-2.12 to generate gtk2 code and then port it to gtk3; however if you are going to do that it may be worth it to simply modify that version of glade to output the gtk3 syntax directly.

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

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.

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!)

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