Solved

5/3 Wavelet Transform

Posted on 2006-07-05
28
278 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
  • 10
  • 5
  • 4
  • +3
28 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
Replace the define with  a proper function.

Regards
Friedrich

0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
 

Author Comment

by:gorexy
Comment Utility
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:Kdo
Comment Utility

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
Comment Utility
can u use my code and make the changes?
I cannot get exactly what you mean
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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:Kdo
Comment Utility

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
Comment Utility
sorry i test but fail

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

Expert Comment

by:Infinity08
Comment Utility
>> 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:Kdo
Comment Utility
#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
Comment Utility
Infinity08/Kdo
I am using DevC++ to complie but seems doesn;t work

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 45

Accepted Solution

by:
Kdo earned 84 total points
Comment Utility

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
Comment Utility
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
Comment Utility
>> #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
Comment Utility
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
Comment Utility
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
Comment Utility
oh it works...
but when I run it, application errors.
0
 
LVL 27

Expert Comment

by:Nopius
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
I guess it's better to run debug session to see what is wrong now with the data processing.
0
 

Author Comment

by:gorexy
Comment Utility
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
Comment Utility
> but still get errors during running

What errors?
0
 

Author Comment

by:gorexy
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now