Solved

5/3 Wavelet Transform

Posted on 2006-07-05
28
295 Views
Last Modified: 2010-05-18
somedays ago my code works well to perform 5/3 wavelet transform but today I cannot make it run.  Also, is there anyone can help to create the reverse part based on the forward part below?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SAVE_UNIT short
#define PROCESS_UNIT short


FILE *in_file, *out_file;
  PROCESS_UNIT **img, **tmp_img;
  int dl,N;

  void main (int argc, char *argv[])
  {
          int i,j;
            printf("Forward TT transform\n");
                  if(argc<5)
                  {
                        printf("Usage:\tt <in_file> <file size> <decomp layers> <out_file>\n");
                        exit(1);
                  }
                  N=atoi(argv[2]);
                  dl=atoi(argv[3]);
                  
                  matrix_allocate(img,N,N,PROCESS_UNIT);
                  matrix_allocate(tmp_img,N,N,PROCESS_UNIT);
       
                  if ((in_file=fopen(argv[1],"rb"))==NULL)
                  {
                        printf("Binary image doesn't exist\n");
                        exit(1);
                  }

                  for(i=0; i<N; i++)
                        for(j=0; j<N; j++)
                              img[i][j]=(PROCESS_UNIT) fgetc(in_file);
                        fclose(in_file);

                        /****TT transform****/

                        { int layer,layer_size,s_coeffs=2,p_coeffs=5,fi,index,index_plus_1;

                        PROCESS_UNIT p_coeff[5]={3,-22,0,22,-3};
                        printf("\n%d,%d,%d,%d,%d\n",p_coeff[0],p_coeff[1],p_coeff[2],p_coeff[3],p_coeff[4],p_coeff[5]);

                        layer_size=2*N;
                        for(layer=0; layer<dl-1; layer++){
                              layer_size=(layer_size>>1);
                              for(i=0;i<layer_size;i++){
                                    for(j=0;j<(layer_size>>1);j++){
                                          tmp_img[i][j]=0;
                                          for(fi=0; fi<s_coeffs; fi++){
                                                index=2*j+fi;
                                                if(index>layer_size-1)
                                                      index=layer_size-1;
                                                tmp_img[i][j]+=img[i][index];
                                          }

                                          tmp_img[i][j]=(PROCESS_UNIT)(tmp_img[i][j]>>1);
                                          tmp_img[i][j+(layer_size>>1)]=0;
                                          for(fi=0; fi<p_coeffs;fi++){
                                                index=2*j+2*fi-4;
                                                index_plus_1=index+1;
                                                if(index>layer_size-1){
                                                      index=layer_size-(index-(layer_size-1));
                                                      index_plus_1=index-1;
                                                }
                                                if(index<0){
                                                      index=-index;
                                                      index_plus_1=index+1;
                                                }

                                                tmp_img[i][j+(layer_size>>1)]+=(PROCESS_UNIT)(((img[i][index]+img[i][index_plus_1])>>1)*p_coeff[fi]);}
                                          tmp_img[i][j+(layer_size>>1)]+=32;

                                          tmp_img[i][j+(layer_size>>1)]=(PROCESS_UNIT)(tmp_img[i][j+(layer_size>>1)]>>6);
                                          tmp_img[i][j+(layer_size>>1)]+=(img[i][2*j]-img[i][2*j+1]);
                                    }
                              }

                              for(j=0; j<layer_size; j++){
                                    for(i=0; i<(layer_size>>1); i++){
                                          img[i][j]=0;
                                          for(fi=0;fi<s_coeffs; fi++){
                                                index=2*i+fi;

                                                if(index>layer_size-1)
                                                      index=layer_size-1;
                                                img[i][j]+=tmp_img[index][j];
                                          }
                                          img[i][j]=(PROCESS_UNIT)(img[i][j]>>1);

                                          img[i+(layer_size>>1)][j]=0;
                                          for(fi=0; fi<p_coeffs; fi++){
                                                index=2*i+2*fi-4;
                                                index_plus_1=index+1;
                                                if(index>layer_size-1){
                                                      index=layer_size-(index-(layer_size-1));
                                                      index_plus_1=index-1;
                                                }
                                                if(index<0){
                                                      index=-index;
                                                      index_plus_1=index+1;
                                                }
                  img[i+(layer_size>>1)][j]+=(PROCESS_UNIT)(((tmp_img[index][j]+tmp_img[index_plus_1][j])>>1)*p_coeff[fi]);
                                          }

                                          img[i+(layer_size>>1)][j]+=32;
                  img[i+(layer_size>>1)][j]=(PROCESS_UNIT)(img[i+(layer_size>>1)][j]>>6);
                  img[i+(layer_size>>1)][j]+=(tmp_img[2*i][j]-tmp_img[2*i+1][j]);
                                    }
                              }
                        }
                        }

                        if((out_file=fopen(argv[4],"wb"))==NULL)
                        {
                              printf("can't open out_put file %s\n",argv[4]);
                              exit(1);
                        }

                        {
                              SAVE_UNIT tmp;
                              for(i=0; i<N; i++){
                                    for(j=0; j<N; j++){
                                          tmp=(SAVE_UNIT)(img[i][j]);
                                          fwrite(&tmp,sizeof(SAVE_UNIT),1,out_file);
                                    }
                              }
                        }
                        fclose(out_file);
}




