Sunday, September 12, 2010

ImageMagick montage (creating picture collages)

Using Gimp  takes much time unless you are familiar with layers etc. Picasa 2 in ubuntu 9.04 does not give permission to customize the collage maker's options (like background color, positioning, gap etc.). So ImageMagick is a good choice to create collages.

An easy way of creating collages with ImageMagick:

montage *.jpg collage.jpg

or consider there are plenty of files in a directory named such as xy.jpg, xy1.jpg, xy2.jpg, xy3.jpg, ..., you can specify the filenames to be montaged (also border type and color, etc.) as:

# montage filesToBeMontaged -theBorderType # -theBorderColor # -theGapBetweenPictures +#+# resultantCollage
montage x{y,y1,y2,y3}.jpg -border 5 -bordercolor darkyellow -geometry +2+2 collage.jpg

Here are the other very useful examples.

Labels: ,

Tuesday, June 29, 2010

Bug in MATLAB?: Increment shortcut in a loop (ex. ++i;)

Today, i used an increment shortcut in a  MATLAB simulation loop. Surprisingly i realized that MATLAB accepts the increment shotcut (++variable) without any warning but it does not increase the variable's value in the loop, it remains the same as  the value that was initialized to.
However as an active Octave user in Ubuntu, this usage of the shortcut was just a programming practice for me and it always worked fine. Actually, as far as i understood from the MATLAB Central, these shortcuts are not implemented in MATLAB. But my point is not the absence of implementation, my point is the absence of warning (or error might be in this case) on the usage of the unimplemented increment shortcut.
It can be called as a bug because a permitted shortcut usage does not work properly at least.

Screeshot from Octave:

Screenshot from Matlab:

Labels: , ,

Wednesday, May 19, 2010

Inline defined function in OCTAVE works faster! (Was slower in MATLAB)

Previous example in MATLAB was concluded the Inline defined function was working slower than the same function defined in a ".m" file .

But if you try the same example in GNU Octave 3.2.2 you'll surprisingly see that inline defined function in Octave works faster.

On the other hand, it was not the aim of the work but Octave works faster than MATLAB for this case.

Labels: , , ,

Thursday, October 15, 2009

How to read Binary Data File in C (Sequentially)

Exactly two lines have to be changed from ASCII file mode given here.
Differences from ASCII mode:
1 - Additional "b" for fopen()
2 - Usage of fread() instate of fscanf
*/
...
fp = fopen(i_filename, "rb"); // read a binary file ("r" and "b")
...
/* loop until all data keep into array */

while( (!feof(fp))){
//fscanf(fp, "%f", &value); you can not use this any more for binary files. Now you have have to use fread()
fread(&value, sizeof(FP32), 1, fp);
...
}

Labels: , ,

Wednesday, August 19, 2009

Türkçe Karakterlerin Unicode karşılıklarını bulmak ve kullanmak

Start->Run->charmap ile Character Map açılır.
Buradaki harflerin unicode karşılıkları Character Map penceresinin en sonunda "U+0130" şeklinde gösterilir.
Bu unicode karakteri yazarken ortadaki "+" işareti kullanılmaz be başa bir "\" işareti koyulur. Örnek: I harfinin unicode'u U+0049 dur, \u0049 şeklinde kullanılır.

Labels:

Monday, August 17, 2009

How to add an image to a JPanel

In case of using Netbeans IDE Palettes for GUI building, in order to insert an image into JPanel, you have to change the layout of panel to "Border Layout". Then you have to use a JLabel to pass the image into JPanel.

I have tried many ways but this worked fine for me:

ImageIcon picturetoInsert = newImageIcon("C:/images/anyimage.gif");
JLabel label = new JLabel("",picturetoInsert,JLabel.CENTER);
pPanel.add(label,BorderLayout.CENTER);




Labels: ,

Tuesday, July 28, 2009

How to read (ASCII) data file in C

/*Function that reads the data file into an array and returns a pointer that points to this array of FP32*/

FP32* readData(const char *i_filename){

FILE *fp;
fp = fopen(i_filename, "r"); //open file , read only
INT32U numElements = 0;
FP32 value; //keep value at pointer position
FP32 *array=NULL; //set to NULL because of realloc
/* loop until all data keep into array */

while( (!feof(fp))){
fscanf(fp, "%f", &value);
array = (FP32 *) realloc (array,(numElements+1) * sizeof(FP32 *));
array[numElements++] = value;
}
/* shift array and add "size of array" to its first element
since there is no way to get size of an array from its FP32 pointer*/

INT32U i;
array = (FP32 *) realloc (array,(numElements+1) * sizeof(FP32 *));
for(i=numElements;i>0;i--){
array[i]=array[i-1];
}
array[0]=numElements;
/* close file */
fclose(fp);
return array;
}

Labels: , ,

Thursday, January 22, 2009

Windows'ta Netbeans 6.5 ile C geliştirme

