Image Compression

Image Compression

A description of how we will implement PNG compression with limited computing resources

The W3C specification for PNG implementation is here:
http://www.w3.org/TR/PNG/#7Transformation

Sample code

/* 

  Procedure for JPEG compression

  Pete Hague 26/11/08

*/

#define STANDALONE

#include <io.h>

void rgbtoycc(char *source, int size) {
  unsigned char rgb[3];
  int i, current;

  /* In the future, inline lookup tables will be used */
  unsigned char yr[256], yg[256], yb[256], cbr[256], cbg[256], cbb[256], crr[256], crg[256], crb[256];

  current=0;

  for (i=0;i<size;i++) {
    rgb[0]=source[current];
    rgb[1]=source[current+1];
    rgb[2]=source[current+2];

    source[current]=16+yr[rgb[0]]+yg[rgb[1]]+yb[rgb[2]];
    source[current+1]=128+cbr[rgb[0]]+cbg[rgb[1]]+cbb[rgb[2]];
    source[current+2]=128+crr[rgb[0]]+crg[rgb[1]]+crb[rgb[2]];

    current+=3;
  }
}

void dechrome(char *source, int size, int width) {
  int i, current;
  unsigned char average;

  /* This procedure assumes an even number width and size. Both are true for flight hardware */

  width*=3; //Correct for 3 bpp

  for (i=0;i<(size-width);i+=2) {
    /* Sets each four pixel block of Cb components to their mutual average */
    average=(source[current+1]+source[current+4]+source[current+1+width]+source[current+4+width])/4;
    source[current+1]=average;
    source[current+4]=average;
    source[current+1+width]=average;
    source[current+4+width]=average;

    /* Sets each four pixel block of Cr components to their mutual average */
    average=(source[current+2]+source[current+5]+source[current+2+width]+source[current+5+width])/4;
    source[current+2]=average;
    source[current+5]=average;
    source[current+2+width]=average;
    source[current+5+width]=average;

    current+=6;
  }
}

void jpeg_compress(char *source, char *destination, int size, int width) {
   /* Convert RGB to YCC */
   rgbtoycc(source, size);

   /* Reduce C resolution */
   dechrome(source, size, width);
}

#ifdef STANDALONE
int main() {
  unsigned char image1[3000], image2[3000];

  jpeg_compress(&image1[0],&image2[0],1000, 40);
}
#endif
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.