preload
Sep 20

Most of technical people are trying to migrate from windows to Linux now a days , but they are facing lot of problems with Linux . they do not even know the forums and blogs of Linux which would be useful for finding answer to their query. The main problem of open source is choosing right one for user need. Because so many open source softwares are available for a single thing. Surely user need some supportive forum or website to find that one . open Peta is also doing that job. The idea of this  post coming to my mind from my college friend . One of my colleague asked me about computer graphics program in Linux using C programming language. the questions is

How to draw line, circle , or 2D graphics in Linux Using C or C++?

I was searching Internet and found some softwares for developing graphics applications in Linux. that softwares are listed here

  1. GTK+   – Gnome Tool Kit
  2. QT  -  The X toolkit
  3. SVGALIB   [ #include<vgagl.h> ]
  4. libgraph [ #include<graphics.h> ]

GTK and QT are simple and used for high level Graphical User Interface [ GUI ] development. SVGALIB and libgraph is used for 2D graphics in Linux .  the syntax and functions are some what different for beginners  [specially the user from windows ] but most of users familiar with windows graphics.h header file in C and C++ , so we can move for libgraph which is exact one for windows graphics user . libgraph is an implementation of the Turbo C graphics API (graphics.h) on GNU/Linux using SDL [ Simple Direct Media Player ]. The library requires SDL for primitive graphics and SDL. First you need to install the following dependency packages using synaptic package manager in Ubuntu Linux to develop the graphics applications properly.

  • SDL Library
Installing SDL Library

Installing SDL Library

  • GUILE – GNU’s Ubiquitous Intelligent Language for Extension

GUILE Installation

GUILE Installation

  • After installing these two packages , download the libgraph and install in your system

#sudo su

#tar -xzvf libgraph-1.0.2.tar.gz

#cd libgraph-1.0.2

#make

# make install

For better understanding , watch this video given here ….


Developing Graphics Application

1. Open your favorite text editor and type the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdio.h"
#include "graphics.h"
int main()
{
int gd=DETECT, gm=VGAMAX;
initgraph(&amp;gd,&amp;gm, 0);
moveto(0, 0);
setcolor(4);
rectangle(50,50,500,200);
while(!kbhit());
closegraph();
return 0;
}

2. Now compile the program with lgraph library

#gcc line.c -lgraph -o output

#./output

If you get any error in this line like

./output: error while loading shared libraries: libgraph.so.1: cannot open shared object file: No such file or directory

Do the following

sudo cp /usr/local/lib/libgraph.* /usr/lib

3. Now one new window will open , there you will get the output Rectangle in Red color.

Output Window

Output Window

The advantage of libgraph is very easy to remember the syntax  [ all functions are same as turbo c graphics.h ] . still if you are facing any problem to develop graphics applications using this , send your problems through comment page of this post.

Mar 24
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/*
* Auhtor : Anthoniraj.A
* Date : 24/03/2009
*/
int** imageRead(char imageName[]);
void imageWrite(int** pels);
int width, height;
int **pixels;
unsigned char *charImage;
 
int** imageRead(char imageName[]) {
FILE* fp;
 
//PGM Headers Variable Declaration
char* type;
int *ptr;
int q, i, j;
 
char header[100];
//Open file for Reading in Binary Mode
//fp = fopen("/home/anthoniraj/image.pgm","rb");
fp = fopen(imageName, "rb");
if (fp == NULL) {
printf("Image does not exist \n");
} else {
//Check the PGM file Type P2 or P5
fgets(header, 100, fp);
if ((header[0] != 80) || (header[1] != 53)) {
printf("Image is not PGM\n");
 
}
 
//Check the Commnets
fgets(header, 100, fp);
while (header[0] == '#') {
//printf("%c\n", header[0]);
fgets(header, 100, fp);
}
 
//Get Width and Height
width = strtol(header, &amp;amp; ptr, 0);
height = atoi(ptr);
 
// Get Maximum Gray Value
fgets(header, 100, fp);
q = strtol(header, &amp;amp; ptr, 0);
 
//Allocating Array Size
charImage = (unsigned char*) malloc(width * height * sizeof (unsigned char));
pixels = (int **) malloc(width * sizeof (int *));
for (i = 0; i &amp; lt; height; i++)
pixels[i] = (int *) malloc(height * sizeof (int));
 
// Pixel Extraction
fread(charImage, 1, (width * height) * sizeof (unsigned char), fp);
for (i = 0; i &amp; lt; height; i++) {
for (j = 0; j &amp; lt; width; j++) {
pixels[i][j] = (int) charImage[i * width + j];
// printf("%d ",pixels[i][j]);
}
//printf("\n");
}
 
// Pixel Extraction
fclose(fp);
}
return pixels;
}
 
void imageWrite(int** pels) {
int i, j;
FILE* fp1;
fp1 = fopen("out.pgm", "w");
 
for (i = 0; i &amp; lt; height; i++) {
for (j = 0; j &amp; lt; width; j++) {
charImage[i * width + j] = pels[i][j];
}
}
fprintf(fp1, "P5\n%d\n%d\n255\n", width, height);
fwrite(charImage, 1, width*height, fp1);
fclose(fp1);
}
 
int main() {
int** p = imageRead("sam1.pgm");
imageWrite(p);
return 0;
}