Netbeans-6.5'i indir.
Netbeans-6.5 icin c/c++ development package'ini indir.
Netbeans-6.5 i kur.
Netbeans -6.5 icin c/c++ dev. pack'i kur.
MinGW-5.1.4'u indir ve kur. -> gcc for windows
MSYS'i (Not: Bu direct link değil, direct link versen de sonradan kırılır.Buradan MSYS yi kendin bulacaksın) indir ve kur-> kurulumda MinGW'nin yerini soracak ve Netbeans icin gerekli gmake'i kuracaktır.
Artık Netbeans, C/C++ projesi oluşturulmak istendiğinde otomatik olarak C compiler ve tools ayarlarını yapacaktır.
Eger hala make icin ne kullanacagını bulamamışsa : "dizin ismi":\msys\1.0\bin\make.exe'yi kullanmasını sağla.

Labels: , ,

Saturday, January 17, 2009

SUN SOLARIS 10'a gcc ve ada compiler kurmak

Makinada internet baglantisi varsa:
Adım 1:
# pkgadd -d http://blastwave.network.com/csw/pkg_get.pkg
eğer pkgadd şu hatayı verirse:
ERROR: attempt to process datastream failed
- bad format in datastream table-of -contents
http://www.blastwave.org/pkg_get.pkg dosyasını indir
ve

# pkgadd -d pkg_get.pkg

komutu ile kur.

Adım 2:
# gedit /opt/csw/bin/pkg-get.conf
komutu ile config dosyasını aç ve mirror linkini şu şekilde değiştir.
url = http://blastwave.network.com/csw/unstable

Adım 3:
gcc ve gnat'ı şu komutlarla kur:
# pkg-get install gcc4core
# pkg-get install gcc4ada
pkg-get'in faydası: 1 - tek komutla indirip kurmak.
2 - dependency'leri kendisi bulur ve kurar.
Adım 4:
gedit editorü ile profile dosyasını aç:
# gedit .etc.profile
ve dosyanın sonuna şu şatırları ekle.
PATH=${PATH}:/opt/csw/bin:/opt/csw/gcc4/bin;export PATH
-- dikkat ederseniz PATH'e /opt/csw/bin dizinini de eklemek gerek. Çünkü gcc "gas"'ı kullanıyor.
Böylece birdahaki açılışta gcc, gnat ve pkg-get fonksiyonlarına direkt ulaşılabilir.

Makinada internet baglantisi yoksa:

Bu durumda ilgili packagelari da iceren su dosyalari blastwave.org'dan indirip pkgadd ile kurmak gerekiyor:

libiconv-1.9.2-SunOS5.8-i386-CSW.pkg.gz

gcc4corert-4.0.2,REV=2005.09.29-SunOS5.8-i386-CSW.pkg.gz

gcc4core-4.0.2,REV=2005.09.29-SunOS5.8-i386-CSW.pkg.gz

gcc4adart-4.0.2,REV=2005.09.29-SunOS5.8-i386-CSW.pkg.gz

gcc4ada-4.0.2,REV=2005.09.29-SunOS5.8-i386-CSW.pkg.gz

common-1.4.6,REV=2008.04.28-SunOS5.8-i386-CSW.pkg.gz

binutils-2.17-SunOS5.8-i386-CSW.pkg.gz

Labels: , ,

Monday, January 08, 2007

VRML (Virtual Reality Modeling Language)

Web'de bulunabilecek en iyi vrml kaynağı, ayrıca underwater systems konusunda da geniş kapsamlı kaynak:
brutzman

Labels: ,

Friday, December 29, 2006

C++'da ifstream kullanmak

