/* Copyright (C) 1999 E. H. Haley

   args: output data file; from stdin: pipe in ls of input jpeg image
   files

   ls dir | tirade out.dat

   maybe ls *.*.jpg, because something deep in the heart of
   jpeg_std_error(&jerr) exits zero when it hits something that's not
   a jpeg at all.

   does targ-style one-level-at-a-time D4 tx.  but sloooowly, little snail.

  PID USER     PRI  NI  SIZE  RSS SHARE STAT  LIB %CPU %MEM   TIME COMMAND
 4754 ebeth     19   0  1188 1188   148 R       0 87.6  0.9 356:10 tiledmany

   Haar wavelets were unbearable.  So were Daub-4's, though.  I'm
   going to feign retaining the scalability of Haars, while using the
   more descriptive D4s.  Still, coefficients are only recorded at one
   level, and the approximation is that most of them still apply at
   different ones.  Since we're only using sign and order, well, sign
   will still be appropriate, and order will be much more descriptive
   for the D4s.

   Color, even value match, sucked.  Totally distracted from the
   texture.  Added in info about average Y, and worked it into scores
   in snatches.  Thence "tirade", which still has a 'd' for D4, and an
   'a' now for average (similarly, "targa" from "targd").

on kidless version of p40kflat:

   date
   ls *.0.jpg | ../../tirade flat.tirade
   date
   ls *.1.jpg | ../../tirade flat.tirade
   date
   
   Tue May 18 07:29:03 EDT 1999
   like, TueMay13:00(?)  

with repaired tirade:
   date
   ls *.0.jpg | ../../tirade flat.trex
   date
   ls *.1.jpg | ../../tirade flat.trex
   date
   
   Tue May 18 20:16:30 EDT 1999

*/

#include <stdio.h>
#include <jpeglib.h>
#include "lib/loadj.h"
#include "lib/top.h"
#include "lib/wto.h"
#include "lib/color.h"
#include "lib/tile.h"

int main(int aardc, char **aardv)
{
  int i,j,k, w,h,c, nt,check;
  JSAMPLE *arr;
  float heap[40], *scr=NULL, *temp;
  unsigned long index[40];
  FILE *datfile;
  res top40;
  tfile curimg;
  char fname[64];

  if(aardc<2){printf("Usage: ls dirname | tirade out.dat\n");exit(0);}

  if ((datfile=fopen(*++aardv,"a"))==NULL)
    { fprintf(stderr, "tirade: can't open %s", *aardv); exit(1); }

  while (fgets(fname,64,stdin)!=NULL)
    {
      check=sscanf(fname,"%s",curimg.fname);
      if((arr=rjf(curimg.fname, &w,&h,&c))==NULL)
	{ fprintf(stderr,"tirade: couldn't open %s; continuing...\n",
		  curimg.fname);
	continue; }
      curimg.side=w;
      scr=(float *)malloc(w*h*c*sizeof(float));

      for(i=0; i<w*h; i++)
	rgbk256yiqy(arr+c*i,scr+c*i,c);
      
      for(i=0; i<w; i++)
	for(j=0; j<h; j++)
	  top40.avgy+=scr[c*(w*j+i)+0]; //k=0: just Y.

      top40.avgy/=(float)(w*h);

      //      wtliketargd(scr,w,h,c,1,daub4);
      wtliketargd(scr,w,h,c,1,dobb4);
      
      top(40, (unsigned long)w*h*c, scr-1,heap-1,index-1);
      top40.side=w;

      for (i=0; i<40; i++)
	{
	  top40.entry[i].index[0]=(index[i]/c)%w;
	  top40.entry[i].index[1]=index[i]/(c*w);
	  top40.entry[i].index[2]=index[i]%c;
	  top40.entry[i].val=heap[i];
	}
  
      curimg.nnodes=1;
      printf("%s %d\n",curimg.fname,curimg.nnodes);

      fwrite(&curimg,sizeof(tfile),1,datfile);
      fwrite(&top40,curimg.nnodes*sizeof(res),1,datfile);

      free(arr);
      free(scr);
    }  
  fclose(datfile);
}

