Search

Carving text on image using gimp

We often want our images to have some text carved on it. Here is how we can do it easily using gimp.

Launch gimp and click on File->Create->Logos->Carved

 photo gimp_carved_select.png

The following menu will appear.

 photo gimp_carved_text.png

In the text field enter the text that has to be carved on the image.

Font Size: The size of the font to be engraved. Select this carefully depending on the size of the image.
Font: The fonr to be used for the characters of the text.
Background Image: Browse and select the image on which the text need to be carved.
Carve raised text: We can tick this check box when we want the text to stand out in a raised manner from the image.
Padding around text: How much spacing should be between the image borders and the text.
For our example we will write the word Linux on the image of the "tux", the linux penguin.
After setting all the above fields click on ok.

The gimp screen will come up with the carved text. The default color of the text is almost white as shown below.

The color can be changed by using the bucket tool available in the toolbox and filling the characters with our desired color by selecting the top most layer among all the layers.

 photo gimp_carved_linux.png

After coloring we will have to export the image using File->Export and choose the path and name for the final file. The following is the final result after coloring the text red.

 photo linux_mbed_2.png

Note that the image being small, it has been repeated to accommodate the whole text.

Creating a queue in linux kernel using list functions

In the post we saw how to create a stack using the list functions. In this post let us use the same list functions to implement a queue.

As we know a queue is a data structure that stores data in the first in first out order. That is the data that is entered first is read out first.

To create a queue using the list functions we can make use of the function list_add_tail



new: The new node to be added
head: Pointer to node before which the new node has to be added.

To make the list work as a queue we need keep inserting every new node before the head node, that is at the tail end of the list and when we read the list, we read from the front end. Thus the entry that is added first will be accessed first.

To demonstrate the operation of queue we will create a proc entry called as queue make it work like a queue.

To create the queue we will use the structure



queue_list : Used to create the linked list using the list functions.
data: Will hold the data to be stored.

In the init function we need to first create the proc entry and then intialize the linked list that we will be using for the queue.



create_new_proc_entry: Function for creation of the proc entry.





Next we need to define the file operations for the proc entry. To manipulate the queue we need two operations push and pop.



The function push_queue will be called when data is written into the queue and pop_queue will be called when data is read from the queue.

In push_queue we will use the function list_add_tail to add every new node behind the head.



In the function pop_queue, before attempting to read the queue, we need to ensure that the queue has data. This can be done using the function list_empty.



Which returns true if the list is empty.

In case the list is not empty, we need to pop out the first data from the list which can be done using the function list_first_entry.



ptr: Pointer to the head node of the list.
type: type of structure of which the head is member of
member: The name of the head node in the structre.
Thus the pop function will look as below.

The flag and the new_node are variables used to ensure that only one node is returned on every read of hte proc entry. The read function from user space will continue to read the proc entry as long as the return value is not zero. In the first pass,new_node=0, we will return the number of bytes of data transfered from one node and in the second pass,new_node=0, we will return 0 to terminate the read. Thus making sure that on every read data of exactly one node is returned. The flag variable works in the similar fashion when the queue is empty to return the string "Queue empty".

Thus the full code of queue will be



To compile the module use the make file.



Compile and insert the module into the kernel



To see the output

Thus we have written the 1,2,3,4 as data into 4 nodes with 4 being inserted last. Now to pop data out of the stack

Implementing stack using the list functions in kernel -2

In the post "Implementing stack using the list fuctions in kernel" we saw how to create a stack entry using the in built list functions of the kernel. Some of the operations that we did while creating the stack can further be automated using some more list functions of the kernel.

We had used a variable "tail" to keep track of the last element of the stack, which was being updated in the push_stack function after every write to the stack. This is not really needed is we use the function list_last_entry.



ptr: Pointer to the head node of the list. type: type of structure of which the head is member of member: The name of the head node in the structure.

For our example, we can use list_last_entry as below.

list_last_entry automatically returns the last node of the list and there is no need to keep track of the tail node.

Thus the push function can be modified as below.



In the pop_stack function, the check for stack being empty was being performed by comparing the tail with the head pointer. This can be easily by using the function list_empty, which returns true is the list is empty



head: Pointer to the head node of the list which is being tested.

list_last_entry can also be used in the pop_stack function to read out the last element of the stack.

Thus the modified pop_stack will look as below.



The complete modified code for stack being

stack_list.c



To compile the module use the make file.



Compile and insert the module into the kernel



To see the output



Thus we have written the 1,2,3,4 as data into 4 nodes with 4 being inserted last. Now to pop data out of the stack



Thus we can see that the proc entry is operating as a stack.
Related posts

Implementing stack using the list functions in kernel
Creating a read write proc entry in kernel versions above 3.10
Pointer to structure from its member pointer: container_of

Finding trignometric inverse in gcalctool in linux

Most linux distros come with gcalctool as the default calculator. It is not obvious on first look as how to find the inverse of trignometric terms in gcalctool. Here is how we can do it.

This is the default look of the gcalctool.

 photo gcalc.png

Let us say we want to find arcsin(0.9)

Type sin or press the sin button

 photo gcalc_sin.png

Now press the up arrow, which is the second button from left in the first row and then type -1.

 photo gcalc_inv.png

Now press the up arrow again to come out of superscript mode.

Now enter the value 0.9 and press enter.

 photo gcalc_sin_inv.png

And we have the angle in degrees for arcsin(0.9)

 photo gcalc_angle.png

Implementing stack using the list functions in kernel

In the posts "Creating a linked list in linux kernel" and "Using list_del to delete a node" saw how to use the in built functions of a kernel to build a linked list. One of the main applications of linked list could be the implementation of stack. In this post, let us make use of the list functions available in the kernel to implement a stack.

A stack, as we know, is a data structure that stores elements in the order of first in last out. So when ever we write or push data into the stack, the data needs to be added as a node at the end of the list and when we read data from the stack we need to read data from the last node of the list and then delete the corresponding node. To implement a stack using a list, we need to keep track of the last element that gets inserted into the list, because when ever we need to read from the stack we need to return the data stored in the last node.

To implement the stack operation using list functions, we will make use of the function



Where
new: The new node to be added to the list
head: Is the node after which the new node has to be added.
If we have to implement a linked list from scratch we would have had to take care of all the pointer manipulations when inserting data into the list or removing data from the list. But as we have the required help, as built in functions, all we need to do is call the right function with the right arguments.

To show the operation of a stack we will make use of a proc entry. We will create proc entry named as "stack" and make it behave as a stack. We will use the following structure as the node



test_list: Will be used to create the linked list using the built in functions of kernel
data: To hold the data of the list.

The first thing we will have to do, to implement a stack using proc entry is create a linked list and create a proc entry in the init function.



create_new_proc_entry : Function to create a new proc entry.



INIT_LIST_HEAD: Initialize the linked list with the head node as test_head.

tail=&test_head; : With no data in the stack the tail and the head point to the same node.

emp_len=strlen(empty): "empty" is the message to the printed when the stack is empty and strlen returns the length of the string.

Once the initialization is done we need to associate the functions for push, writing data to stack and pop, reading data from stack.



Now we need to implement the functions pop_stack and push and stack.

push_stack:



msg=kmalloc(10*sizeof(char),GFP_KERNEL): allocate memory for the data to be received from the user space.
temp=copy_from_user(msg,buf,count): copy the data from user space into the memory allocated
node=kmalloc(sizeof(struct k_list *),GFP_KERNEL): Allocate a new node.
node->data=msg: Assign the data received to the node.
list_add(&node->test_list,tail): Add the new node to the list. Note that we are passing the node after which the new node has to be added as tail. This makes sure that the list behaves as a stack.
cnt++: Increase the count of nodes that are present in the list.
tail=&(node->test_list): Change the node to point to the new node of the list. Note that we are assigning the address of test_list to tail and not the address of the node.

pop_stack:



In the pop operation we pop the last element inserted into the stack and then delete the node. According to the push_stack function, the variable tail points to the last element inserted into the stack.

According the standard practice followed in the the kernel, the read operation has to return the number of bytes of data transfered to the user space and user space functions continue to read as long as they do not get a 0 as return value indicating end of the read operation.

To make the proc entry work as a stack, we need to make sure that every read operation return only one node. To ensure this, in the above code we have used a variables new_node and flag, which after returning the data of one node or empty stack, respectively, makes the return value to 0, which ensures the read is terminated.

While popping data out of stack, we need to first check if the stack has data in it. This is done by comparing tail with test_head. If test is pointing to test_head it means the stack is empty and we return the string "Stack Empty". This is implemented by following piece of code



On the other hand if stack has data in it.



node=container_of(tail,struct k_list ,test_list): Get the pointer to the node which has the tail, last node of the list, as its member.
msg=node->data : Assign the data in the node to msg
ret=strlen(msg): Get the length of data to be transfered.
new_node=0: set new_node to 0, to indicate one node has been read.

Set count value to sting length, if count is larger than string length and reduce the variable ret by count value.
ret become 0 when ever the count valur is greater than ret.
temp=copy_to_user(buf,msg, count): Copy msg to user space.


Once all the data from the node has been transferred count becomes 0, not we need to change the tail to point to the node previous to the current node.

tail=((node->test_list).prev): Assign to tail the address of test_list of previous node.
list_del(&node->test_list): delete the node from the list.
new_node=1: Set new_node to 1 for the read of next node.

The full code for the kernel module looks as below.

stack_list.c



To compile the module use the make file.



Compile and insert the module into the kernel



To see the output



Thus we have written the 1,2,3,4 as data into 4 nodes with 4 being inserted last. Now to pop data out of the stack



Thus we can see that the proc entry is operating as a stack.

Related posts

Implementing stack using the list functions in kernel-2
Creating a read write proc entry in kernel versions above 3.10
Pointer to structure from its member pointer: container_of