Next: Comments and References Up: Image Previous: Encoding

Compression

Basic Concepts

Principles of Compression

Types of Compression

Run-Lenght Encoding

Run Lenght Cell

typedef struct RLEdatum {
  unsigned char cnt;
  Pixel pix;
} RLEdatum;

Compression

void rle_write(int fd, Image *i)
{
  RLEdatum r;
  int u, v, c;

  for (v = 0; v < i->h; v++) {
    r.pix = img_geti(i, 0, v); r.cnt = 1;
    for (u = 1; u < i->w; u++) {
      if (r.cnt < 255 && (c = img_geti(i, u, v)) == r.pix) {
        r.cnt++;
      } else {
        write(fd, &r, sizeof(RLEdatum));
        r.pix = c; r.cnt = 1;
      }
    }
    write(fd, &r, sizeof(RLEdatum));
  }
}

Decompression

void rle_read(int fd, Image *i)
{
  RLEdatum r;
  int u = 0, v = 0;

  while (read(fd, &r, sizeof(RLEdatum))) {
    do {
      img_puti(i,u,v,r.pix);
      if (++u == i->w) {
        u = 0; v++;
      }
    } while (--r.cnt);
  }
}



Luiz Velho
Wed Apr 7 12:17:01 "EST 1999