JPEG compressor

Dependencies:   SDL_lib SX1276Lib mbed

Revision:
1:f0c646dfe574
Parent:
0:1076a8b67c6c
Child:
2:f256eebcade8
--- a/functions.h	Wed Sep 07 18:47:02 2016 +0000
+++ b/functions.h	Thu Sep 22 00:01:55 2016 +0000
@@ -10,6 +10,7 @@
 #define RAIZ2 1.414214
 #define PI16 0.19635
 
+
 void DCT(int matrix[8][8][3],short int f[HEIGHT][WIDTH][3],int p_fil,int p_col){
     int i=0,j=0,u,v;
     float cu=1.0,cv=1.0;
@@ -20,7 +21,7 @@
 
     for(u=p_fil;u<8+p_fil;u++){
 
-        if(u==0){
+        if(u-p_fil==0){
             cu=RAIZ2/2;
         }
         else cu=1.0;
@@ -28,33 +29,38 @@
             f[u][v][0]=0;
             f[u][v][1]=0;
             f[u][v][2]=0;
-            if(v==0){
+            if(v-p_col==0){
                 cv=RAIZ2/2;
 
             }
             else cv=1.0;
 
-            short int dato=0;
-            short int dato2=0;
+            float dato=0.0;
+            float dato2=0.0;
 
+            float acum0=0.0,acum1=0.0,acum2=0.0;
 
             for(i=0;i<8;i++){
 
                 for(j=0;j<8;j++){
 
-                    dato=(short int)(cos((float)(((j<<1)+1)*(v-p_col)*PI16)));
-                    dato2=(short int)(cos((float)(((i<<1)+1)*(u-p_fil)*PI16)));
+                    dato=(float)(cos((float)(((j*2)+1)*(v-p_col)*PI16)));
+                    dato2=(float)(cos((float)(((i*2)+1)*(u-p_fil)*PI16)));
 
-                    f[u][v][0]+=(dato*dato2*(matrix[i][j][0]));//crominancia V
-                    f[u][v][1]+=(dato*dato2*(matrix[i][j][1]));//crominancia U
-                    f[u][v][2]+=(dato*dato2*(matrix[i][j][2]));//luminancia Y
+                    acum0+=(float)(dato*dato2*(matrix[i][j][0]));//crominancia V
+                    acum1+=(float)(dato*dato2*(matrix[i][j][1]));//crominancia U
+                    acum2+=(float)(dato*dato2*(matrix[i][j][2]));//luminancia Y
 
                 }
             }
                 float dato3=(float)((cu*cv)/4);
-                f[u][v][0]*=dato3;
-                f[u][v][1]*=dato3;
-                f[u][v][2]*=dato3;
+                acum0*=(float)dato3;
+                acum1*=(float)dato3;
+                acum2*=(float)dato3;
+
+                f[u][v][0]=(short int)acum0;
+                f[u][v][1]=(short int)acum1;
+                f[u][v][2]=(short int)acum2;
 
 
         }
@@ -111,13 +117,13 @@
     int i=0,p_x=0,p_y=0,j=0;
     int flag=0;
 
-    for(i=0;i<WIDTH*HEIGHT;i++){
+    for(i=0;i<WIDTH*HEIGHT*3;i+=3){
         
-        *(matrix+j)=f[p_y][p_x][0];
-        *(matrix+j+1)=f[p_y][p_x][1];
-        *(matrix+j+2)=f[p_y][p_x][2];
+        *(matrix+i)=f[p_y][p_x][0];
+        *(matrix+i+1)=f[p_y][p_x][1];
+        *(matrix+i+2)=f[p_y][p_x][2];
 
-        j+=3;
+        
         if(flag==0){
             p_x++,p_y--;
             if(p_y<0){
@@ -185,4 +191,161 @@
         }
     }
 
-}
\ No newline at end of file
+}
+
+void RLC (short int *matrix,short int acumulador [3]){
+
+    int i,k=0,acum_y=0,acum_u=0,acum_v=0;
+    short int y,u,v,y_ant=0,u_ant=0,v_ant=0,flag=0;
+    int cont[3]={1,1,1};
+    acumulador[0]=0;
+    acumulador[1]=0;
+    acumulador[2]=0;
+
+    int index=0;
+    for(i=0;i<WIDTH*HEIGHT*3;i+=3){
+
+        y=*(matrix+i+2);
+        u=*(matrix+i+1);
+        v=*(matrix+i);
+
+        
+        if(y_ant==y){
+            cont[0]++;
+        }
+        else{
+            if(i==0){
+
+            }
+            else{
+
+                acumulador[0]+=2;    
+                cont[0]=1;
+            }
+
+
+
+        }
+
+        if(u_ant==u){
+
+            cont[1]++;
+
+
+        }
+        else{
+            if(i==0){
+
+            }
+            else{
+
+                acumulador[1]+=2;  
+                cont[1]=1;
+            }
+
+        }
+
+        if(v_ant==v){
+
+            cont[2]++;
+
+        }
+        else{
+            if(i==0){
+
+            }
+            else{
+
+                
+                acumulador[2]+=2;  
+                cont[2]=1;
+            }
+
+
+        }
+        y_ant=y;
+        u_ant=u;
+        v_ant=v;
+    }
+    
+}
+
+void RLC2 (short int *matrix,short int * temp1,short int * temp2, short int *temp3){
+
+    int i,k=0,acum_y=0,acum_u=0,acum_v=0;
+    short int y,u,v,y_ant=0,u_ant=0,v_ant=0,flag=0;
+    int cont[3]={1,1,1};
+    int cy=0,cu=0,cv=0;
+
+
+    for(i=0;i<WIDTH*HEIGHT*3;i+=3){
+
+        y=*(matrix+i+2);
+        u=*(matrix+i+1);
+        v=*(matrix+i);
+
+        
+        if(y_ant==y){
+            cont[0]++;
+        }
+        else{
+            if(i==0){
+
+            }
+            else{
+                *(temp1+cy)=cont[0];
+                *(temp1+cy+1)=y_ant;
+                cy+=2;
+  
+                cont[0]=1;
+            }
+
+
+
+        }
+
+        if(u_ant==u){
+
+            cont[1]++;
+
+
+        }
+        else{
+            if(i==0){
+
+            }
+            else{
+                *(temp2+cu)=cont[1];
+                *(temp2+cu+1)=u_ant;
+                cu+=2;
+                cont[1]=1;
+            }
+
+        }
+
+        if(v_ant==v){
+
+            cont[2]++;
+
+        }
+        else{
+            if(i==0){
+
+            }
+            else{
+                *(temp3+cv)=cont[2];
+                *(temp3+cv+1)=v_ant;
+                cv+=2;
+                cont[2]=1;
+            }
+
+
+        }
+        y_ant=y;
+        u_ant=u;
+        v_ant=v;
+    }
+    
+}
+
+