LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 04-22-2013, 05:58 PM   #1
prw8864
LQ Newbie
 
Registered: Apr 2013
Distribution: Suse 12.3
Posts: 12

Rep: Reputation: Disabled
calculations with l_int32 variables in C++


I am working with a library that uses variables on type l_int32.
My understanding is that this is a quad word integer (on my system AMD 64 bit / suse 12.3)
I am trying to subtract 2 values "height = y_HI - y_LOW;" and then use height in a function to clip the borders of an image.

the variables
l_int32 height, y_HI, y_LOW ;

I can make the direct assignment of "height = 2491;" and the program clips the image correctly. But if I try to calculate the new height the clip region is short. The calculation comes up with the correct value 2491 and a check of the value "cout << height << endl;" will show 2491 but the image is clipped short.

height = y_HI - y_LOW;
box1 = boxCreate(x_LOW, y_LOW, width, height);
pix1 = pixClipRectangle(pixtmp, box1, NULL);

once calculated height is used with out alteration
"cout << height << endl;" should not alter the values.

what is the diffrence between these two assignments that would corrupt the output?
 
Old 04-22-2013, 06:25 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,109

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by prw8864 View Post
what is the diffrence between these two assignments that would corrupt the output?
I don't think there would be a difference. I think the problem is in some part of the code that you have not shown.

If you show a larger section of the code, it is more likely that someone can spot your error.

Quote:
Originally Posted by prw8864 View Post
I am working with a library that uses variables on type l_int32.
Is your use of Leptonica supposed to be secret?

I never heard of Leptonica before doing a google search on keywords from your post. But someone else answering your question might have a much easier time guessing what you are doing wrong if you had said Leptonica rather than "a library".

Last edited by johnsfine; 04-22-2013 at 06:35 PM.
 
Old 04-22-2013, 10:09 PM   #3
prw8864
LQ Newbie
 
Registered: Apr 2013
Distribution: Suse 12.3
Posts: 12

Original Poster
Rep: Reputation: Disabled
Follow up

the following is the code to calculate new height and/or width

cout << "X_LOW: " << x_LOW << " Y_LOW:" << y_LOW << " X_HI:" << x_HI << " y_HI:" << y_HI << endl;
cout << "W:" << w << " H:" << h << endl;

if (x_HI != x_LOW)
{
w2 = 0;
w2 = x_HI - x_LOW;
}
else // unchanged value
{
x_HI = w;
x_LOW = x;
w2 = w;
}

if (y_HI != y_LOW)
{
h2 = 0;
h2 = y_HI - y_LOW;
}
else
{
y_HI = h;
y_LOW = y;
h2 = h;
}

cout << "NEW =>X:" << x_LOW << " Y:" << y_LOW << " w:" << w2 << " h:" << h2 << endl;

//h2 = 2491;
box1 = boxCreate(x_LOW, y_LOW, w2, h2);
pix1 = pixClipRectangle(pixtmp, box1, NULL);
return pix1;

Yes I am using leptonica.
 
Old 04-23-2013, 05:36 AM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,109

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
You still didn't show anything that could be the problem.

Quote:
Originally Posted by prw8864 View Post
a check of the value "cout << height << endl;" will show 2491
You looked carefully? If it showed -2491, maybe you didn't notice the minus sign.

Quote:
but the image is clipped short.
How much? Maybe that would give some hint about the problem.

Quote:
height = y_HI - y_LOW;
box1 = boxCreate(x_LOW, y_LOW, width, height);
pix1 = pixClipRectangle(pixtmp, box1, NULL);

once calculated height is used with out alteration
"cout << height << endl;" should not alter the values.

what is the diffrence between these two assignments that would corrupt the output?
If everything you described is correct, then a change to your program that should not make a difference somehow does. One of the ways that is possible in C is if you continue using a memory allocation after it is freed. In this example, that would need to be pixtmp.

Where did pictmp come from? Are you sure that object was not freed before this use?
 
Old 04-23-2013, 07:06 AM   #5
prw8864
LQ Newbie
 
Registered: Apr 2013
Distribution: Suse 12.3
Posts: 12

Original Poster
Rep: Reputation: Disabled
The source image is a ppm graphic file that has been collected via scanner.
Because the scanned is unable to detect the end of page this program will be used to clip off the resulting black box area.

Is there a way to sent the image file as test data? the file is 23M and can be uploaded directly.

