for(x=0;xget_image_width();x++) { for(y=0;yget_image_height();y++) { rp = r; r = data->get_pixel(x,y,RED); if(rp == r && rn < 255) { //if we find another pixel the same colour, increment the counter rn++; } else { //reset the counter and write the previous series compressed[byte_ptr] = (char)rn & 0xff; byte_ptr++; compressed[byte_ptr] = (char)rp & 0xff; byte_ptr++; rt += rn; rn = 1; } } }