#define matrix_allocate(matrix, hor_size, ver_size, TYPE)
{
 
TYPE *imptr;
  int _i;
  matrix=(TYPE **)calloc((unsigned long)ver_size,sizeof(TYPE *));
  imptr=(TYPE*)calloc(((unsigned long)hor_size)*((unsigned long)ver_size),sizeof(TYPE));
  if (imptr==NULL){
         printf("\nNo Memory in matrix allocate");
         exit(-1);
                        }
  for (_i=0; _i<ver_size; ++_i,imptr+=hor_size)
        matrix[_i]=imptr;

}

thanks
0
Comment
Question by:gorexy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 4
  • +3
28 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 17041158
First of all : what does "today I cannot make it run" mean ? What problem do you have, what are the errors you get, ... ?

Second : has the code been changed since the last time you were able to run it ?  If so, what has been changed ?

Third : what other circumstances are different now than the last time you ran it without problems ?
0
 
LVL 24

Expert Comment

by:fridom
ID: 17041341
Replace the define with  a proper function.

Regards
Friedrich

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 17042949
Howdy....

Are you using the same compiler and flags?

You're creating stack variables from within the executable portions of the code.  This is a C++ extension that isn't universally accepted in C programming.


Kent
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gorexy
ID: 17042998
Error message
I rearrange the code
//////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SAVE_UNIT short
#define PROCESS_UNIT short
#define matrix_allocate(matrix, hor_size, ver_size, TYPE)
{
 
TYPE *imptr;
  int _i;
  matrix=(TYPE **)calloc((unsigned long)ver_size,sizeof(TYPE *));
  imptr=(TYPE*)calloc(((unsigned long)hor_size)*((unsigned long)ver_size),sizeof(TYPE));
  if (imptr==NULL){
        printf("\nNo Memory in matrix allocate");
        exit(-1);
                    }
  for (_i=0; _i<ver_size; ++_i,imptr+=hor_size)
       matrix[_i]=imptr;

}