Code:
 #include <unistd.h>
 #include <iostream>
 #include <vector>
 #include <string>                                                                                                    
 #include <leptonica/allheaders.h> 
                                                                                              
 using namespace std;
 
 LEPT_DLL extern const char *ImageFileFormatExtensions[];
 
 l_int32 Getcount(PIX *pixtmp, vector<l_int32> &row_cnt, l_int32 w, l_int32 h)
 {
	 
	 void   **lines1;
	 l_int32 i, j, count1;
	 l_int32 max;
	 
	 max = 0;
	 lines1 = pixGetLinePtrs(pixtmp, NULL);
	 for (i = 0; i < h; i++) 
	 { 
		 count1 = 0;
		 for (j = 0; j < w; j++) 
		 { 
			 if (GET_DATA_BIT(lines1[i], j) == 0)
				 count1++;
		 }
		 row_cnt.push_back(count1);
		 max = (count1 > max) ? count1 : max; 
	 }
	 FREE(lines1);
	 return max;
 }
 
 PIX * AutoCrop(PIX *pixtmp)
 {
	 PIX    *pix1, *pix2;
	 BOX    *box1;
	 vector<l_int32> I_norm;
	 vector<l_int32> I_rot90;
	 l_int32 w, w2, x, x_HI, x_LOW, h, h2, y, y_HI, y_LOW;
	 long MAX_W, MAX_H;
	 
	 pixGetDimensions(pixtmp, &w, &h, NULL);
	 pix1 = pixConvertTo1(pixtmp, 130);
	 MAX_W = Getcount(pix1, I_norm, w, h); 
	 /*cout << "I_norm" << endl;
	 for (long i = 0; i < h; i++)
	 {
		 cout << "Y[" <<  i << "] " << I_norm[i] << endl;
	 } */
	 pix2 = pixRotate90(pix1, 1);
	 MAX_H = Getcount(pix2, I_rot90, h, w); 
	 
	 /*cout << "I_rot90" << endl;
	 for (long i = 0; i < w; i++)
	 {
		 cout << "Y[" <<  i << "] " << I_rot90[i] << endl;
	 }*/
	 pixDestroy(&pix1);
	 pixDestroy(&pix2);
	 
	 w2 = w/2;
	 h2 = h/2;
	 x = 0;
	 y = 0;
	 
	 cout << "X:" << x << " Y:" << y << " w:" << w << " h:" << h << endl;
	 //cout << "x_HI" << endl;
	 x_HI = w2;
	 while ((x_HI < w) & (I_norm[long(x_HI)] != MAX_W)) 
	 {
		 //cout << x_HI << " cnt >" << I_norm[long(x_HI)] << " MAX:" << MAX_W << endl;
		 x_HI++;

	 }
	 
	 //cout << "x_LOW" << endl;
	 x_LOW = w2;
	 while ((x_LOW > x) & (I_norm[long(x_LOW)] != MAX_W)) 
	 {
		 //cout << x_LOW << " cnt >" << I_norm[long(x_LOW)] << " MAX:" << MAX_W << endl;
		 x_LOW--;
	 }
	 
	 //cout << "y_HI" << endl;
	 y_HI = h2;
	 while ((y_HI < h) & (I_rot90[long(y_HI)] != MAX_H)) 
	 {
		 //cout << y_HI << " cnt >" << I_rot90[long(y_HI)] << " MAX:" << MAX_H << endl;
		 y_HI++;
	 }
	 
	 //cout << "y_LOW" << endl;
	 y_LOW = h2;
	 while ((y_LOW > y) & (I_rot90[long(y_LOW)] != MAX_H)) 
	 {
		 //cout << y_LOW << " cnt >" << I_rot90[long(y_LOW)] << " MAX:" << MAX_H << endl;
		 y_LOW--;
	 }
	 
	 cout << "X_LOW: " << x_LOW << " Y_LOW:" << y_LOW << " X_HI:" << x_HI << " y_HI:" << y_HI << endl;
	 cout << "W:" << w << " H:" <<  h << endl;
	 
	 if (x_HI != x_LOW)
	 {
		w2 = 0;
		w2 = abs(x_HI - x_LOW); 
	 }
	 else  // unchanged value
	 {
		x_HI = w;
		x_LOW = x;
		w2 = w;
	 }

	 if (y_HI != y_LOW)
	 {
		h2 = 0;
		h2 = abs(y_HI - y_LOW); // abs to insure positive val but no effect
	 }
	 else
	 {
		y_HI = h;
		y_LOW = y;
		h2 = h;
	 }
	 
	 cout << "NEW =>X:" << x_LOW << " Y:" << y_LOW << " w:" << w2 << " h:" << h2 << endl;
	
	 //h2 = 2491; 
	 box1 = boxCreate(x_LOW, y_LOW, w2, h2); 
	 pix1 = pixClipRectangle(pixtmp, box1, NULL);
	 
	 return pix1;
 }

 int main(int argc,  char **argv) 
 {
	  static char  mainName[] = "croptext"; 
	  string infile, outfile; 
	  PIX    *pixs, *pix1;
	  
	  char cwd[256];
	 
	  if (argc != 2)
	  {                                                                                                      
		exit(ERROR_INT("Syntax: Auto_Crop infile", mainName, 1));  
      }
      else
      {
		  infile = argv[1];
		  if ((pixs = pixRead(infile.c_str())) == NULL)                                                  
			 exit(ERROR_INT("can not read image", mainName, 1));
		  pix1 = pixDeskew(pixs, 0);
		  pixDestroy(&pixs);
		  
		  pixs = AutoCrop(pix1);
		  pixDestroy(&pix1);
		  getcwd(cwd, sizeof(cwd));
		  outfile = string(cwd) + "/New.pdf";
		  cout << outfile << endl;
		  remove(outfile.c_str());
		  pixWrite(outfile.c_str(), pixs, IFF_LPDF ); 
		  pixDestroy(&pixs);
	  }
 }
 
Old 04-23-2013, 07:18 AM   #6
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,953

Rep: Reputation: 1309Reputation: 1309Reputation: 1309Reputation: 1309Reputation: 1309Reputation: 1309Reputation: 1309Reputation: 1309Reputation: 1309Reputation: 1309
Quote:
Originally Posted by johnsfine View Post
You looked carefully? If it showed -2491, maybe you didn't notice the minus sign.
I was thinking about a rotation or probably an upside down calculation (so the zero point is the top left corner or bottom right or ??)
 
Old 04-23-2013, 08:49 AM   #7
prw8864
LQ Newbie
 
Registered: Apr 2013
Distribution: Suse 12.3
Posts: 12

Original Poster
Rep: Reputation: Disabled
if height or width are less than 0

Error in boxCreate: w and h not both >= 0
Error in pixClipRectangle: box not defined
/home/phillip/projects/Auto_Crop/New.pdf
Error in pixWrite: pix not defined

h2 = abs(y_HI - y_LOW); prevents this.
Code:
		 y_LOW = h2; // center of image y axis. y = 0
		 while ((y_LOW > y) & (I_rot90[long(y_LOW)] != MAX_H)) 
		 {
			 //cout << y_LOW << " cnt >" << I_rot90[long(y_LOW)] << " MAX:" << MAX_H << endl;
			 y_LOW--;
		 }
the border search starts from the inside and goes out


if h2 is directly assigned as 2491 the image is trimmed correctly.
the code calculates the value 2491 which is correct.
but if h2 is calculated the image is trimmed short as apposed to assigned directly , the image is correct.

the variable is loaded to "box1 = boxCreate(x_LOW, y_LOW, w2, h2); "
directly with out alteration from my code. ??????????????
 
Old 04-23-2013, 09:30 PM   #8
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,320
Blog Entries: 3

Rep: Reputation: 1464Reputation: 1464Reputation: 1464Reputation: 1464Reputation: 1464Reputation: 1464Reputation: 1464Reputation: 1464Reputation: 1464Reputation: 1464
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 04-23-2013, 10:39 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,827

Rep: Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507
It's a good thing you added debug-prints to the code. Now please remember not everyone here is clairvoyant, so please show the output of these debug-prints.
 
  


Reply

Tags
c++, calculate, variables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Time Calculations lonesoac0 Linux - Newbie 5 02-23-2012 01:48 PM
[SOLVED] Date Calculations using script!! anishkumarv Linux - Newbie 9 09-24-2011 03:36 PM
[SOLVED] top calculations sbauer72 Linux - Distributions 2 04-01-2011 08:54 PM
threads for mathematical calculations? skywalker27182 Programming 3 02-13-2005 10:25 AM
need help with pi calculations! daven1 Programming 2 01-06-2005 07:21 PM


All times are GMT -5. The time now is 06:25 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration