/* get image colour stats */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>

/* Other header files. */
#include "oslib/os.h"
#include "oslib/osfile.h"
#include "oslib/osspriteop.h"


unsigned int spriteareastart;
unsigned int firstsprite;
unsigned int *spritepointer;
unsigned int spritedata;
static int height,width;
char *envpointer;
char rgbpointer;
char envstring[256];
char *charpointer;

int rhist[256];
int bhist[256];
int ghist[256];

double r,a,as,w,x,y,xo,yo,xd,yd,wd,f,w2pi;

double v,v2,w2,r2,e;

double racc,bacc,gacc;

int i,p,ncenter,n,nc,range,npixels;

int min_expanded, max_expanded;

int expanded_level[256];

double slope;

osspriteop_area *My_Area;
osspriteop_header *sprite_Header;


int LoadSprite(char* source_name);

int get_sprite_start(void);
void get_sprite_size(void);
/* void draw_stuff(void); */

void read_pixels(void);
void read_sprite(void);

int thispixel(int,int);
int detoxpage2(char * source_file);


int detoxpage2(char * source_file)
{

    LoadSprite(source_file);

   
  i=0;
  do
  {
    rhist[i]=0; ghist[i]=0;; bhist[i]=0;
    i++;
  } while (i<255);
  
  racc = 0.0; gacc = 0.0; bacc =0.0;
 
  ncenter = width*height/2 + width/2; npixels = width*height;


  read_pixels();

  osspriteop_save_sprite_file(osspriteop_USER_AREA, My_Area, source_file);

  if (My_Area)
	  free(My_Area);

  return 0;
}

void read_pixels(void)
{
  int pt;// pointer to navigate the image
  unsigned int colour;
 // unsigned int xstart,xstop;

  spritepointer = (unsigned int*)spriteareastart;
  firstsprite = spriteareastart+spritepointer[2];  // start + offset to first sprite
 
  spritepointer = (unsigned int*)firstsprite;
  spritedata = firstsprite+spritepointer[8];
  spritepointer = (unsigned int*)spritedata;

//  double rpc, gpc, bpc;
  unsigned int red,blue,green;
//  double min, max;
  double slope;
  
//  min = -1.0; max = -1.0;
  
//  xstart=1; pt=0;
//  xstop=width*height;
  
  pt=0;  // sprite uint32 / wordptr

// stats on all pixels: ajout de à la couleur
  do
  {
    colour=spritepointer[pt];
    
    red = (colour >> 0) & 0xffu;
    green = (colour >> 8) & 0xffu;
    blue = (colour >> 16) & 0xffu;
    
    //bhist[blue] = bhist[blue]+1;
    //ghist[green] = ghist[green]+1;
    //rhist[red] = rhist[red]+1;

    bhist[blue]  += 1;
    ghist[green] += 1;
    rhist[red]   += 1;
    /* test line follows */
    
    
    min_expanded = -1;  max_expanded = -1;
     
    pt += 1;
  } while (pt < npixels);
 
  pt=0;

  // stats on all previous tables: col hist[]: integer array
  // scaling 100.0/npixels
  do
  { 
    racc += 100.0*(rhist[pt])/npixels;
    bacc += 100.0*(bhist[pt])/npixels;
    gacc += 100.0*(ghist[pt])/npixels;
                                           
    if (min_expanded < 0)
    {
      if (bacc > 0.1)
      {
        min_expanded = pt;
      }
    }
        
    if (max_expanded < 0)
    {
      if (bacc > 50.0)
      {
        max_expanded = pt;
      }
    }
    
    pt += 1;
  } while (pt < 256);
       
  slope = 255.0/(double)(max_expanded - min_expanded);
  
  
  pt = 0;
     
  do
  {
    
    expanded_level[pt] = 0;
   
    pt += 1;
  } while (pt <= min_expanded);
     
  do
  {
    expanded_level[pt] = (int) ( slope * (double)( pt - min_expanded)   );
    
    pt += 1;
  } while (pt <= max_expanded);
     
  do
  {
    expanded_level[pt] = 255;
    pt += 1;
  } while (pt <256);
          
    
    /******  now process image (all points)*****/
    
    pt = 0; 
    
    do
    {
      colour=spritepointer[pt];
        
      blue = (colour >> 16) & 0xffu;
      
      blue = expanded_level[blue];
      
      spritepointer[pt] = (unsigned int)( blue + blue*256 + blue*256*256); 
      
      pt += 1;
    } while (pt < width*height);
     
} 


int LoadSprite(char* source_name)
{
  //Find size of sprite file
  int size;
  char spritename[12];
  os_error *error;

  osfile_read_stamped_no_path(source_name ,NULL,NULL,&size,NULL,NULL);
   
  //Reserve memory for user sprite area,  Size of area should be size of file + 4 bytes for length
  My_Area = malloc(size);

  //Initialise area with size...
  My_Area->size = size +4; 
  My_Area->first = 16;
  //Finish initialising with this sprite op  initialise sprite area

  // load sprite
  osspriteop_load_sprite_file(osspriteop_USER_AREA, My_Area, source_name );


   error = xosspriteop_return_name(osspriteop_USER_AREA, My_Area, spritename, 16, 1, 0);
        if (error) {
                fprintf(stderr, "osspriteop_return_name: 0x%x: %s\n",
                        error->errnum, error->errmess);
                exit(1);
                }
#ifdef DEBUG
  fprintf(stderr, "Sprite Name: %s\n", spritename);
#endif
  //osspriteop_read_sprite_info( osspriteop_USER_AREA, My_Area, (osspriteop_id) source_name, & width, &height, false, NULL);
  osspriteop_read_sprite_info( osspriteop_USER_AREA, My_Area, (osspriteop_id) spritename , & width, &height, false, NULL);

 spriteareastart = (int)My_Area;//  + My_Area->first;

 return 0;

}
