Skip to content

Instantly share code, notes, and snippets.

Created August 17, 2012 06:39
Show Gist options
  • Save anonymous/3376505 to your computer and use it in GitHub Desktop.
Save anonymous/3376505 to your computer and use it in GitHub Desktop.
Simple block-based intra-frame-coding using a ring-buffer
/* Use with files {ppm,image}.{c,h} from the QTC distribution */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "image.h"
#include "ppm.h"
#define BUFSIZE 1024*1024
#define BS 8
struct block {
struct pixel pixel[BS][BS];
bool present;
};
static struct block ringbuf[BUFSIZE];
uint32_t rbindex = 0;
int bcount = 0;
static uint32_t auto_block(struct block *);
static uint32_t find_block(struct block *);
static uint32_t put_block(struct block *);
static uint32_t *encode_image(struct image *);
static uint32_t find_block(struct block * needle) {
uint32_t i;
for (i = 0; i < BUFSIZE; i++)
if (0 == memcmp(needle,&ringbuf[i],sizeof *needle)) return i;
return UINT32_MAX;
}
static uint32_t auto_block(struct block * needle) {
uint32_t i;
i = find_block(needle);
if (i != UINT32_MAX) return i;
else return put_block(needle);
}
static uint32_t put_block(struct block * needle) {
bcount++, rbindex++;
rbindex %= BUFSIZE;
memcpy(&ringbuf[rbindex],needle,sizeof*needle);
return rbindex;
}
static uint32_t *encode_image(struct image *image) {
struct block block = { .present = true };
int i,j,k,l;
int width = image->width, height = image->height;
for (i = 0; i < width / BS; i++) {
for (j = 0; j < height / BS; j++) {
for (k = 0; k < BS; k++) {
for (l = 0; l < BS; l++) {
memcpy(&block.pixel[k][l],&image->pixels[j*BS*width+i*BS+k+l*width],sizeof(struct pixel));
}
}
auto_block(&block);
}
}
return NULL;
}
int main() {
struct image image;
ppm_read(&image,NULL);
encode_image(&image);
printf("%ix%i, %i Bloecke\n",image.width,image.height,(image.width/BS)*(image.height/BS));
printf("Benoetigte Bloecke: %i\n",bcount);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment