LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2013, 05:22 AM   #1
g_paschoal
Member
 
Registered: Oct 2009
Posts: 131

Rep: Reputation: 15
For Loop passing the index to filename


I am working with Magick++ API on linux and I am reading multiple images, processing them in for loop and then I need to write them in the for loop as well passing the index value "i" to the filename. However I don't use sprintf, I use the method from Magick++ which is image.write();

How can do it ? See part of my code:

Code:
int main(int argc, char **argv) {

// ImageMagick Stuffs

	InitializeMagick(*argv);

	vector<Blob> myblob;
	vector<Image> imageList(7);
	imageList[0].read("/cars/placa1/2.png");
	imageList[1].read("/cars/placa1/4.png");
	imageList[2].read("/cars/placa1/9.png");
	imageList[3].read("/cars/placa1/9_.png");
	imageList[4].read("/cars/placa1/e.png");
	imageList[5].read("/cars/placa1/b.png");
	imageList[6].read("/cars/placa1/g.png");


	for (unsigned int i = 0;  i <= imageList.size() ; ++i) {
		imageList[i].resize("30x50");
//		imageList[i].write("/cars/results/%d.png");
		imageList[i].write(&myblob[i]);
	}
This code compiles but crash on runtime.

Code:
root@ubuntu32:~/workspace/ImageM/Debug# ./ImageM 
Segmentation fault (core dumped)

Any help will be appreciated. Thank you very much
 
Old 11-01-2013, 05:32 AM   #2
andrewthomas
Senior Member
 
Registered: May 2010
Location: Chicago Metro
Distribution: Arch, Gentoo, Slackware
Posts: 1,690

Rep: Reputation: 312Reputation: 312Reputation: 312Reputation: 312
I don't use Magick++ but have you poked around over here:

http://www.imagemagick.org/discourse...wforum.php?f=1

It might also be helpful if you posted a bigger portion of the program, someone more knowledgable may be able to figure it out from what you posted, but I certainly need more of the code.
 
Old 11-01-2013, 06:29 AM   #3
g_paschoal
Member
 
Registered: Oct 2009
Posts: 131

Original Poster
Rep: Reputation: 15
Thanks for your help, I will post on that forum.
 
Old 11-01-2013, 10:01 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
    vector<Blob> myblob(7); // need to initialize myblob vector
...
        // <= would go up to 7, but you want only 0 to 6
	for (unsigned int i = 0;  i < imageList.size() ; ++i) {
		imageList[i].resize("30x50");
//		imageList[i].write("/cars/results/%d.png");
		imageList[i].write(&myblob[i]);
	}
 
Old 11-01-2013, 10:12 AM   #5
g_paschoal
Member
 
Registered: Oct 2009
Posts: 131

Original Poster
Rep: Reputation: 15
Hi ntubski, thanks for your help.

I did what you suggested. It did compile OK and run OK.

However it is not creating any files.

this line should create a image with the same name it was read or could be something else I just need to get these images created in a for loop;

imageList[i].write(&myblob[i]);


Thanks
 
Old 11-01-2013, 10:30 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by g_paschoal View Post
this line should create a image with the same name it was read or could be something else I just need to get these images created in a for loop;

imageList[i].write(&myblob[i]);
I would guess that a "Blob" is just a block of memory, so that line probably writes the image into memory, not to a file.

The commented out line would probably create a file, but you'll want sprintf to expand the "%d".
 
Old 11-01-2013, 10:51 AM   #7
g_paschoal
Member
 
Registered: Oct 2009
Posts: 131

Original Poster
Rep: Reputation: 15
got it....

I don't want to abuse even more of your time. But... would you please show me an example of how to use sprintf in this case ?

You know... I am a beginner in c++ programmer

Thanks
 
Old 11-01-2013, 10:59 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Untested:

Code:
char filename[100];
snprintf(filename, sizeof filename, "/cars/results/%d.png", i);
imageList[i].write(filename);
 
Old 11-01-2013, 11:14 AM   #9
g_paschoal
Member
 
Registered: Oct 2009
Posts: 131

Original Poster
Rep: Reputation: 15
It worked

Thanks Thanks Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Please help script loop into the /tmp/filename.txt out put with filename and wc. dotran Linux - Newbie 10 06-08-2012 05:02 PM
SH: Passing a variable out of a "while ; do ; done" loop frenchn00b Programming 13 11-10-2011 12:48 AM
[SOLVED] Passing a list of variables to a for loop pdr_dan Linux - Newbie 5 03-21-2011 12:38 PM
Passing a filename onto a program in Wine Riichard Linux - Software 2 11-17-2004 10:11 AM
Bash script to change a filename associated with an inode index number. Ziv Programming 22 06-19-2004 08:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration