Hello all!!
I have a mistake, but I cannot understand, why it occurs... :(
My code:
====================================================
#include "EXTERN.h" #include "perl.h" #include "XSUB.h"
#include "ppport.h"
MODULE = HalfWork PACKAGE = HalfWork
SV*
sum_numbers(char *number, int l,int w,int h,int k)
INIT:
int size,n,num,c,cc,ccc,cccc,i,j,r,g,b;
double y; HV* hash = newHV(); AV* array = newAV();
double data[w], akf[w]; double **ma; double **massiv;
CODE:
// printf ("l - %d, w - %d, h - %d\n", l,w,h);
n = 0; num = 0; c = 0; cc=0; ccc=0;
// double data[w], akf[w];
// double **ma;
ma = malloc(h * sizeof(*ma));
for (i=0; i<h; ++i){
ma[i] = malloc ( w * sizeof(**ma));
}
// double **massiv;
massiv = malloc(h * sizeof(*massiv));
for (i=0; i<h; ++i){
massiv[i] = malloc ( w * sizeof(**massiv));
}
while (n < l) { c++;
if (c==1) {
r=abs((int)number[n]);
// printf("c - %d r - %d\n",c,r);
}
if (c==2) {
// g=abs((int)number[n]);
// printf("c - %d g - %d\n",c,g);
}
if (c==3) { cc++;
// b=abs((int)number[n]);
// printf("c - %d b - %d\n",c,b);
c=0;
y=r; // * 0.56 + g * 0.33 + b * 0.11;
//printf ("cc - %d \n", cc);
if(cc%w!=0){
ma[num][cc-1]=y;
// printf("num - %d, cc %d, ma[num][cc] - %g\n",num,cc-1,ma[num][cc-1]);
}
if(cc%w==0){
ma[num][cc-1]=y;
// printf("num - %d, cc %d, ma[num][cc] - %g\n",num,cc-1,ma[num][cc-1]);
num++;
cc=0; ccc=0;
}
}
n++;
}
massiv=ma;
for (i=k; i<=h-1-k; i++){//printf ("i - %d ",i);
for (j=k; j<=w-1-k; j++){//printf ("j - %d\n",j);
int tmp=0; int mn=0;
int tmp1=0;
for (mn = -k; mn<=k; mn++){
int tmp_num = j + i;
tmp = tmp + ma[tmp_num][j];
}
for (mn = -k; mn<=k; mn++){
int tmp_num = j + j;
tmp1 = tmp + ma[i][tmp_num];
}
double tmp2; tmp2=(tmp+tmp1)/2;
double xxx; xxx = tmp2/(2*k+1);
// if (xxx>=255 && xxx<=0) xxx=1; // <------------------------------ in this line i have error ... :(
printf ("[ %g ]",xxx); // <------------------------------ or this line :(
//
// whithout this lines my code will work fine...
//
printf (" | i - %d j - %d w - %d, h - %d \n",i,j, w, h);
// massiv[i][j]=tmp2/(2*mn+1);
//printf ("sr - %g \n",tmp2/(2*k+1));
// massiv[i][j]=xxx;
}
}
av_push(array, newSVnv(ccc));
hv_store(hash, "array",3,(SV*)array, 0);
hv_store(hash, "unsigned_char",3, newSVpv ((char*) number, l*sizeof(number[0])), 0);
RETVAL = newRV_noinc((SV*) hash);
OUTPUT:
RETVAL
====================================================
make:
====================================================
[EMAIL PROTECTED] HalfWork]$ make
/usr/bin/perl /usr/lib/perl5/5.8.0/ExtUtils/xsubpp -typemap /usr/lib/perl5/5.8.0/ExtUtils/typemap HalfWork.xs > HalfWork.xsc && mv HalfWork.xsc HalfWork.c
Please specify prototyping behavior for HalfWork.xs (see perlxs manual)
gcc -c -I. -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBUGGING -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -O2 -g -march=i386 -mcpu=i686 -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\" -fPIC "-I/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE" HalfWork.c
rm -f blib/arch/auto/HalfWork/HalfWork.so
LD_RUN_PATH="" gcc -shared -L/usr/local/lib HalfWork.o -o blib/arch/auto/HalfWork/HalfWork.so
chmod 755 blib/arch/auto/HalfWork/HalfWork.so
====================================================
perl file:
==================================================== #!/usr/bin/perl
use strict; use warnings; use Glib qw(FALSE TRUE); use Gtk2 -init; use ExtUtils::testlib; use HalfWork;
my(@criteria,$k,@a,@res,$pix,$button,@m,$img,$pixbuf,$pixels,$w,$h,$c);
my $image = Gtk2::Image->new; my $e; $k=$ARGV[1]; print $k,"\n";
&obr($ARGV[0]);
sub obr{ my ($txt1,$min, $max); $min=10; $max=440; $img=$_[0] if $_[0]; $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file ($img); $pixels = $pixbuf->get_pixels(); $h = $pixbuf->get_height; $w = $pixbuf->get_width; my $criteria; my $hash_ref = HalfWork::sum_numbers($pixels,length $pixels,$w,$h,$k); foreach my $key (keys %$hash_ref) { print "$key"; # => $hash_ref->{$key}\n"; } } ====================================================
run:
====================================================
[EMAIL PROTECTED] HalfWork]$ time perl rr.pl file.jpg 1 1 [ 18,6667 ] | i - 1 j - 1 w - 768, h - 288 [ 24,3333 ] | i - 1 j - 2 w - 768, h - 288
[...snip...]
[ 86,3333 ] | i - 1 j - 284 w - 768, h - 288 [ 53,3333 ] | i - 1 j - 285 w - 768, h - 288 [ 55,6667 ] | i - 1 j - 286 w - 768, h - 288 Segmentation fault
real 0m0.992s user 0m0.200s sys 0m0.060s [EMAIL PROTECTED] HalfWork]$
====================================================
Thank for your help!
dmitriy