Skip to content

Instantly share code, notes, and snippets.

@clausecker
Forked from anonymous/blocks.c
Created August 18, 2012 20:48
Show Gist options
  • Select an option

  • Save clausecker/3389793 to your computer and use it in GitHub Desktop.

Select an option

Save clausecker/3389793 to your computer and use it in GitHub Desktop.

Revisions

  1. clausecker revised this gist Aug 20, 2012. 2 changed files with 137 additions and 99 deletions.
    99 changes: 0 additions & 99 deletions blocks.c
    Original file line number Diff line number Diff line change
    @@ -1,99 +0,0 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <string.h>
    #include <stdbool.h>

    #include "image.h"
    #include "ppm.h"

    #define BUFSIZE 64*1024 /* number of tiles in the ringbuffer */
    #define BS 8 /* Size of a tile */

    struct block {
    struct pixel pixel[BS][BS];
    };

    static struct block ringbuf[BUFSIZE];

    int rbindex = 0;
    int bcount = 0;

    static int auto_block(struct block *);
    static int find_block(struct block *);
    static int put_block(struct block *);
    static int *encode_image(struct image *);

    static int find_block(struct block * needle) {
    int i;
    int limit = bcount > BUFSIZE ? BUFSIZE : rbindex;

    for (i = 0; i < limit; i++)
    if (0 == memcmp(needle,&ringbuf[i],sizeof *needle)) return i;

    return INT_MAX;
    }

    static int auto_block(struct block * needle) {
    int i;

    i = find_block(needle);

    if (i != INT_MAX) return i;
    else return put_block(needle);
    }

    static int put_block(struct block * needle) {

    bcount++;

    rbindex %= BUFSIZE;

    memcpy(&ringbuf[rbindex],needle,sizeof*needle);

    return rbindex++;
    }

    static int *encode_image(struct image *image) {
    struct block block;
    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++) {
    block.pixel[k][l] = image->pixels[j*BS*width+i*BS+k+l*width];
    }
    }

    auto_block(&block);
    }
    }

    return NULL;
    }

    int main() {
    struct image image;
    int dimension, framenum = 0;
    int diff;

    while(ppm_read(&image,NULL)) {
    diff = bcount;

    //image_transform(&image);
    encode_image(&image);

    dimension = (image.width/BS)*(image.height/BS);

    diff = bcount - diff;

    printf("Frame %4i, neue B.: %6i (%.2f%%), B. insges: %7i\n",++framenum,diff,diff*100.0/dimension,rbindex);

    image_free(&image);
    }

    return 0;
    }
    137 changes: 137 additions & 0 deletions tcc.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,137 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <string.h>
    #include <stdbool.h>

    #include "image.h"
    #include "ppm.h"
    #include "databuffer.h"
    #include "rangecode.h"

    #define INDEXBITS 14
    #define BUFSIZE (1<<INDEXBITS)
    #define BS 8

    struct block {
    struct pixel pixel[BS][BS];
    };

    static struct block ringbuf[BUFSIZE];

    struct databuffer *imagebuffer, *indexbuffer;

    int rbindex = 0;
    int bcount = 0;

    static int find_block(struct block * needle) {
    int i;
    int limit = bcount > BUFSIZE ? BUFSIZE : rbindex;


    // simplifiaction for analysis. Use BUFSIZE later
    for (i = 0; i < limit ; i++)
    if (0 == memcmp(needle,&ringbuf[i],sizeof *needle)) return i;

    return -1;
    }

    static int put_block(struct block * needle) {
    int x, y;

    bcount++;

    rbindex %= BUFSIZE;

    ringbuf[rbindex] = *needle;

    for( y=0; y<BS; y++ )
    for( x=0; x<BS; x++ )
    {
    databuffer_add_byte( needle->pixel[x][y].x, imagebuffer );
    databuffer_add_byte( needle->pixel[x][y].y, imagebuffer );
    databuffer_add_byte( needle->pixel[x][y].z, imagebuffer );
    }

    return rbindex++;
    }

    static int auto_block(struct block * needle) {
    int i;

    i = find_block(needle);

    if (i != -1) return i;
    else return put_block(needle);
    }

    static uint32_t *encode_image(struct image *image) {
    struct block block;
    int i,j,k,l;
    int width = image->width, height = image->height;
    int index;


    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++) {
    block.pixel[k][l] = image->pixels[j*BS*width+i*BS+k+l*width];
    }
    }

    index = auto_block(&block);
    databuffer_add_bits( index, indexbuffer, INDEXBITS );
    }
    }

    return NULL;
    }

    int main() {
    struct image image;
    int dimension, framenum = 0;
    int diff;
    int size = 0;
    struct databuffer *imagebuffer_comp, *indexbuffer_comp;
    struct rangecoder *imagecoder, *indexcoder;

    imagecoder = rangecoder_create( 2, 8 );
    indexcoder = rangecoder_create( 2, 8 );

    while(ppm_read(&image,NULL)) {
    diff = bcount;

    imagebuffer = databuffer_create( 1024 );
    indexbuffer = databuffer_create( 512 );

    image_transform(&image);
    encode_image(&image);

    dimension = (image.width/BS)*(image.height/BS);

    diff = bcount - diff;

    imagebuffer_comp = databuffer_create( 1024 );
    indexbuffer_comp = databuffer_create( 512 );

    rangecode_compress( imagecoder, imagebuffer, imagebuffer_comp );
    rangecode_compress( indexcoder, indexbuffer, indexbuffer_comp );

    size += imagebuffer_comp->size*8+imagebuffer_comp->bits;
    size += indexbuffer_comp->size*8+indexbuffer_comp->bits;

    printf("Frame %4i, neue B.: %6i (%.2f%%), B. insges: %7i size: %f\n",++framenum,diff,diff*100.0/dimension,bcount,size/(8.0*1024.0*1024.0));

    image_free(&image);

    databuffer_free( imagebuffer_comp );
    databuffer_free( indexbuffer_comp );

    databuffer_free( imagebuffer );
    databuffer_free( indexbuffer );

    }

    return 0;
    }
  2. clausecker revised this gist Aug 20, 2012. 1 changed file with 20 additions and 21 deletions.
    41 changes: 20 additions & 21 deletions blocks.c
    Original file line number Diff line number Diff line change
    @@ -1,50 +1,49 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <limits.h>
    #include <string.h>
    #include <stdbool.h>

    #include "image.h"
    #include "ppm.h"

    #define BUFSIZE 64*1024
    #define BS 8
    #define BUFSIZE 64*1024 /* number of tiles in the ringbuffer */
    #define BS 8 /* Size of a tile */

    struct block {
    struct pixel pixel[BS][BS];
    };

    static struct block ringbuf[BUFSIZE];

    uint32_t rbindex = 0;
    uint32_t bcount = 0;
    int 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 int auto_block(struct block *);
    static int find_block(struct block *);
    static int put_block(struct block *);
    static int *encode_image(struct image *);

    static uint32_t find_block(struct block * needle) {
    uint32_t i;
    uint32_t limit = bcount > BUFSIZE ? BUFSIZE : rbindex;
    static int find_block(struct block * needle) {
    int i;
    int limit = bcount > BUFSIZE ? BUFSIZE : rbindex;

    /* simplifiaction for analysis. Use BUFSIZE later */
    for (i = 0; i < limit; i++)
    if (0 == memcmp(needle,&ringbuf[i],sizeof *needle)) return i;

    return UINT32_MAX;
    return INT_MAX;
    }

    static uint32_t auto_block(struct block * needle) {
    uint32_t i;
    static int auto_block(struct block * needle) {
    int i;

    i = find_block(needle);

    if (i != UINT32_MAX) return i;
    if (i != INT_MAX) return i;
    else return put_block(needle);
    }

    static uint32_t put_block(struct block * needle) {
    static int put_block(struct block * needle) {

    bcount++;

    @@ -55,7 +54,7 @@ static uint32_t put_block(struct block * needle) {
    return rbindex++;
    }

    static uint32_t *encode_image(struct image *image) {
    static int *encode_image(struct image *image) {
    struct block block;
    int i,j,k,l;
    int width = image->width, height = image->height;
    @@ -65,7 +64,7 @@ static uint32_t *encode_image(struct image *image) {
    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));
    block.pixel[k][l] = image->pixels[j*BS*width+i*BS+k+l*width];
    }
    }

    @@ -79,7 +78,7 @@ static uint32_t *encode_image(struct image *image) {
    int main() {
    struct image image;
    int dimension, framenum = 0;
    uint32_t diff;
    int diff;

    while(ppm_read(&image,NULL)) {
    diff = bcount;
  3. clausecker revised this gist Aug 20, 2012. 1 changed file with 20 additions and 9 deletions.
    29 changes: 20 additions & 9 deletions blocks.c
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    #include "image.h"
    #include "ppm.h"

    #define BUFSIZE 1024*1024
    #define BUFSIZE 64*1024
    #define BS 8

    struct block {
    @@ -17,6 +17,7 @@ struct block {
    static struct block ringbuf[BUFSIZE];

    uint32_t rbindex = 0;
    uint32_t bcount = 0;

    static uint32_t auto_block(struct block *);
    static uint32_t find_block(struct block *);
    @@ -25,9 +26,10 @@ static uint32_t *encode_image(struct image *);

    static uint32_t find_block(struct block * needle) {
    uint32_t i;
    uint32_t limit = bcount > BUFSIZE ? BUFSIZE : rbindex;

    /* simplifiaction for analysis. Use BUFSIZE later */
    for (i = 0; i < rbindex; i++)
    for (i = 0; i < limit; i++)
    if (0 == memcmp(needle,&ringbuf[i],sizeof *needle)) return i;

    return UINT32_MAX;
    @@ -44,6 +46,8 @@ static uint32_t auto_block(struct block * needle) {

    static uint32_t put_block(struct block * needle) {

    bcount++;

    rbindex %= BUFSIZE;

    memcpy(&ringbuf[rbindex],needle,sizeof*needle);
    @@ -74,16 +78,23 @@ static uint32_t *encode_image(struct image *image) {

    int main() {
    struct image image;
    int dimension;
    int dimension, framenum = 0;
    uint32_t diff;

    while(ppm_read(&image,NULL)) {
    diff = bcount;

    //image_transform(&image);
    encode_image(&image);

    ppm_read(&image,NULL);
    //image_transform(&image);
    dimension = (image.width/BS)*(image.height/BS);

    encode_image(&image);
    diff = bcount - diff;

    dimension = (image.width/BS)*(image.height/BS);
    printf("%ix%i, %i Bloecke\n",image.width,image.height,dimension);
    printf("Benoetigte Bloecke: %i (%.2f%%)\n",rbindex,rbindex*100.0/dimension);
    printf("Frame %4i, neue B.: %6i (%.2f%%), B. insges: %7i\n",++framenum,diff,diff*100.0/dimension,rbindex);

    image_free(&image);
    }

    return 0;
    }
  4. clausecker revised this gist Aug 18, 2012. 1 changed file with 9 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions blocks.c
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    /* Use with files {ppm,image}.{c,h} from the QTC distribution */

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    @@ -14,13 +12,11 @@

    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 *);
    @@ -30,7 +26,8 @@ static uint32_t *encode_image(struct image *);
    static uint32_t find_block(struct block * needle) {
    uint32_t i;

    for (i = 0; i < BUFSIZE; i++)
    /* simplifiaction for analysis. Use BUFSIZE later */
    for (i = 0; i < rbindex; i++)
    if (0 == memcmp(needle,&ringbuf[i],sizeof *needle)) return i;

    return UINT32_MAX;
    @@ -46,17 +43,16 @@ static uint32_t auto_block(struct block * needle) {
    }

    static uint32_t put_block(struct block * needle) {
    bcount++, rbindex++;

    rbindex %= BUFSIZE;

    memcpy(&ringbuf[rbindex],needle,sizeof*needle);

    return rbindex;
    return rbindex++;
    }

    static uint32_t *encode_image(struct image *image) {
    struct block block = { .present = true };
    struct block block;
    int i,j,k,l;
    int width = image->width, height = image->height;

    @@ -78,13 +74,16 @@ static uint32_t *encode_image(struct image *image) {

    int main() {
    struct image image;
    int dimension;

    ppm_read(&image,NULL);
    //image_transform(&image);

    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);
    dimension = (image.width/BS)*(image.height/BS);
    printf("%ix%i, %i Bloecke\n",image.width,image.height,dimension);
    printf("Benoetigte Bloecke: %i (%.2f%%)\n",rbindex,rbindex*100.0/dimension);

    return 0;
    }
  5. @invalid-email-address Anonymous created this gist Aug 17, 2012.
    90 changes: 90 additions & 0 deletions blocks.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    /* 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;
    }