FILE *in_file, *out_file;
  PROCESS_UNIT **img, **tmp_img;
  int dl,N;

  void main (int argc, char *argv[])
  {
         int i,j;
          printf("Forward TT transform\n");
               if(argc<5)
               {
                    printf("Usage:\tt <in_file> <file size> <decomp layers> <out_file>\n");
                    exit(1);
               }
               N=atoi(argv[2]);
               dl=atoi(argv[3]);
               
               matrix_allocate(img,N,N,PROCESS_UNIT);
               matrix_allocate(tmp_img,N,N,PROCESS_UNIT);
       
               if ((in_file=fopen(argv[1],"rb"))==NULL)
               {
                    printf("Binary image doesn't exist\n");
                    exit(1);
               }

               for(i=0; i<N; i++)
                    for(j=0; j<N; j++)
                         img[i][j]=(PROCESS_UNIT) fgetc(in_file);
                    fclose(in_file);

                    /****TT transform****/

                    { int layer,layer_size,s_coeffs=2,p_coeffs=5,fi,index,index_plus_1;

                    PROCESS_UNIT p_coeff[5]={3,-22,0,22,-3};
                    printf("\n%d,%d,%d,%d,%d\n",p_coeff[0],p_coeff[1],p_coeff[2],p_coeff[3],p_coeff[4],p_coeff[5]);

                    layer_size=2*N;
                    for(layer=0; layer<dl-1; layer++){
                         layer_size=(layer_size>>1);
                         for(i=0;i<layer_size;i++){
                              for(j=0;j<(layer_size>>1);j++){
                                   tmp_img[i][j]=0;
                                   for(fi=0; fi<s_coeffs; fi++){
                                        index=2*j+fi;
                                        if(index>layer_size-1)
                                             index=layer_size-1;
                                        tmp_img[i][j]+=img[i][index];
                                   }

                                   tmp_img[i][j]=(PROCESS_UNIT)(tmp_img[i][j]>>1);
                                   tmp_img[i][j+(layer_size>>1)]=0;
                                   for(fi=0; fi<p_coeffs;fi++){
                                        index=2*j+2*fi-4;
                                        index_plus_1=index+1;
                                        if(index>layer_size-1){
                                             index=layer_size-(index-(layer_size-1));
                                             index_plus_1=index-1;
                                        }
                                        if(index<0){
                                             index=-index;
                                             index_plus_1=index+1;
                                        }

                                        tmp_img[i][j+(layer_size>>1)]+=(PROCESS_UNIT)(((img[i][index]+img[i][index_plus_1])>>1)*p_coeff[fi]);}
                                   tmp_img[i][j+(layer_size>>1)]+=32;

                                   tmp_img[i][j+(layer_size>>1)]=(PROCESS_UNIT)(tmp_img[i][j+(layer_size>>1)]>>6);
                                   tmp_img[i][j+(layer_size>>1)]+=(img[i][2*j]-img[i][2*j+1]);
                              }
                         }

                         for(j=0; j<layer_size; j++){
                              for(i=0; i<(layer_size>>1); i++){
                                   img[i][j]=0;
                                   for(fi=0;fi<s_coeffs; fi++){
                                        index=2*i+fi;

                                        if(index>layer_size-1)
                                             index=layer_size-1;
                                        img[i][j]+=tmp_img[index][j];
                                   }
                                   img[i][j]=(PROCESS_UNIT)(img[i][j]>>1);

                                   img[i+(layer_size>>1)][j]=0;
                                   for(fi=0; fi<p_coeffs; fi++){
                                        index=2*i+2*fi-4;
                                        index_plus_1=index+1;
                                        if(index>layer_size-1){
                                             index=layer_size-(index-(layer_size-1));
                                             index_plus_1=index-1;
                                        }
                                        if(index<0){
                                             index=-index;
                                             index_plus_1=index+1;
                                        }
               img[i+(layer_size>>1)][j]+=(PROCESS_UNIT)(((tmp_img[index][j]+tmp_img[index_plus_1][j])>>1)*p_coeff[fi]);
                                   }

                                   img[i+(layer_size>>1)][j]+=32;
               img[i+(layer_size>>1)][j]=(PROCESS_UNIT)(img[i+(layer_size>>1)][j]>>6);
               img[i+(layer_size>>1)][j]+=(tmp_img[2*i][j]-tmp_img[2*i+1][j]);
                              }
                         }
                    }
                    }

                    if((out_file=fopen(argv[4],"wb"))==NULL)
                    {
                         printf("can't open out_put file %s\n",argv[4]);
                         exit(1);
                    }

                    {
                         SAVE_UNIT tmp;
                         for(i=0; i<N; i++){
                              for(j=0; j<N; j++){
                                   tmp=(SAVE_UNIT)(img[i][j]);
                                   fwrite(&tmp,sizeof(SAVE_UNIT),1,out_file);
                              }
                         }
                    }
                    fclose(out_file);
}







error
1. line 7 F:\tt.cpp expected unqualified-id before '{' token
2. line7 F:\tt.cpp expected `,' or `;' before '{' token
3. line 27 F:\tt.cpp `main' must return `int'
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 17043034

That's a very strange way to define the matrix_allocate macro.  It appears that you really want to create a function and prototype.  If so, put a prototype in the code shortly after the #include statements and other macros.

matrix_allocate(matrix, hor_size, ver_size, TYPE) ;

Then define the function like any other:

#define matrix_allocate(matrix, hor_size, ver_size, TYPE)
{
...
}


Kent
0
 

Author Comment

by:gorexy
ID: 17043119
can u use my code and make the changes?
I cannot get exactly what you mean
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 17043139
And in case you do want a macro, don't forget to use \'s at each macro line break !! A multi-line macro has to be like this :

#define MULTI_LINE_MACRO  \
     line 1                               \
     line 2                               \
     line 3

And don't forget that the return type of main must be int, not void.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 17043158

Looking further, it appears that you're trying to define this as a macro to cause the code to be expanded within the outer function.  C doesn't let you do it this way, but it does support the functionality.

Create the prototype and define the function with the INLINE modifier:

inline matrix_allocate(matrix, hor_size, ver_size, TYPE) ;

Then define the function like any other:

inline matrix_allocate(matrix, hor_size, ver_size, TYPE)
{
...
}

Apologies, I should have dropped the keyword '#define' in my previous example.
Kent

0
 

Author Comment

by:gorexy
ID: 17043197
sorry i test but fail

do u mind to copy my code and try in your complier?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 17043217
>> do u mind to copy my code and try in your complier?
who are you talking to ? Kdo, or me ?

If me : just add a \ at the end of each macro line, and change the main return type to int
But imo, changing the macro to an inline function (as Kdo suggested) is a lot better !



Just another side note : you're doing several memory allocations, but never a free !!
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 17043287
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SAVE_UNIT short
#define PROCESS_UNIT short
matrix_allocate(matrix, hor_size, ver_size, TYPE) ;

matrix_allocate(matrix, hor_size, ver_size, TYPE)
{
 
TYPE *imptr;
  int _i;
  matrix=(TYPE **)calloc((unsigned long)ver_size,sizeof(TYPE *));
  imptr=(TYPE*)calloc(((unsigned long)hor_size)*((unsigned long)ver_size),sizeof(TYPE));
  if (imptr==NULL){
        printf("\nNo Memory in matrix allocate");
        exit(-1);
                    }
  for (_i=0; _i<ver_size; ++_i,imptr+=hor_size)
       matrix[_i]=imptr;

}

FILE *in_file, *out_file;
  PROCESS_UNIT **img, **tmp_img;
  int dl,N;

  main (int argc, char *argv[])
  {
.......  // remainder of code
0
 

Author Comment

by:gorexy
ID: 17043289
Infinity08/Kdo
I am using DevC++ to complie but seems doesn;t work

0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 84 total points
ID: 17043317

Whoops...

I'm trying to run too fast.   :~}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SAVE_UNIT short
#define PROCESS_UNIT short

matrix_allocate (TYPE**matrix, int hor_size, int ver_size) ;

matrix_allocate (TYPE **matrix, int hor_size, int ver_size)
{
 
TYPE *imptr;
  int _i;
  matrix=(TYPE **)calloc((unsigned long)ver_size,sizeof(TYPE *));
  imptr=(TYPE*)calloc(((unsigned long)hor_size)*((unsigned long)ver_size),sizeof(TYPE));
  if (imptr==NULL){
        printf("\nNo Memory in matrix allocate");
        exit(-1);
                    }
  for (_i=0; _i<ver_size; ++_i,imptr+=hor_size)
       matrix[_i]=imptr;

}

FILE *in_file, *out_file;
  PROCESS_UNIT **img, **tmp_img;
  int dl,N;

  main (int argc, char *argv[])
  {
.......  // remainder of code
0
 

Author Comment

by:gorexy
ID: 17043322
Kdo,
 I just copy and paste your code and compile I got errors

6 F:\tt.cpp expected constructor, destructor, or type conversion before '(' token
6 F:\tt.cpp expected `,' or `;' before '(' token
 F:\tt.cpp In function `int main(int, char**)':
41 F:\tt.cpp expected primary-expression before "short"
41 F:\tt.cpp `matrix_allocate' undeclared (first use this function)
  (Each undeclared identifier is reported only once for each function it appears in.)
42 F:\tt.cpp expected primary-expression before "short"
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 17046015
>> #define matrix_allocate(matrix, hor_size, ver_size, TYPE)

If you want to use your matrix_allocate function for different types, you have to use a #define, in the way Infinity08 has explained (http:#17043139)


If it is fine if it is just working for short, you can use the code from Kdo  (http:#17043317) with these modifications:

#define SAVE_UNIT short
#define PROCESS_UNIT short
#define TYPE short

void matrix_allocate (TYPE**matrix, int hor_size, int ver_size) ;

void matrix_allocate (TYPE **matrix, int hor_size, int ver_size)
{
    .......  // remainder of code
0
 

Author Comment

by:gorexy
ID: 17047595
now the code becomes:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SAVE_UNIT short
#define PROCESS_UNIT short
#define TYPE short
void matrix_allocate (TYPE**matrix, int hor_size, int ver_size, TYPE);
void matrix_allocate (TYPE**matrix, int hor_size, int ver_size, TYPE){
//void matrix_allocate(matrix, hor_size, ver_size, TYPE) {
  TYPE *imptr;
  int _i;
  matrix=(TYPE **)calloc((unsigned long)ver_size,sizeof(TYPE *));
  imptr=(TYPE*)calloc(((unsigned long)hor_size)*((unsigned long)ver_size),sizeof(TYPE));
  if (imptr==NULL){
         printf("\nNo Memory in matrix allocate");
         exit(-1);
                        }
  for (_i=0; _i<ver_size; ++_i,imptr+=hor_size)
          matrix[_i]=imptr;
  }


  FILE *in_file, *out_file;
  PROCESS_UNIT **img, **tmp_img;
  int dl,N;

int main (int argc, char *argv[])
  {
          int i,j;
            printf("Forward TT transform\n");
                  if(argc<5)
                  {
                        printf("Usage:\tt <in_file> <file size> <decomp layers> <out_file>\n");
                        exit(1);
                  }
                  N=atoi(argv[2]);
                  dl=atoi(argv[3]);
                  
                  matrix_allocate(img,N,N,PROCESS_UNIT);
                  matrix_allocate(tmp_img,N,N,PROCESS_UNIT);
       
                  if ((in_file=fopen(argv[1],"rb"))==NULL)
                  {
                        printf("Binary image doesn't exist\n");
                        exit(1);
                  }

                  for(i=0; i<N; i++)
                        for(j=0; j<N; j++)
                              img[i][j]=(PROCESS_UNIT) fgetc(in_file);
                        fclose(in_file);

                        /****TT transform****/

                        { int layer,layer_size,s_coeffs=2,p_coeffs=5,fi,index,index_plus_1;

                        PROCESS_UNIT p_coeff[5]={3,-22,0,22,-3};
                        printf("\n%d,%d,%d,%d,%d\n",p_coeff[0],p_coeff[1],p_coeff[2],p_coeff[3],p_coeff[4],p_coeff[5]);

                        layer_size=2*N;
                        for(layer=0; layer<dl-1; layer++){
                              layer_size=(layer_size>>1);
                              for(i=0;i<layer_size;i++){
                                    for(j=0;j<(layer_size>>1);j++){
                                          tmp_img[i][j]=0;
                                          for(fi=0; fi<s_coeffs; fi++){
                                                index=2*j+fi;
                                                if(index>layer_size-1)
                                                      index=layer_size-1;
                                                tmp_img[i][j]+=img[i][index];
                                          }

                                          tmp_img[i][j]=(PROCESS_UNIT)(tmp_img[i][j]>>1);
                                          tmp_img[i][j+(layer_size>>1)]=0;
                                          for(fi=0; fi<p_coeffs;fi++){
                                                index=2*j+2*fi-4;
                                                index_plus_1=index+1;
                                                if(index>layer_size-1){
                                                      index=layer_size-(index-(layer_size-1));
                                                      index_plus_1=index-1;
                                                }
                                                if(index<0){
                                                      index=-index;
                                                      index_plus_1=index+1;
                                                }

                                                tmp_img[i][j+(layer_size>>1)]+=(PROCESS_UNIT)(((img[i][index]+img[i][index_plus_1])>>1)*p_coeff[fi]);}
                                          tmp_img[i][j+(layer_size>>1)]+=32;

                                          tmp_img[i][j+(layer_size>>1)]=(PROCESS_UNIT)(tmp_img[i][j+(layer_size>>1)]>>6);
                                          tmp_img[i][j+(layer_size>>1)]+=(img[i][2*j]-img[i][2*j+1]);
                                    }
                              }

                              for(j=0; j<layer_size; j++){
                                    for(i=0; i<(layer_size>>1); i++){
                                          img[i][j]=0;
                                          for(fi=0;fi<s_coeffs; fi++){
                                                index=2*i+fi;

                                                if(index>layer_size-1)
                                                      index=layer_size-1;
                                                img[i][j]+=tmp_img[index][j];
                                          }
                                          img[i][j]=(PROCESS_UNIT)(img[i][j]>>1);

                                          img[i+(layer_size>>1)][j]=0;
                                          for(fi=0; fi<p_coeffs; fi++){
                                                index=2*i+2*fi-4;
                                                index_plus_1=index+1;
                                                if(index>layer_size-1){
                                                      index=layer_size-(index-(layer_size-1));
                                                      index_plus_1=index-1;
                                                }
                                                if(index<0){
                                                      index=-index;
                                                      index_plus_1=index+1;
                                                }
                  img[i+(layer_size>>1)][j]+=(PROCESS_UNIT)(((tmp_img[index][j]+tmp_img[index_plus_1][j])>>1)*p_coeff[fi]);
                                          }

                                          img[i+(layer_size>>1)][j]+=32;
                  img[i+(layer_size>>1)][j]=(PROCESS_UNIT)(img[i+(layer_size>>1)][j]>>6);
                  img[i+(layer_size>>1)][j]+=(tmp_img[2*i][j]-tmp_img[2*i+1][j]);
                                    }
                              }
                        }
                        }

                        if((out_file=fopen(argv[4],"wb"))==NULL)
                        {
                              printf("can't open out_put file %s\n",argv[4]);
                              exit(1);
                        }

                        {
                              SAVE_UNIT tmp;
                              for(i=0; i<N; i++){
                                    for(j=0; j<N; j++){
                                          tmp=(SAVE_UNIT)(img[i][j]);
                                          fwrite(&tmp,sizeof(SAVE_UNIT),1,out_file);
                                    }
                              }
                        }
                        fclose(out_file);
                        }


3 errors:

 D:\ctest\TT.cpp In function `int main(int, char**)':
39 D:\ctest\TT.cpp expected primary-expression before "short" (matrix_allocate(img,N,N,PROCESS_UNIT);)

40 D:\ctest\TT.cpp expected primary-expression before "short" (                  matrix_allocate(tmp_img,N,N,PROCESS_UNIT);)
0
 
LVL 27

Assisted Solution

by:Nopius
Nopius earned 83 total points
ID: 17047692
You can't pass type name 'short' as a parameter to the function.

Try to modify (remove last parameter at all):
void matrix_allocate (TYPE**matrix, int hor_size, int ver_size){

then change two calls:
matrix_allocate(img,N,N,PROCESS_UNIT);
matrix_allocate(tmp_img,N,N,PROCESS_UNIT);

to

matrix_allocate(img,N,N);
matrix_allocate(tmp_img,N,N);
0
 

Author Comment

by:gorexy
ID: 17047812
oh it works...
but when I run it, application errors.
0
 
LVL 27

Expert Comment

by:Nopius
ID: 17047978
first, it may fail because size of 'short' is not that you expect
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 83 total points
ID: 17048604
>> but when I run it, application errors.
Does it also fail when you try my earlier solution ? ie. Use this :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SAVE_UNIT short
#define PROCESS_UNIT short
#define matrix_allocate(matrix, hor_size, ver_size, TYPE)    \
{    \
     \
TYPE *imptr;    \
  int _i;    \
  matrix=(TYPE **)calloc((unsigned long)ver_size,sizeof(TYPE *));    \
  imptr=(TYPE*)calloc(((unsigned long)hor_size)*((unsigned long)ver_size),sizeof(TYPE));    \
  if (imptr==NULL){    \
        printf("\nNo Memory in matrix allocate");    \
        exit(-1);    \
                    }    \
  for (_i=0; _i<ver_size; ++_i,imptr+=hor_size)    \
       matrix[_i]=imptr;    \
    \
}

FILE *in_file, *out_file;
  PROCESS_UNIT **img, **tmp_img;

//rest of the code ...


If that also fails with application errors, then there's a problem with your code ... please post the exact errors.
0
 

Author Comment

by:gorexy
ID: 17048667
ok now..
but yes maybe the code get problem when I read an image, it promotes error
would you like to try?
0
 
LVL 27

Expert Comment

by:Nopius
ID: 17048690
I guess it's better to run debug session to see what is wrong now with the data processing.
0
 

Author Comment

by:gorexy
ID: 17048813
I remove the core part and try to see what happen

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SAVE_UNIT short
#define PROCESS_UNIT short
#define matrix_allocate(matrix, hor_size, ver_size, TYPE)    \
{    \
     \
TYPE *imptr;    \
  int _i;    \
  matrix=(TYPE **)calloc((unsigned long)ver_size,sizeof(TYPE *));    \
  imptr=(TYPE*)calloc(((unsigned long)hor_size)*((unsigned long)ver_size),sizeof(TYPE));    \
  if (imptr==NULL){    \
        printf("\nNo Memory in matrix allocate");    \
        exit(-1);    \
                    }    \
  for (_i=0; _i<ver_size; ++_i,imptr+=hor_size)    \
       matrix[_i]=imptr;    \
    \
}




FILE *in_file, *out_file;
PROCESS_UNIT **img, **tmp_img;
int dl,N;



void main (int argc, char *argv[])
{
     
          int i,j;
            printf("Forward TT transform\n");
                  if(argc<5)
                  {
                        printf("Usage:\tt <in_file> <file size> <decomp layers> <out_file>\n");
                        exit(1);
                  }
                  N=atoi(argv[2]);
                  dl=atoi(argv[3]);
                  
                  matrix_allocate(img,N,N,PROCESS_UNIT);
                  matrix_allocate(tmp_img,N,N,PROCESS_UNIT);
       
                  if ((in_file=fopen(argv[1],"rb"))==NULL)
                  {
                        printf("Binary image doesn't exist\n");
                        exit(1);
                  }
//////////////////////////////////////////////////////////////////////////
                  //for(i=0; i<N; i++)
                  //      for(j=0; j<N; j++)
                  //            img[i][j]=(PROCESS_UNIT) fgetc(in_file);
                  //      fclose(in_file);
//
                        
//////////////////////////////////////////////////////////////////
                        if((out_file=fopen(argv[4],"wb"))==NULL)
                        {
                              printf("can't open out_put file %s\n",argv[4]);
                              exit(1);
                        }

                        {
                              SAVE_UNIT tmp;
                              for(i=0; i<N; i++){
                                    for(j=0; j<N; j++){
                                          tmp=(SAVE_UNIT)(img[i][j]);
                                          fwrite(&tmp,sizeof(SAVE_UNIT),1,out_file);
                                    }
                              }
                        }
                        fclose(out_file);
}




but still get errors during running
0
 
LVL 27

Expert Comment

by:Nopius
ID: 17048828
> but still get errors during running

What errors?
0
 

Author Comment

by:gorexy
ID: 17048847
when run in DOS, it prompts application errors
now I found that

when I remark  tmp=(SAVE_UNIT)(img[i][j]);, it  can run
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question