Tuesday, August 5, 2014

Laptop slows down /increased CPU consumption when connected to power


I came across this issue in my dell laptop (linux) where the laptop slowed down as soon as I was putting it to charging. This is generally counter-intuitive where you would expect the performance to increase on connecting it to power. I believe Windows OS even has settings where your laptop can move to high performance if plugged to a power source.

On searching the internet and going through my system logs, I was finally able to resolve the issue.

The solution turned out to be pretty simple:


Open the local.conf file, typically in the location-> /etc/modprobe.d/local.conf

Edit the file and add the following line:

drm_kms_helper poll=N  

You need to save the file and restart your computer.
It worked like magic for me :)


Have come to know from a friend that the problem is not restricted to Linux OS but also extends to windows. We are trying to investigate the issue and will post if we get a solution to it.



Saturday, July 26, 2014

Code for reading bmp image files directly in C/C++

We have discussed the process of reading BMP files in earlier post1 and post2 in detail. You can find the code below:

unsigned char* ReadBMP(char* filename, int* array)

{

 FILE* img = fopen(filename, "rb");   //read the file


unsigned char header[54];
fread(header, sizeof(unsigned char), 54, img); // read the 54-byte header


   // extract image height and width from header
 int width = *(int*)&header[18];    
 int height = *(int*)&header[22];    
 int padding=0; while (width*3+padding) % 4!=0 padding++;

 int widthnew=width*3+padding;

 unsigned char* data = new unsigned char[widthnew];

 for (int i=0; i<height; i++ ) {                                    

 fread( data, sizeof(unsigned char), widhthnew, img);

 for (int j=0; j<width*3; j+=3)                                

 { //Convert BGR to RGB and store                      

array[i*width+j+0] = data[j+2];                              

array[i*width+j+1] = data[j+1];                              

array[i* width+j+2] = data[j+0]; }}                        

fclose(img); //close the file
}



Please post incase you face any difficulty in running the codes.

Reading a bmp image directly using C++: Part 2


As discussed in an earlier post about the header portion of a bmp file and the information it contains.

We can observe that byte 18 and byte 22 respectively contain the height and width of the image which we will be using to read the total information in the image.

Also note that the total size of the header is 54 bytes, which is the offset obtained before we can start getting pixel level data.

Initalise the function as :
unsigned char* ReadBMP(char* filename, int* array)


Notice that we are reading the data into a 1-D array which we can always read as 3D (length, width, and colorscale) while reading as-

pixel at position x,y would be array[width*x+y]

Red value of pixel x,y would be [widht*x+y +0 ]
Blue value of pixel x,y would be [width*x+y +1 ]
Green value of pixel x,y would be [width*x+y +2 ]

To start reading the image, we will first open the file and store it in a variable img as follows-
 
 FILE* img = fopen(filename, "rb");   //read the file
Allocate 54 bytes for the image header and read it using fread-
 
unsigned char header[54];
fread(header, sizeof(unsigned char), 54, img); // read the 54-byte header
 
Obtain the height and width information from the header as discussed earlier-
 
   // extract image height and width from header   
  int width = *(int*)&header[18];       
There is extra padding present generally in BMP files if the image width/row is not a multiple of 4 bytes. Also the actual row size is 3 times the width returned by the header since we have three values each of R, G and B respectively being stored for each pixel. In BMP windows save pixels as B, G, R format instead of the conventional R, G,B order. We will be taking care of this as well.

  int padding=0; while (width*3+padding) % 4!=0 padding++;
We will create a dummy width - widthnew which we will used for reading the file

  int widthnew=width*3+padding;
The final piece of code is to read the pixels one by one through iteration-

 for (i=0; i<height; i++ ) {                                      

fread( data, sizeof(unsigned char), widhthnew, img);

for (int j=0; j<width*3; j+=3)                                  

 { //Convert BGR to RGB and store                         

array[i*width+j+0] = data[j+2];                                

array[i*width+j+1] = data[j+1];                                

array[i* width+j+2] = data[j+0]; }}                          

fclose(img); //close the file   

The complete code has been shared in the nextpost

Saturday, July 12, 2014

Dealing with different python versions


One can install multiple python versions in unix based operating sytems such as Mac and Linux

To work on a specific python version one can simply use the commands - python'version' in the terminals

Examples:

python2.6 mycode.py would run python 2.6
python3 mycode.py would run python version3.*


Similarly for specific library installation using pip on can directly call 

pip2.6 or pip-2.6 (respectively depending on using pip version 1.5 or lower)


The syntax for using easy_install is also similar:

sudo easy_install2.6 package
sudo easy_install package (would install the package for the default python)


Also to change the version of python one can simply create an alias for the command 'python' to the version of choice in the bashrc file

open the bashrc file with your favorite editor and place the following line in the end- 
eg- vim ~/.bashrc

alias "python" "python'version' "

example: alias "python" "python2.6"


If you dont have a bashrc file in your home folder, create one and paste the code


Also never delete your original python in Linux/Mac OS because a lot of the OS software dependencies are linked to python and deleting can simply make your OS stop working


PIP installation - dealing with multiple python versions

Recently, I was facing this problem  of having multiple versions of python installed and pip installs the install library to the wrong python version

The solution to this is very simple:

Suppose you have different python versions, say:
1) Python 2.6
2) Python 2.7
3) Python 3.1

Pip supports the format pip-{version}

So to install a package for say python2.7 one can simply call

pip-2.7 install packagename

Similary for 3.1

pip-3.1 install packagename


Also for one having pip version 1.5 or higher the format would be pip{version}
that is the above commands will become

pip2.7 install packagename
pip3.1 install packagename

^notice that the '-' is missing in the above commands.