C++'da ifstream'in boş satırları okumaması için kullanılmasını önerdiğim kod [Bkz. C++'da fstream kullanmak] eğer formatted data dosyasında negatif sayı ile başlayan bir satır varsa o satırı ihmal ediyor.
Bundan kurtulmanın da yolu var. Biraz çirkin gibi ama eğer komentlediğiniz satırlar '-' karakteri ile başlamıyorsa şimdilik kullanılabilir.

örneğin aşağıdaki kod formatted data dosyasında '%' karakterini koment satırı olarak kabul ediyor ayrıca negatif sayı ile başlayan satırları da hatasız okuyor.
Not: Bunu kaynaklarda kolayca bulabilirsiniz. Entry'i yazma sebebim daha önce verdiğim kodun muhtemel hatasını anlatmaktır.

if(file_to_read.peek()!='-'){ //satır başında negatif sayı yoksa
if(!isdigit(file_to_read.peek())|| file_to_read.peek()=='%') {
file_to_read.ignore();
continue;
}
}

Labels: ,

Thursday, December 21, 2006

Inline in C++

The double vector sorting and addition functions are defined as inline using the inline keyword.

Code:
//MyMath.h
inline int sort_vector_inline_new(vector<double>& v){ //new inline
sort(v.begin(),v.end());
return 1;
}
inline int add_inline_new(int a,int b){
return a+b;
}


Result:(Speed test)

Labels: , ,

Inline Function in C++ (vector sorting speed test)

Now it differs from addition...
"double" vector sorting with class defined function is faster than inline function defined in header file. See the below test.

Test:
Copy and paste the below codes to your previous test project files to add vector sorting capability.
İf you want, change the name of the class "Add" in "MyMath.h". Since it contains also sort() function now
Do not forget to include "vector" and "algortihm" for dealing with vector and its sort() method.
Here is the additional includes for changed files
#include
"vector"
#include
"algorithm"
//put this code to "MyMath.h" int sort_vector_inline(vector<double>& v){ //defined inline sort(v.begin(),v.end()); return 1; }
int sort_vector_class_def(vector<double>& v);

===============================================

//put this code to "class_def.cpp" int sort_vector_class_def(vector<double>& v){ //defined inline sort(v.begin(),v.end()); return 1; }


===============================================

//All of Main.cpp is given here.(lots of changes). //Filename:Main.cpp
#include "MyMath.h"
#include "stdafx.h"
#include
"vector"
#include
"algorithm"

using namespace std;
void main()
{
DWORD startTime,endTime,difTime;
vector v,v1;
int a=1;
int b=2;
int c;
Add testAdd;

cout<<"double vector sorting..."<<"\n";

//fill vector with random numbers
for(int j=0;j<1e7;j++){
v1.push_back(rand());
}
startTime=GetTickCount();
testAdd.sort_vector_inline(v1);
endTime=GetTickCount();
difTime=endTime-startTime;
cout<< "it took "<
//fill vector again since sorted vector sorting is much more faster

for(int j=0;j<1e7;j++){
v.push_back(rand());
}
startTime=GetTickCount();
testAdd.sort_vector_class_def(v);
endTime=GetTickCount();
difTime=endTime-startTime;
cout<< "it took "<
cout<<"addition..."<<"\n";

//use function defined in other cpp file
startTime=GetTickCount();
for(int i=0;i<1e9;i++){
c=testAdd.add_class_def(a,b);
}
endTime=GetTickCount();
difTime=endTime-startTime;
cout<< "it took "<startTime=GetTickCount();
for(int i=0;i<1e9;i++){ style="color: rgb(0, 0, 0);"> c=testAdd.add_inline(a,b);
}
endTime=GetTickCount();
difTime=endTime-startTime;
cout<< "it took "<}

===============================================


I changed the order of functions in main file to see if there is a difference in speed.
It is same for the addition. Still inline function works faster.
Let's see what happened for vector sorting (below pictures).



if you change the order of function calling sequence, inline function for vector sorting works faster.
So we could not say anything about speeds yet.


Labels: , ,

Inline Function in C++

... .However, another good reason to inline is that you can sometimes speed up your program by inlining the right function. Instead of calling the function every time it is invoked, the compiler will replace the function call with a copy of the function body.
[from the page given in the link. Also read Why not inline everything?]

And here is my project for speed comparison of inline defined function and class defined function in Visual C++:

First: Copy and paste below codes with the correct name into header files in your project.

//Filename:MyMath.h
#include

using namespace std;

class Add{
public:
int add_inline( int a,int b){
return a+b;
}
int add_class_def(int a1,int b1);
};

========================================================

// Filename:stdafx.h
// include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Classes Reference and related electronic
// documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft C++ Libraries products.
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// turns off ATL's hiding of some common and often safely ignored warning messages
#define _ATL_ALL_WARNINGS
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0403
#endif
// TODO: this disables support for registering COM objects
// exported by this project since the project contains no
// COM objects or typelib. If you wish to export COM objects
// from this project, add a typelib and remove this line

#define _ATL_NO_COM_SUPPORT
#include

Second: Below codes are the cpp files for your project.

//Filename:Main.cpp
#include "MyMath.h"
#include "stdafx.h"
#include

using namespace std;
void main()
{
DWORD startTime,endTime,difTime;

int a=1;
int
b=2;
int c;
Add testAdd;
// use inline function declared and defined in header file MyMath.h
startTime=GetTickCount();
for(int i=0;i<1e9;i++){>
c=testAdd.add_inline(a,b);
}
endTime=GetTickCount();
difTime=endTime-startTime;
cout<< "it took "<<<">
//use function defined in other cpp file
startTime=GetTickCount();
for(int i=0;i<1e9;i++){
c=testAdd.add_class_def(a,b);
}
endTime=GetTickCount();
difTime=endTime-startTime;
cout<< "it took "<<<">
}
========================================================

//File name:class_def.cpp
#include "MyMath.h"
int Add::add_class_def(int a1,int b1){
return a1+b1;
}

========================================================


// Filename: stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes
// PerformanceCounter.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Classes Reference and related electronic
// documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft C++ Libraries products.
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file


Results:
*test1


*test2 (In another computer with different configuration)

Labels: , ,