/* Copyright (c) 2010 MJ Rutter 
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 */ 

/* Reduce a bitmap from 8 bits per pixel to 1, 2 or 4 bits per pixel
 * in-place. Note that each row is rounded up to the nearest
 * integer number of bytes in length.
 *
 * Originally intended for paletted images, but should work with greyscale
 * if new depth passed in image->pal_len...
 */

#include<stdio.h>
#include<stdlib.h>

#include "bmp2eps.h"

void compact(struct bitmap *image){
  int ncols,i,j,height,width,debug;
  unsigned char *p,*p2,ctmp;

  ncols=image->pal_len;
  height=image->height;
  width=image->width;
  debug=image->flags&DEBUG_MASK;

  if ((image->type==BMP)&&(image->colour==GREY)){ /* Need to squash high bits */
    p=image->data;
    ctmp=ncols-1;
    for(i=0;i<width*height;i++) *p++&=ctmp;
  }

  if (ncols==2){
    if (debug>1) fprintf(stderr,"Compacting to 1 bit / pixel\n");
    image->depth=1;
    p=p2=image->data;
    for(i=0;i<height;i++){
      for(j=0;j<width-7;j+=8,p2+=8)
        *p++=(*p2<<7)+(*(p2+1)<<6)+(*(p2+2)<<5)+(*(p2+3)<<4)
	  +(*(p2+4)<<3)+(*(p2+5)<<2)+(*(p2+6)<<1)+*(p2+7);
      if (j!=width){
        *p=0;
        for (;j<width;j++) *p=(*p<<1)+*p2++;
        *p=*p<<(8-(width&7));
        ctmp=*(p-1)&((1<<(8-(width&7)))-1); /* this might help the rle */
        *p|=ctmp;                               /* ditto */
        p++;
      }
    }
    image->data_len=height*((width+7)>>3);
  }
  else if (ncols<=4){
    if (debug>1) fprintf(stderr,"Compacting to 2 bits / pixel\n");
    image->depth=2;
    p=p2=image->data;
    for(i=0;i<height;i++){
      for(j=0;j<width-3;j+=4,p2+=4)
        *p++=(*p2<<6)+(*(p2+1)<<4)+(*(p2+2)<<2)+*(p2+3);
      if (j!=width){
        *p=0;
        for (;j<width;j++) *p=(*p<<2)+*p2++;
        *p=*p<<(2*(4-(width&3)));
        ctmp=*(p-1)&((1<<(2*(4-(width&3))))-1); /* this might help the rle */
        *p|=ctmp;                               /* ditto */
        p++;
      }
    }
    image->data_len=height*((width+3)>>2);
  }
  else if (ncols<=16){
    if (debug>1) fprintf(stderr,"Compacting to 4 bits / pixel\n");
    image->depth=4;
    p=p2=image->data;
    for(i=0;i<height;i++){
      for(j=0;j<(width-1);j+=2,p2+=2)
	*(p++)=(*p2<<4)+*(p2+1);
      if (j==(width-1)){
	*p=(*(p2++))<<4;
	*p|=*(p-1)&0xf;                       /* to help rle */
	p++;
      }
    }
    image->data_len=height*((width+1)>>1);
  }
  
  p=realloc(image->data,image->data_len);
  if (p) image->data=p;
}
