OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
Usds.c
Go to the documentation of this file.
1 /*============================================================================
2  CONFIDENTIAL Do not distribute.
3  This source code contains confidential and proprietary algorithms.
4  ------------------------------------------------------------------------
5 
6  (C) Copyright 1993,1994 University of New Mexico. All Rights Reserved.
7  Name: Usds.c
8 
9 FOR INTERNAL USE ONLY INCLUDES MS OPTION
10 
11  Description: Decoding software compatible with USES chip and uses software.
12  Software is to be used for evaluation purposes only,
13  not for commercial purposes.
14  Author: Jack Venbrux of the Microelectronics Research Center
15  Date: September 9, 1993
16  Revised: May 4, 1994
17  To Compile: cc -O -o Usds Usds.c
18  For help: compile and type: Usds
19 =============================================================================*/
20 #include <stdlib.h> /* for PC compatibility*/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdint.h>
24 
25 /*****************************************************************************
26  GLOBAL DEFINES
27 *****************************************************************************/
28 #define MIN(x,y) ((x)<(y)? (x): (y))
29 
30 #define MAXLINE 80
31 #define INBLK_size 1024
32 #define TYP 0
33 #define EXT2 1
34 #define ZERO 2
35 #define ID_DECODE 3
36 #define MAX_BLKREF 128
37 #define MAX_REFPAC 128
38 #define MAX_ZBLKS 64
39 #define J_MAX 16
40 #define PIXREF_DEFAULT 256
41 #define N_DEFAULT 8
42 #define J_DEFAULT 16
43 
44 /*****************************************************************************
45  GLOBAL VARIABLES
46 *****************************************************************************/
47 int SIGN_EXT_OUTPUT=1; /* sign extend output bits to nearest next byte*/
48 
49 unsigned IN_array[INBLK_size+1];
50 unsigned XP_IN[INBLK_size+1];
52 char Buffer_in2[10000];
53 char *Binptr; /* buffer ptr */
54 unsigned SIG[2048+1]; /* maximum line size.. max size of zero blks */
55 unsigned short OUT[4096+1];
56 char Buffer_out[10240];
57 char *Bptr; /* buffer ptr */
58 char *Bend; /* buffer end ptr */
59 
60 int N;
61 int NN;
62 int J;
63 int XP; /* predictor: last pixel from previous block */
65 int MASK_IN;
67 unsigned short MASKNOT[17];
69 int IDbits;
70 int REF; /* reference flag */
71 int REF2REF; /* difference references */
72 int REFREF_flag; /* difference references */
73 int TRUEREF; /* true when REF*(!REF2REF) */
76 int IN_INDEX=0;
77 int NEXT_BIT=15;
79 int NN_mode;
80 int EP_mode;
82 int TWOD_line_mode; /* first line with NN, rest with 2d */
83 int MS_mode;
86 int BlkPac;
87 int PixRef;
88 int BlkRef;
89 int RefPac;
92 int EXT2_bit; /* ID flag bit */
94 int ZBLKS;
95 int ZFLAG;
97 int XMAX;
98 int XMAX_MS;
99 int XMAXneg;
102 unsigned REFERENCE;
104 long BLKCNT;
106 int MS[]={0,1,3,6,10,15,21,28};
107 int M[]={ 0, 1,1, 2,2,2, 3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,5, 6,6,6,6,6,6,6,
108  7,7,7,7,7,7,7,7};
109 char copyright[] = "@(#) (C) Copyright 1993,1994 University of New Mexico. All Rights Reserved.";
110 
111 static int More_data=1;
112 static int end_fill=0;
113 static char sccs_id[] = "@(#) Usds.c - Version: 1.3, Revised: 06/10/96 10:48:30";
114 static char noncommercial1[] = "This software is for evaluating the extended Rice algorithm found";
115 static char noncommercial2[] = "in the USES chip. It is not to be used for commercial purposes.";
116 static char credits1[] = "This work was supported in part by NASA Space Engineering Research Center grant";
117 static char credits2[] = "NAGW-3293 and Lossless Data Compression grant NAG 5-2166. Work on USES was in";
118 static char credits3[] = "collaboration with NASA, Goddard Space Flight Center, Greenbelt, Maryland, USA.";
119 
120 
121 /*****************************************************************************
122  FUNCTION PROTOTYPES
123 *****************************************************************************/
124 int get_ref();
125 int un_z();
126 int check_end( int id, int end_fill);
127 int get_input( int *max_data, int32_t inBytes,
128  uint8_t *encryptData);
129 int get_EP_IN( FILE *file_ptr, int pixels);
130 
131 void un_K();
132 void un_ext2();
133 void un_FS( int flag);
134 void un_DEF ();
135 void data_output( uint16_t *decryptData, int *decryptPtr);
136 //void data_output();
137 //void flush_buffer();
138 void flush_buffer( uint16_t *decryptData, int32_t *decryptPtr);
139 void check_ref_per_sample( int blkref_flag, int pixref_flag);
140 void open_files( int argc, char *argv[]);
141 void set_flags( int argc, char *argv[], int *pixref_flag, int *blkref_flag);
142 void initialize( int pixref_flag, int blkref_flag);
143 void do_uncoding( int32_t inBytes,
144  uint8_t *encryptData,
145  uint16_t *decryptData);
146 
147 /*****************************************************************************
148  CODING SECTION
149 *****************************************************************************/
150 
151 /* main(argc,argv)*/
152 
153 //int usds(argc,argv)
154 int usds(argc,argv, inBytes, encryptData, decryptData)
155 int argc;
156 char *argv[];
157 int32_t inBytes;
158 uint8_t *encryptData;
159 uint16_t *decryptData;
160 
161 {
162  int pixref_flag,blkref_flag;
163  set_flags(argc,argv,&pixref_flag,&blkref_flag);
164  initialize(pixref_flag,blkref_flag);
165  do_uncoding( inBytes, encryptData, decryptData);
166  // fclose(In_ptr);
167  // fclose(Out_ptr);
168  return(0);
169 }
170 
171 
172 /*---------------------------------------------------------------------------
173 do_uncoding()
174 ---------------------------------------------------------------------------*/
175 void do_uncoding( int32_t inBytes,
176  uint8_t *encryptData,
177  uint16_t *decryptData)
178 {
179  int id;
180  int new_packet,max_data;
181  int i,zblks;
182 
183  static int32_t decryptPtr = 0;
184 
185  BLKCNT=0;
186  ZFLAG=0;
187  new_packet=0;
188  REFERENCE=0;
189  TRUEREF=REF;
190  while ((id=get_input(&max_data, inBytes, encryptData)) >= 0)
191  {
192  // if ( inBytes == 2056) printf("id %d\n", id);
193  BLKCNT++;
194  for (i=0;i<J;i++)
195  SIG[i]= 0; /* when N large and k large this is needed */
196  if (TRUEREF)
197  {
198  REFERENCE=get_ref(); /* REFERENCE is X+XMAXneg */
199  if ((id > FS_id)&&(id != D_id))
200  un_K();
201  else if (id == FS_id)
202  un_FS(TYP);
203  else if (id == Low_id)
204  if (EXT2_bit)
205  un_ext2();
206  else
207  {
208  zblks=un_z();
209  BLKCNT=BLKCNT+zblks-1;
210  }
211  else
212  un_DEF();
213  data_output( decryptData, &decryptPtr);
214  }
215  else
216  {
217  if ((id > FS_id)&&(id != D_id))
218  un_K();
219  else if (id == FS_id)
220  un_FS(TYP);
221  else if (id == Low_id)
222  if (EXT2_bit)
223  un_ext2();
224  else
225  {
226  zblks=un_z();
227  BLKCNT=BLKCNT+zblks-1;
228  }
229  else un_DEF();
230  data_output( decryptData, &decryptPtr);
231  }
232  if ((PACKET_flag)&&(BLKCNT % BlkPac==0))
233  { /* end of packet */
234  /* if NEXT_BIT==15. then packet ended on word boundary, no fill*/
235  if (NEXT_BIT != 15)
236  IN_INDEX++; /* skip to next word */
237  NEXT_BIT=15;
238  new_packet=1;
239  }
240  REF=0;
241  REF2REF=0;
242  TRUEREF=0;
243  if (BLKCNT%BlkRef==0)
244  {
245  TWOD_line_mode=0;
246  REF2REF=(((!EP_mode)&&(REFREF_flag))&&(!new_packet)&&(!BYPASS_mode));
247  REF= ((!EP_mode)&&(!BYPASS_mode));
248  TRUEREF=((REF)&&(!REF2REF));
249  if (IN_INDEX > max_data)
250  {
251  flush_buffer( decryptData, &decryptPtr);
252  return;
253  }
254  }
255  new_packet=0;
256  }
257  /* fprintf(stderr," id %d\n", id); */
258  flush_buffer( decryptData, &decryptPtr);
259  return;
260 }
261 /*---------------------------------------------------------------------------
262 get_ref()
263 ---------------------------------------------------------------------------*/
264 int get_ref()
265 {
266  int in0,in1,msb,lsb,shift;
267  int ref,in_bit_ptr;
268 
269  /*--- globals ---------*/
270  in0=IN_array[IN_INDEX];
271  in1=IN_array[IN_INDEX+1];
272  in_bit_ptr=NEXT_BIT;
273 
274  /* condition 1: reference contained within first word*/
275  in0=in0 & MASKNOT[in_bit_ptr+1]; /* zero out leading bits */
276  if (in_bit_ptr >= (NN-1))
277  {
278  shift=in_bit_ptr-(NN-1);
279  ref=(in0>>shift);
280  if (shift==0)
281  {
282  NEXT_BIT=15;
283  IN_INDEX++;
284  }
285  else
286  NEXT_BIT=shift-1;
287  }
288  else
289  /* condition 2: reference spans two bytes*/
290  {
291  shift=NN-(in_bit_ptr+1);
292  msb=(in0<<shift);
293  lsb= in1 >> (16-shift);
294  ref=msb|lsb;
295  NEXT_BIT= 15-shift;
296  IN_INDEX++;
297  }
298  return(ref);
299 }
300 /*---------------------------------------------------------------------------
301 get_id3
302 ---------------------------------------------------------------------------*/
303 int get_id3()
304 {
305  int id,shift,msb,lsb;
306 
307  /*----- get ID -----*/
308  if (NEXT_BIT >= 2)
309  {
310  if (NEXT_BIT==2)
311  {
312  NEXT_BIT=15;
313  id=IN_array[IN_INDEX++] & 0x07;
314  }
315  else
316  {
317  shift=NEXT_BIT-2;
318  id=(IN_array[IN_INDEX]>>shift)&0x07;
319  NEXT_BIT=shift-1;
320  }
321  }
322  else
323  {
324  msb=(IN_array[IN_INDEX]<<(2-NEXT_BIT))&0x07;
325  NEXT_BIT=15-(2-NEXT_BIT);
326  lsb=(IN_array[++IN_INDEX]>>(NEXT_BIT+1));
327  id=msb|lsb;
328  }
329  K_bits= id-Kfactor;
330  if (id==Low_id)
331  {
332  /*---- find if EXT2 or ZERO flag -----*/
334  if (NEXT_BIT==0)
335  {
336  NEXT_BIT=15;
337  IN_INDEX++;
338  }
339  else
340  NEXT_BIT--;
341  }
342  return(id);
343 }
344 
345 /*---------------------------------------------------------------------------
346 get_id4
347 ---------------------------------------------------------------------------*/
348 int get_id4()
349 {
350  int id,shift,msb,lsb;
351 
352  /*----- get ID -----*/
353  if (NEXT_BIT >= 3)
354  {
355  if (NEXT_BIT==3)
356  {
357  NEXT_BIT=15;
358  id=IN_array[IN_INDEX++] & 0x0F;
359  }
360  else
361  {
362  shift=NEXT_BIT-3;
363  id=(IN_array[IN_INDEX]>>shift)&0x0F;
364  NEXT_BIT=shift-1;
365  }
366  }
367  else
368  {
369  msb=(IN_array[IN_INDEX]<<(3-NEXT_BIT))&0x0F;
370  NEXT_BIT=15-(3-NEXT_BIT);
371  lsb=(IN_array[++IN_INDEX]>>(NEXT_BIT+1));
372  id=msb|lsb;
373  }
374  K_bits= id-Kfactor;
375  if (id==Low_id)
376  {
377  /*---- find if EXT2 or ZERO flag -----*/
379  if (NEXT_BIT==0)
380  {
381  NEXT_BIT=15;
382  IN_INDEX++;
383  }
384  else
385  NEXT_BIT--;
386  }
387  return(id);
388 }
389 /*---------------------------------------------------------------------------
390  Function: un_z
391 ---------------------------------------------------------------------------*/
392 int un_z()
393 {
394  int i,j,bits,zcnt,zblks,line_blk;
395 
396  /* find size of FS coded word */
397  un_FS(ZERO);
398  bits=SIG[0]+1;
399 
400  if ((LAST_word)&&(bits > MAX_ZBLKS))
401  return 0;
402  /* (void)exit(0); */
403 
404  /* number of blocks of zeros = zerocnt+1 */
405  if (bits <5)
406  zblks=bits;
407  else if (bits==5)
408  {
409  /* EOL condition NOTE: USES will not code this if <5 blks of zeros*/
410  /* how many blocks to EOL?... USES forces an EOL after 64 blocks */
411  /* present blk for given line is equal to (BLKCNT%BlkRef)+1 */
412  line_blk=(BLKCNT%BlkRef); /* start of blk cnt for given line*/
413  if (BlkRef > 64)
414  {
415  if (line_blk > MAX_ZBLKS)
416  zblks=BlkRef-line_blk+1;
417  else
418  zblks=MAX_ZBLKS-line_blk+1;
419  }
420  else
421  zblks=BlkRef-line_blk+1;
422  /* but hardware limited to MAX_ZBLKS blocks */
423  /**** new stuff to be compatible with USES chip ****/
424  }
425  else
426  zblks=bits-1;
427  if (TRUEREF)
428  {
429  SIG[0]=REFERENCE;
430  for (i=1;i<J;i++)
431  SIG[i]=0;
432  }
433  else
434  {
435  for (i=0;i<J;i++)
436  SIG[i]=0;
437  }
438  zcnt=J;
439  for (j=2;j<=zblks;j++)
440  {
441  for (i=0;i<J;i++)
442  SIG[zcnt++]=0;
443  }
444  ZFLAG=1;
445  ZBLKS=zblks;
446  if (ZBLKS > BlkRef)
447  {
448  fprintf(stderr,"ERROR: Usds lost. Illegal number of zero_blks=%d. Must decode with same\n",ZBLKS);
449  fprintf(stderr," options you encoded with. Aborting on blk %ld, pixel %ld.\n",BLKCNT,BLKCNT*J);
450  (void)exit(1);
451  }
452  return(zblks);
453 }
454 /*---------------------------------------------------------------------------
455  Function: un_ext2
456 ---------------------------------------------------------------------------*/
457 void un_ext2()
458 {
459  int i,j,itemp,m,b;
460  int temp[J_MAX];
461 
462  /* fill in the J/2 SIG values */
463  j=0;
464  itemp=J/2+TRUEREF;
465  un_FS(EXT2);
466  for (i=TRUEREF;i<itemp;i++)
467  {
468  m=SIG[i];/* after FS fcn SIG array is filled with FS */
469  b=M[m];
470  temp[j+1]=m-MS[b];
471  temp[j]=b-temp[j+1];
472  j=j+2;
473  }
474  for (i=0;i<J;i++)
475  SIG[i]=temp[i]; /* fill global array for output fcn to use */
476 }
477 /*---------------------------------------------------------------------------
478  Function: un_FS
479 ---------------------------------------------------------------------------*/
480 void un_FS(flag)
481 int flag;
482 {
483  int sigwrds;
484  int zerocnt,one_cnt,bits;
485  int word;
486 
487  zerocnt=0;
488 
489  if (flag==TYP)
490  {
491  one_cnt=J-TRUEREF;
492  sigwrds=TRUEREF;
493  }
494  else if (flag==EXT2)
495  {
496  one_cnt=J/2;
497  sigwrds=TRUEREF;
498  }
499  else /* count FS zeros */
500  {
501  one_cnt=1;
502  sigwrds=0;
503  }
504 
505  word=IN_array[IN_INDEX++]<<(15-NEXT_BIT); /* get to first FS bit */
506  bits=NEXT_BIT+1; /* remaining bits of first word */
507 
508  /*----------- FS decoding -------------------------*/
509  while (one_cnt>0)
510  {
511  if ((word&0x8000)==0)
512  zerocnt++;
513  else /* found a "1" */
514  {
515  SIG[sigwrds++]=zerocnt;
516  zerocnt=0;
517  one_cnt--;
518  }
519  word=word<<1;
520  bits--;
521  if (bits==0) /* finished 1 word */
522  {
523  word=IN_array[IN_INDEX++];
524  bits=16;
525  }
526  }
527  IN_INDEX--;
528  NEXT_BIT=bits-1;
529 }
530 /*---------------------------------------------------------------------------
531  Function: un_K
532 ---------------------------------------------------------------------------*/
533 void un_K ()
534 {
535  int i,bitptr,dat,dat1,dat0,shift,mask;
536  int zerocnt,one_cnt,bits;
537  int word;
538  int sigwrds;
539 
541  /*----------- FS decoding -------------------------*/
542  zerocnt=0;
543  sigwrds=TRUEREF;
544  one_cnt=J-TRUEREF;
545  word=IN_array[IN_INDEX++]<<(15-NEXT_BIT); /* get to first FS bit */
546  bits=NEXT_BIT+1; /* remaining bits of first word */
547  while (one_cnt>0)
548  {
549  if ((word&0x8000)==0)
550  zerocnt++;
551  else /* found a "1" */
552  {
553  SIG[sigwrds++]=zerocnt;
554  zerocnt=0;
555  one_cnt--;
556  }
557  word=word<<1;
558  bits--;
559  if (bits==0) /* finished 1 word */
560  {
561  word=IN_array[IN_INDEX++];
562  bits=16;
563  }
564  }
565  NEXT_BIT=bits-1;
566  IN_INDEX--;
567  bitptr=bits;
568  /*-------------------------------------------------*/
569  for (i=TRUEREF;i<J;i++)
570  {
571  if (bitptr > K_bits)
572  {
573  bitptr=bitptr-K_bits;
574  dat=((IN_array[IN_INDEX])>>bitptr)&mask;
575  }
576  else if (bitptr < K_bits)
577  {
578  shift=K_bits-bitptr;
579  dat0=(IN_array[IN_INDEX++]<<shift)&mask;
580  bitptr=16-shift;
581  dat1=((IN_array[IN_INDEX])>>(bitptr))&mask;
582  dat=(dat0|dat1);
583  }
584  else
585  {
586  dat=IN_array[IN_INDEX++]&mask;
587  bitptr=16;
588  }
589  SIG[i]= (SIG[i]<<K_bits) | dat;
590  }
591  NEXT_BIT=bitptr-1;
592 }
593 /*---------------------------------------------------------------------------
594  Function: un_DEF
595 ---------------------------------------------------------------------------*/
596 void un_DEF ()
597 {
598  int t_cnt,bits,sigwrds,D_cnt;
599  int unsigned short word; /* must be a short */
600 
601  sigwrds=TRUEREF;
602  D_cnt=J-TRUEREF;
603  word=IN_array[IN_INDEX++];
604  word=word<<(15-NEXT_BIT); /* get to first Default bit */
605  bits=NEXT_BIT+1; /* remaining bits of first word */
606  /*-------------------------------------------------*/
607  t_cnt=1;
608  while (D_cnt > 0)
609  {
610  SIG[sigwrds]=(SIG[sigwrds]<<1)|(((unsigned)word)>>15);
611  if (t_cnt==NN )
612  {
613  sigwrds++;
614  t_cnt=0;
615  D_cnt--;
616  }
617  t_cnt++;
618  word=word<<1;
619  bits--;
620  if (bits==0)
621  {
622  word=IN_array[IN_INDEX++];
623  bits=16;
624  }
625  }
626  IN_INDEX--;
627  NEXT_BIT=bits-1;
628 }
629 /*---------------------------------------------------------------------------
630  Function: unmap_NN
631 ---------------------------------------------------------------------------*/
632 void unmap_NN(start,pixels)
633 int start,pixels;
634 {
635  int i,x,sig;
636 
637  for (i=start;i<pixels;i++)
638  {
639  sig=SIG[i];
640 
641  if (sig >= (XP<<1))
642  x=sig;
643  else if (sig > ((XMAX-XP)<<1))
644  x=XMAX-sig;
645  else if (sig&1)
646  x=XP-((sig+1)>>1);
647  else
648  x=XP+(sig>>1);
649  OUT[i]=x-XMAXneg;
650  XP=x;
651  }
652 }
653 /*---------------------------------------------------------------------------
654  Function: unmap_EP
655 ---------------------------------------------------------------------------*/
656 void unmap_EP(start,pixels)
657 int start,pixels;
658 {
659  int i,x,xp,sig;
660 
661  (void) get_EP_IN(Ep_ptr,pixels); /* fill block of XP_IN predictors*/
662  for (i=start;i<pixels;i++)
663  {
664  xp=XP_IN[i];
665  sig=SIG[i];
666  if (sig >= (xp<<1))
667  x=sig;
668  else if (sig > ((XMAX-xp)<<1))
669  x=XMAX-sig;
670  else if (sig&1)
671  x=xp-((sig+1)>>1);
672  else
673  x=xp+(sig>>1);
674  OUT[i]=x-XMAXneg;
675  }
676 }
677 /*---------------------------------------------------------------------------
678  Function: unmap_2D
679 ---------------------------------------------------------------------------*/
680 void unmap_TWOD(start,pixels)
681 int start,pixels;
682 {
683  int i,x,xp,sig;
684 
685  (void) get_EP_IN(Twod_ptr,pixels); /* fill block of XP_IN predictors*/
686 
687  for (i=start;i<pixels;i++)
688  {
689  xp=(XP+XP_IN[i])/2;
690  sig=SIG[i];
691  if (sig >= (xp<<1))
692  x=sig;
693  else if (sig > ((XMAX-xp)<<1))
694  x=XMAX-sig;
695  else if (sig&1)
696  x=xp-((sig+1)>>1);
697  else
698  x=xp+(sig>>1);
699  OUT[i]=x-XMAXneg;
700  XP=x;
701  }
702 }
703 /*---------------------------------------------------------------------------
704  Function: unmap_MS
705  NOTE on REFERENCES:
706  --------------------------------------------------
707  USES does the following with Raw data X and XP:
708  REFERENCE=(X+XMAXneg)-(XP+XMAXneg)+XMAX
709  where XMAXneg=(2**(N-1))if bipolar data and XMAXneg=0 else.
710  XMAX=(2**N)-1
711  eg. for N=12, XMAXneg=2048 and XMAX=4095
712  --------------------------------------------------
713  USDS does the inverse
714  (X+XMAXneg)=REFERENCE+(XP+XMAXneg)-XMAX
715  X=REFERENCE+(XP+XMAXneg)-XMAX-XMAXneg
716  --------------------------------------------------
717 
718 ---------------------------------------------------------------------------*/
719 void unmap_MS(start,pixels)
720 int start,pixels;
721 {
722  int i,x,d0,d1,sig;
723  static int prev_d1=0;
724 
725  /* fill block of MS predictors,bipolar is shifted already*/
726  (void) get_EP_IN(Ms_ptr,pixels);
727 
728  if (REF)
729  {
730  prev_d1=REFERENCE;
731  if (! REF2REF) /* XP_IN[0] already has been level shifted */
732  OUT[0]=REFERENCE+XP_IN[0]-XMAX-XMAXneg; /* refs are level shifted */
733  }
734 
735 
736  for (i=start;i<pixels;i++)
737  {
738  sig=SIG[i];
739 
740  if (sig >= (prev_d1<<1))
741  d1=sig;
742  else if (sig > ((XMAX_MS-prev_d1)<<1))
743  d1=XMAX_MS-sig;
744  else if (sig&1)
745  d1=prev_d1-((sig+1)>>1);
746  else
747  d1=prev_d1+(sig>>1);
748 
749  d0=d1-XMAX;
750  x=d0+XP_IN[i];
751  prev_d1=d1;
752  OUT[i]=x-XMAXneg;
753  }
754 }
755 /*---------------------------------------------------------------------------
756  Function: findid
757 ---------------------------------------------------------------------------*/
758 int findid(max_data)
759 int max_data;
760 {
761 
762  int id;
763 
764  if (IN_INDEX > max_data)
765  return(-1);
766 
767  if (!BYPASS_mode)
768  {
769  if (NN>8)
770  id=get_id4();
771  else
772  id=get_id3();
773 
774  if (IN_INDEX > max_data)
775  return(-1);
776 
777  if ((id < 0)||(id > D_id))
778  {
779  fprintf(stderr,"ERROR: Usds lost-- found bogus ID of %d. You must decode with same\n",id);
780  fprintf(stderr," options you encoded with. Aborting on blk %ld, pixel %ld.\n",BLKCNT,
781  BLKCNT*J);
782  (void)exit(1);
783  }
784  }
785  else
786  id=D_id; /* in Bypass mode */
787 
788  return(id);
789 }
790 /*****************************************************************************
791  INPUT OUTPUT SECTION
792 *****************************************************************************/
793 /*---------------------------------------------------------------------------
794  Function: get_input
795 Description: grab chunks of data and fill up IN_array[]
796 ---------------------------------------------------------------------------*/
797 int get_input( int *max_data, int32_t inBytes,
798  uint8_t *encryptData)
799 //int *max_data;
800 {
801  int id,i,index,msb,incnt,cnt=0;
802  static int Max_data=0;
803  static int called_cnt=0;
804  static int encryptPtr=0;
805  // if ( inBytes == 2056) printf("called_cnt: %d\n", called_cnt);
806  called_cnt++;
807 
808  /*-------- get data section --------------------------*/
809  if (IN_INDEX >= INBLK_size-18)
810  { /* close to end of IN_array readjust then refill with more data*/
811  if (Max_data == INBLK_size-1)
812  { /* if Max_data < INBLK_size then no more data to load in */
813  cnt=0;
815  IN_array[cnt++]=IN_array[index];
816  IN_INDEX=0;
817  More_data=1;
818  }
819  }
820  if (More_data)
821  {
822  Binptr= &Buffer_in[0];
823  More_data=0;
824 
825  // printf("encryptPtr: %d\n", encryptPtr);
826  if ( ((INBLK_size-cnt)<<1) > inBytes) {
827  memcpy( (void*)Buffer_in, encryptData, inBytes);
828  incnt = inBytes;
829  encryptPtr = 0;
830  } else {
831  if ( encryptPtr == 0) {
832  memcpy( (void*)Buffer_in, encryptData, ((INBLK_size-cnt)<<1));
833  incnt = (INBLK_size-cnt)<<1;
834  encryptPtr = incnt;
835  } else {
836  int32_t nread = inBytes-encryptPtr;
837  if ( (inBytes-encryptPtr) > (INBLK_size-cnt)<<1) nread = (INBLK_size-cnt)<<1;
838  memcpy ( (void*)Buffer_in, &encryptData[encryptPtr], nread);
839  incnt = nread;
840  encryptPtr += incnt;
841  if ( encryptPtr == inBytes) encryptPtr = 0;
842  }
843  }
844 
845  if (incnt >= 1)
846  {
847  // if ( inBytes == 2056) printf("incnt: %d\n", incnt);
848  // printf("buffer_in[0]: %d\n", Buffer_in[0]);
849  index=(incnt>>1); /* divide by 2 */
850  for (i=0;i<index;i++)
851  {
852  msb = (*Binptr)<<8;;
853  Binptr++;
854  IN_array[cnt++]=((msb|((*Binptr)&0xFF))&0xFFFF);
855  Binptr++;
856  }
857  if ((incnt&0x01)==1)
858  { /* final byte is fill */
859  IN_array[cnt++]=((((*Binptr)<<8)|0xFF)&0xFFFF);
860  Binptr++;
861  end_fill=1; /* entire byte of fill */
862  }
863  }
864  // if ( inBytes == 2056) printf("cnt: %d\n", cnt);
865  Max_data=cnt-1;
866  IN_array[cnt] = 0; // JMG 10/17/14
867  }
868  *max_data=Max_data;
869  id=findid(Max_data);
870 
871  if (IN_INDEX >= Max_data)
872  {
873  LAST_word=1;
874  id=check_end(id,end_fill);
875 
876  }
877 
878  return(id);
879 }
880 /*---------------------------------------------------------------------------
881 check_end()
882 Description: determine when file ends (final fill bits have been turned to '1's
883  unless operating in Chip Mode)
884 ---------------------------------------------------------------------------*/
885 int check_end(id,end_fill)
886 int id,end_fill;
887 {
888  int zbits,ext2bits,fsbits,blks_togo;
889 
890  /* Smallest blocksize != zero, is ext2..J=8,IDcompressed totcnt=7 bits */
891  /* so using smallest blocksize.. see if possible to fit a scanline */
892  /* in the last data word. Encoder has already filled with */
893  /* ones anything that isn't data in the last byte (so no zero IDs) */
894  /* The smallest winning blocksize for FS is 10 bits */
895 
896  if (id > FS_id)
897  id=(-1);
898  else
899  {
900  /*ID found in last word.. so no more than 15 bits left in lastword*/
901  /* if zero or ext2.. toggle bit exists-- no more than 14 bits left*/
902 
903  blks_togo=BlkRef-(BLKCNT%BlkRef);
904 
905  zbits=1; /* once id and toggle grabbed.. 1 bit needed to end zero blk*/
906  ext2bits=((J/2)+1);
907  fsbits=J+1;
908 
911  if (blks_togo > 9999)
912  {
913  zbits++; /* once id is grabbed, only 2 bits needed to end zblks*/
914  ext2bits=ext2bits+5; /* coding ID + zeroblk*/
915  fsbits=fsbits+5; /* coding ID + zeroblock */
916  }
917  if ((id == Low_id)&&(EXT2_bit==0))
918  { /* zero blk */
919  if (((NEXT_BIT+1)-(end_fill*8)) < zbits)
920  id=(-1);
921  }
922  else if ((id == FS_id) &&
923  (((NEXT_BIT+1)-(end_fill*8)) < fsbits))
924  id=(-1);
925  else if ((id == Low_id)&&(((NEXT_BIT+1)-(end_fill*8)) < ext2bits))
926  id=(-1);
927  else if (id > FS_id)
928  id=(-1);
929  }
930  return(id);
931 }
932 
933 /*---------------------------------------------------------------------------
934  Function: get_EP_IN()
935  Description: fill XP_IN[] array with pixels from an external pixel file
936  Returns: Number of samples read in (1,2,..)
937 ---------------------------------------------------------------------------*/
938 int get_EP_IN(file_ptr,pixels)
939 FILE *file_ptr;
940 int pixels;
941 {
942  int i,lsb,msb,x,pos,x_cnt;
943 
944  x_cnt=0;
945  if (BYTE_pixel == 2) /* 2 bytes per pixel */
946  {
947  while(x_cnt <pixels)
948  {
949  msb = getc(file_ptr);
950  if ((lsb = getc(file_ptr)) == EOF)
951  goto BOTTOM;
952  x = (msb<<8) | lsb;
953  XP_IN[x_cnt++]=(x & MASK_IN);
954  }
955  }/*twobytes*/
956  else /* one byte per pixel */
957  {
958  while(x_cnt <pixels)
959  {
960  if ((x = getc(file_ptr)) == EOF)
961  goto BOTTOM;
962  XP_IN[x_cnt++]= (x & MASK_IN);
963  }
964  }
965  if (NEG_data)
966  {
967  for (i=0;i<x_cnt;i++)
968  {
969  if ((MASK_SIGN&XP_IN[i])!= 0) /* check sign bit for neg*/
970  {
971  pos=(int)((MASK_IN&(~XP_IN[i]))+1);
972  XP_IN[i]=((-pos)+XMAXneg)&MASK_IN;
973  }
974  else
975  {
977  }
978  }
979  }
980 BOTTOM:
981 return((int)x_cnt);
982 }
983 /*---------------------------------------------------------------------------
984  Function: data_output
985 ---------------------------------------------------------------------------*/
986 void data_output( uint16_t *decryptData, int *decryptPtr)
987 {
988  int i,pixels;
989  int start;
990  unsigned short *p,*pend;
991 
992  if (ZFLAG)
993  {
994  pixels=ZBLKS*J;
995  ZBLKS=0;
996  }
997  else
998  pixels=J;
999 
1000  start=0;
1001  if (REF)
1002  {
1003  XP=REFERENCE;
1004  if (! REF2REF)
1005  {
1006  OUT[0]=REFERENCE-XMAXneg; /* refs are level shifted pixels */
1007  start=1;
1008  }
1009  }
1010  if ((ENT_CODING)||(BYPASS_mode))
1011  for (i=0;i<pixels;i++)
1012  OUT[i] = SIG[i]-XMAXneg;
1013  else if (NN_mode)
1014  unmap_NN(start,pixels);
1015  else if (TWOD_line_mode)
1016  {
1017  (void) get_EP_IN(Twod_ptr,pixels); /* fill block of predictors*/
1018  unmap_NN(start,pixels);
1019  }
1020  else if(EP_mode) /* external Predictor mode */
1021  unmap_EP(start,pixels);
1022  else if(TWOD_mode) /* external Predictor mode */
1023  unmap_TWOD(start,pixels);
1024  else if(MS_mode)
1025  unmap_MS(start,pixels);
1026 
1027 
1028  if (REF) /* for next ref2ref compression */
1029  {
1030  if (!MS_mode)
1031  REFERENCE=(OUT[0]+XMAXneg)&MASK_IN;/*this restores to orig. ref */
1032  else
1033  REFERENCE=(OUT[0]+XMAXneg)-XP_IN[0]+XMAX; /*XPin is level shifted*/
1034  }
1035  /*-------- output data -----------*/
1036  if (BYTE_pixel==1)
1037  {
1038  pend= &OUT[pixels-1];
1039  for (p= &OUT[0];p<=pend;p++)
1040  {
1041  if ((MASK_SIGN & *p)== 0)
1042  *Bptr++ = *p;
1043  else
1044  *Bptr++ = *p | MASKN_neg; /* sign extend */
1045  if (Bptr == Bend) /* dump buffer to output */
1046  {
1047  memcpy( &decryptData[*decryptPtr], Buffer_out, sizeof(Buffer_out));
1048 
1049  // fwrite((void *)Buffer_out,(size_t)sizeof(Buffer_out),(size_t)1,Out_ptr);
1050  Bptr=Buffer_out;
1051  *decryptPtr += sizeof(Buffer_out);
1052  }
1053  }
1054  }
1055  else
1056  {
1057  pend= &OUT[pixels-1];
1058  for (p= &OUT[0];p<=pend;p++)
1059  {
1060  if ((MASK_SIGN & *p)== 0)
1061  *Bptr++ = ((unsigned)(*p) & MASK_IN)>>8;
1062  else
1063  *Bptr++ = ((unsigned)(*p) | MASKN_neg)>>8; /* sign extend */
1064  *Bptr++ = *p;
1065 
1066  if (Bptr == Bend) /* dump buffer to output */
1067  {
1068  memcpy( &decryptData[*decryptPtr], Buffer_out, sizeof(Buffer_out));
1069  // fwrite((void*)Buffer_out,(size_t)sizeof(Buffer_out),(size_t)1,Out_ptr);
1070  Bptr=Buffer_out;
1071  *decryptPtr += sizeof(Buffer_out);
1072  }
1073 
1074  }
1075  }
1076  ZFLAG=0;
1077 }
1078 /*---------------------------------------------------------------------------
1079  Function: flush_buffer
1080  Description: called upon EOF flushes output buffer
1081 ---------------------------------------------------------------------------*/
1082 void flush_buffer( uint16_t *decryptData, int32_t *decryptPtr)
1083 {
1084  if (Bptr != Buffer_out)
1085  {
1086  memcpy( &decryptData[*decryptPtr], Buffer_out, Bptr-Buffer_out);
1087  // fwrite((void*)Buffer_out,(size_t)(Bptr-Buffer_out),(size_t)1,Out_ptr);
1088  Bptr=Buffer_out;
1089  *decryptPtr = 0;
1090  }
1091 }
1092 /*****************************************************************************
1093  USER INTERFACE and HELP
1094 *****************************************************************************/
1095 /*--------------------------------------------------------------------
1096  help()
1097 *--------------------------------------------------------------------*/
1098 void help()
1099 {
1100  fprintf(stderr,"================================================================================\n");
1101  fprintf(stderr," Usds: Universal Source Decoder for Space\n");
1102  fprintf(stderr," %s\n", copyright+5);
1103  fprintf(stderr," UNM, Microelectronics Research Center, Albuquerque, New Mexico 87131 USA\n");
1104  fprintf(stderr,"%s\n",noncommercial1);
1105  fprintf(stderr,"%s\n",noncommercial2);
1106  fprintf(stderr,"===============================================================================\n");
1107  fprintf(stderr,"%s\n",credits1);
1108  fprintf(stderr,"%s\n",credits2);
1109  fprintf(stderr,"%s\n",credits3);
1110  fprintf(stderr,"================================================================================\n");
1111  fprintf(stderr,"\nUsds -n <N> -s <scanline_length> [-j <J>][-rr][-help][options] file\n\n");
1112  fprintf(stderr," -n : <N> Followed by quantization in bits per sample. Default is 8.\n");
1113  fprintf(stderr," -s : Followed by Scanline length. (Defines pixels per reference.)\n");
1114  fprintf(stderr," Default Scanline length is 256.\n");
1115  fprintf(stderr," -j : <J> Blocksize. Optional. Default is 16 pixels per block. J=[8,10,16].\n");
1116  fprintf(stderr," -rr : Optional. Reference to Reference differencing. Typically improves\n");
1117  fprintf(stderr," compression by exploiting correlation between ajacent references.\n");
1118  fprintf(stderr," Only supported in nearest neighbor 2D, and 2L modes\n");
1119  fprintf(stderr," -help : usage is: 'uses -help' to get more options and examples\n");
1120  fprintf(stderr," file : Encoded file name without the '.E' extension\n");
1121  fprintf(stderr," Must decode with same options used for encoding\n");
1122  fprintf(stderr,"Output : Output is binary file 'file.D'..should be same as original.\n");
1123 
1124  fprintf(stderr,"\nDATA FORMAT options: [-neg][-pos]\n");
1125  fprintf(stderr," -neg : Specify data is two's complement.\n");
1126  fprintf(stderr," -pos : Positive data. This is the default and need not be specified.\n");
1127  fprintf(stderr,"\nSIMPLE OPERATING MODES: [-nn ][-ec][-by]\n");
1128  fprintf(stderr," -nn : Typical mode. Default. Nearest Neighbor prediction.\n");
1129  fprintf(stderr," -ec : Entropy Coding for positive data. No DPCM, mapping, or references.\n");
1130  fprintf(stderr," -by : Bypass. Packetize input to 16 bits. No coding, no IDs or References.\n");
1131 
1132  fprintf(stderr,"\nOPERATING MODES REQUIRING PREDICTOR FILE: [-2d][-2L][-ep]\n");
1133  fprintf(stderr," These modes require another file the same size as the original\n");
1134  fprintf(stderr," image. The second binary file contains predictor values that are\n");
1135  fprintf(stderr," used in coding the original image. The Decoder also needs the\n");
1136  fprintf(stderr," predictor file when decoding in these modes.\n");
1137  fprintf(stderr," -2d : 2D prediction. Avg of previous X and external prediction.\n");
1138  fprintf(stderr," Predictors must be in binary file 'file.2D'.\n");
1139  fprintf(stderr," -2L : 2D prediction only uses nearest neighbor prediction for first line.\n");
1140  fprintf(stderr," First line of file.2D is ignored to allow nearest neighbor prediction.\n");
1141  fprintf(stderr," -ep : External Prediction. Predictors must be binary file file.EP.\n");
1142  fprintf(stderr," -ms : Multi-spectral option. Predictors must be binary file.MS.\n");
1143  fprintf(stderr,"\nCHIP SPECIFIC:\n");
1144  fprintf(stderr," -br : <Blocks per reference>. Default is 16 blocks. Typically,\n");
1145  fprintf(stderr," insert a reference every scanline.\n");
1146  fprintf(stderr," -rp : <Refs per packet>.(OPTIONAL). If this arg. is not used, uses assumes\n");
1147  fprintf(stderr," continuous mode til EOF (and ends on a byte). Packets are coded\n");
1148  fprintf(stderr," to a 16 bit boundary, but stats are for 16 and 8 bit conditions.\n");
1149  fprintf(stderr,"\nDECODER SPECIFIC OPTION:\n");
1150  fprintf(stderr,"-nosign: Do NOT sign extend output when data is negative.\n");
1151  fprintf(stderr," Sign extending to nearest byte is the default for negative data.\n");
1152  fprintf(stderr,"================================================================================\n");
1153  fprintf(stderr," NOTE: you must decode using the same options used when encoding.\n");
1154  fprintf(stderr," %s\n", copyright+5);
1155 
1156  fprintf(stderr," %s\n",sccs_id+5);
1157  fprintf(stderr,"================================================================================\n");
1158 }
1159 /*--------------------------------------------------------------------
1160  set_flags()
1161 *--------------------------------------------------------------------*/
1162 void set_flags(argc,argv,pixref_flag,blkref_flag)
1163 int argc;
1164 char *argv[];
1165 int *blkref_flag,*pixref_flag;
1166 {
1167  int tempc;
1168 
1169  if (argc < 2)
1170  {
1171  help();
1172  (void)exit(1);
1173  }
1174 
1175  *blkref_flag=0;
1176  *pixref_flag=0;
1177  BYPASS_flag=0;
1178  N=N_DEFAULT;
1179  J=J_DEFAULT;
1180  if (N <= 8)
1181  IDbits=3;
1182  else
1183  IDbits=4;
1185  BlkRef=(PixRef+J-1)/J;
1186  BYTE_pixel=1;
1187  NN_mode=1;
1188  EP_mode=0;
1189  SCANLINE=BlkRef*J;
1190  Kfactor=1; /* id-Kfactor = ksplit_bits */
1191 
1192  EXT2_flag=1;
1193  EXT2_bit=0;
1194  ZOP_flag=1;
1195  Low_id=0;
1196  FS_id=1;
1197  D_id=15;
1198 
1199  argc--;
1200  tempc=0;
1201  while (++tempc < argc)
1202  {
1203  if (strcmp(argv[tempc],"-help")==0)
1204  {
1205  help();
1206  (void)exit(1);
1207  }
1208  else if (strcmp(argv[tempc],"-ec")==0)
1209  {
1210  ENT_CODING=1;
1211  EP_mode=1;
1212  NN_mode=0; /* no reference */
1213  }
1214  else if (strcmp(argv[tempc],"-by")==0)
1215  {
1216  BYPASS_mode=1;
1217  NN_mode=0;
1218  }
1219  else if (strcmp(argv[tempc],"-neg")==0)
1220  NEG_data=1;
1221  else if (strcmp(argv[tempc],"-pos")==0)
1222  NEG_data=0;
1223  else if (strcmp(argv[tempc],"-nn")==0)
1224  {
1225  NN_mode=1;
1226  TWOD_mode=0;
1227  EP_mode=0;
1228  }
1229  else if (strcmp(argv[tempc],"-2d")==0)
1230  {
1231  TWOD_mode=1;
1232  NN_mode=0;
1233  }
1234  else if (strcmp(argv[tempc],"-2L")==0)
1235  {
1236  TWOD_line_mode=1;
1237  TWOD_mode=1;
1238  NN_mode=0;
1239  }
1240  else if (strcmp(argv[tempc],"-ep")==0)
1241  {
1242  EP_mode=1;
1243  NN_mode=0;
1244  BYPASS_flag=0;
1245  }
1246  else if (strcmp(argv[tempc],"-ms")==0)
1247  {
1248  MS_mode=1;
1249  NN_mode=0;
1250  }
1251  else if (strcmp(argv[tempc],"-n")==0)
1252  {
1253  tempc++;
1254  sscanf(argv[tempc],"%d",&N);
1255  }
1256  else if (strcmp(argv[tempc],"-j")==0)
1257  {
1258  tempc++;
1259  sscanf(argv[tempc],"%d",&J);
1260  }
1261  else if (strcmp(argv[tempc],"-stats")==0)
1262  ; /* do nothing: just allow same flags as is in encoder */
1263  else if (strcmp(argv[tempc],"-chip")==0)
1264  ; /* do nothing: just allow same flags as is in encoder */
1265  else if (strcmp(argv[tempc],"-pf")==0)
1266  ; /* do nothing: just allow same flags as is in encoder */
1267  else if ((strcmp(argv[tempc],"-refref")==0)||
1268  (strcmp(argv[tempc],"-rr")==0))
1269  REFREF_flag=1;
1270  else if (strcmp(argv[tempc],"-bits")==0)
1271  ; /* do nothing: just allow same flags as is in encoder */
1272  else if ((strcmp(argv[tempc],"-refpac")==0)||
1273  (strcmp(argv[tempc],"-rp")==0))
1274  {
1275  tempc++;
1276  sscanf(argv[tempc],"%d",&RefPac);
1277  PACKET_flag=1;
1278  if (RefPac==0)
1279  RefPac=128;
1280  }
1281  else if ((strcmp(argv[tempc],"-blkref")==0)||
1282  (strcmp(argv[tempc],"-br")==0))
1283  {
1284  tempc++;
1285  sscanf(argv[tempc],"%d",&BlkRef);
1286  if (BlkRef==0)
1287  BlkRef=128;
1288  *blkref_flag=1;
1289  }
1290  else if (strcmp(argv[tempc],"-s")==0)
1291  {
1292  tempc++;
1293  sscanf(argv[tempc],"%d",&PixRef);
1294  *pixref_flag=1;
1295  }
1296  else if ((strcmp(argv[tempc],"-pixref")==0)||
1297  (strcmp(argv[tempc],"-pr")==0)) /* to be backward compatible */
1298  {
1299  tempc++;
1300  sscanf(argv[tempc],"%d",&PixRef);
1301  *pixref_flag=1;
1302  }
1303  else if ((strcmp(argv[tempc],"-nosign")==0))
1304  SIGN_EXT_OUTPUT=0;/*NO sign extend output to nearest next byte*/
1305  else
1306  {
1307  fprintf(stderr,"ERROR: %s is not a valid argument. Aborting\n",argv[tempc]);
1308  (void)exit(1);
1309  }
1310  }/*while*/
1311 
1312  // open_files(argc,argv);
1313 
1314 }
1315 /*****************************************************************************
1316  OPEN FILES
1317 *****************************************************************************/
1318 /*--------------------------------------------------------------------
1319 open_files()
1320 *--------------------------------------------------------------------*/
1321 void open_files(argc,argv)
1322 int argc;
1323 char *argv[];
1324 {
1325  char out_file[80],ep_file[80],twod_file[80],ms_file[80];
1326 
1327  /* files */
1328  sprintf(In_file,"%s.E",argv[argc]); /* binary compressed pixel data */
1329  sprintf(out_file,"%s.D",argv[argc]);/* Decompressed binary file */
1330  sprintf(ep_file,"%s.EP",argv[argc]); /* external predictor input file*/
1331  sprintf(twod_file,"%s.2D",argv[argc]);/* external predictor input file*/
1332  sprintf(ms_file,"%s.MS",argv[argc]); /* multispectral pred. file */
1333 
1334  // if ((In_ptr=fopen(In_file,"rb"))==NULL)
1335  //{
1336  // fprintf(stderr,"ERROR: not able to open file %s.\n",In_file);
1337  // (void)exit(1);
1338  //}
1339 
1340  if ((Out_ptr=fopen(out_file,"wb"))==NULL)
1341  {
1342  fprintf(stderr,"ERROR: not able to open file %s.\n",out_file);
1343  (void)exit(1);
1344  }
1345  if (!ENT_CODING)
1346  {
1347  if ((EP_mode)&&((Ep_ptr=fopen(ep_file,"rb")) == NULL))
1348  {
1349  fprintf(stderr,"ERROR: External predictor file %s not found.\n",ep_file);
1350  (void)exit(1);
1351  }
1352  if ((TWOD_mode)&&((Twod_ptr=fopen(twod_file,"rb")) == NULL))
1353  {
1354  fprintf(stderr,"ERROR: External predictor file %s not found.\n",twod_file);
1355  (void)exit(1);
1356  }
1357  if ((MS_mode)&&((Ms_ptr=fopen(ms_file,"rb")) == NULL))
1358  {
1359  fprintf(stderr,"ERROR: External predictor file %s not found.\n",
1360  ms_file);
1361  (void)exit(1);
1362  }
1363  }
1364 }
1365 /*****************************************************************************
1366  INITIALIZING
1367 *****************************************************************************/
1368 /*-------------------------------------------------------------------
1369 initialize()
1370 *--------------------------------------------------------------------*/
1371 void initialize(pixref_flag,blkref_flag)
1372 int pixref_flag,blkref_flag;
1373 {
1374  int i;
1375 
1376  if ((EP_mode)||(BYPASS_mode))
1377  REFREF_flag=0; /* idiot proofing.. inhibit ref2ref differences */
1378 
1379  if (BYPASS_mode)
1380  {
1381  IDbits=0;
1382  ENT_CODING=0;
1383  NEG_data=0;
1384  }
1385  /*--------------*/
1386  if (pixref_flag)
1387  BlkRef=(int)(PixRef+J-1)/J;
1388  else
1389  {
1390  PixRef=BlkRef*J;
1391  SCANLINE=BlkRef*J;
1392  }
1393 
1394  check_ref_per_sample(blkref_flag,pixref_flag);
1395 
1396  if (N>8 )
1397  {
1398  BYTE_pixel=2;
1399  IDbits=4;
1400  }
1401  else
1402  D_id=7; /* only change when default is chosen over k=6 */
1403 
1404 
1405  /* define MASKING array */
1406  MASKNOT[0]=0;
1407  for (i=1;i<=16;i++)
1408  MASKNOT[i]=(1<<i)-1;
1409 
1410  MASKN_neg=0; /* will be ORed with output */
1411  if (NEG_data)
1412  {
1413  XMAXneg=1<<(N-1);
1414  MASK_SIGN=(1<<(N-1));
1415  if (SIGN_EXT_OUTPUT)
1416  MASKN_neg=(~MASKNOT[N]);
1417  }
1418  else
1419  {
1420  XMAXneg=0;
1421  MASK_SIGN=0;
1422  }
1423  if (MS_mode)
1424  {
1425  NN=N+1;
1426  if (NN>8)
1427  {
1428  IDbits=4;
1429  D_id=15;
1430  }
1431  }
1432  else
1433  NN=N;
1434  XMAX_MS=(1<<NN)-1;
1435  XMAX=(1<<N)-1; /* max pos integer before level shifting back to negative*/
1436 
1437  MASK_IN=MASKNOT[N];
1438 
1439  Bptr= &Buffer_out[0];
1440  Bend= &Buffer_out[0]+sizeof(Buffer_out);
1441  REF= ((!EP_mode)&&(!BYPASS_mode));
1442  Binptr= &Buffer_in[0];
1443  More_data = 1;
1444  end_fill = 0;
1445  IN_INDEX = 0;
1446  NEXT_BIT = 15;
1447 }
1448 /*---------------------------------------------------------------------------
1449 check_ref_per_sample()
1450 ---------------------------------------------------------------------------*/
1451 void check_ref_per_sample(blkref_flag,pixref_flag)
1452 int blkref_flag,pixref_flag;
1453 {
1454  if ((blkref_flag==1)&&(pixref_flag==1))
1455  {
1456  fprintf(stderr,"ERROR:%s either blkref or pixref may be specified but not both. Aborting.\n",In_file);
1457  (void)exit(1);
1458  }
1459 
1460  SCANLINE=BlkRef*J;
1461  if (PACKET_flag)
1463  /*--------- error checking on samples_per_ref--------------*/
1464  if (((PixRef % J) !=0)||(PixRef < J))
1465  fprintf(stderr,"WARNING: %d not multiple of J=%d. Assuming encoder filled out scanline.\n",PixRef,J);
1466 }
an array had not been initialized Several spelling and grammar corrections were which is read from the appropriate MCF the above metadata values were hard coded A problem calculating the average background DN for SWIR bands when the moon is in the space view port was corrected The new algorithm used to calculate the average background DN for all reflective bands when the moon is in the space view port is now the same as the algorithm employed by the thermal bands For non SWIR changes in the averages are typically less than Also for non SWIR the black body DNs remain a backup in case the SV DNs are not available For SWIR the changes in computed averages were larger because the old which used the black body suffered from contamination by the micron leak As a consequence of the if SV DNs are not available for the SWIR the EV pixels will not be the granule time is used to identify the appropriate tables within the set given for one LUT the first two or last two tables respectively will be used for the interpolation If there is only one LUT in the set of it will be treated as a constant LUT The manner in which Earth View data is checked for saturation was changed Previously the raw Earth View DNs and Space View DNs were checked against the lookup table values contained in the table dn_sat The change made is to check the raw Earth and Space View DNs to be sure they are less than the maximum saturation value and to check the Space View subtracted Earth View dns against a set of values contained in the new lookup table dn_sat_ev The metadata configuration and ASSOCIATEDINSTRUMENTSHORTNAME from the MOD02HKM product The same metatdata with extensions and were removed from the MOD021KM and MOD02OBC products ASSOCIATEDSENSORSHORTNAME was set to MODIS in all products These changes are reflected in new File Specification which users may consult for exact the pow functions were eliminated in Emissive_Cal and Emissive bands replaced by more efficient code Other calculations throughout the code were also made more efficient Aside from a few round off there was no difference to the product The CPU time decreased by about for a day case and for a night case A minor bug in calculating the uncertainty index for emissive bands was corrected The frame index(0-based) was previously being used the frame number(1-based) should have been used. There were only a few minor changes to the uncertainty index(maximum of 1 digit). 3. Some inefficient arrays(Sigma_RVS_norm_sq) were eliminated and some code lines in Preprocess_L1A_Data were moved into Process_OBCEng_Emiss. There were no changes to the product. Required RAM was reduced by 20 MB. Now
void un_DEF()
Definition: Usds.c:596
unsigned short OUT[4096+1]
Definition: Usds.c:55
int j
Definition: decode_rs.h:73
#define MAX_ZBLKS
Definition: Usds.c:38
void set_flags(int argc, char *argv[], int *pixref_flag, int *blkref_flag)
int NN_mode
Definition: Usds.c:79
int findid(int max_data)
Definition: Usds.c:758
long BLKCNT
Definition: Usds.c:104
int K_bits
Definition: Usds.c:103
unsigned IN_array[INBLK_size+1]
Definition: Usds.c:49
int XMAXneg
Definition: Usds.c:99
int get_input(int *max_data, int32_t inBytes, uint8_t *encryptData)
Definition: Usds.c:797
int PACKET_flag
Definition: Usds.c:90
char * Bend
Definition: Usds.c:58
int PixRef
Definition: Usds.c:87
int BYPASS_flag
Definition: Usds.c:78
int un_z()
Definition: Usds.c:392
int N
Definition: Usds.c:60
void flush_buffer(uint16_t *decryptData, int32_t *decryptPtr)
Definition: Usds.c:1082
#define NULL
Definition: decode_rs.h:63
int MS_mode
Definition: Usds.c:83
FILE * Twod_ptr
Definition: Usds.c:100
void initialize(int pixref_flag, int blkref_flag)
Definition: Usds.c:1371
void check_ref_per_sample(int blkref_flag, int pixref_flag)
Definition: Usds.c:1451
int NN
Definition: Usds.c:61
float32 * pos
Definition: l1_czcs_hdf.c:35
FILE * Ms_ptr
Definition: Usds.c:100
int SCANLINE
Definition: Usds.c:74
int NEXT_BIT
Definition: Usds.c:77
int check_end(int id, int end_fill)
Definition: Usds.c:885
int IN_INDEX
Definition: Usds.c:76
int MASK_SIGN
Definition: Usds.c:64
int FS_id
Definition: Usds.c:105
#define ZERO
Definition: Usds.c:34
int usds(int argc, argv, int32_t inBytes, uint8_t *encryptData, uint16_t *decryptData)
Definition: Usds.c:154
int BYPASS_mode
Definition: Usds.c:84
char copyright[]
Definition: Usds.c:109
char * Binptr
Definition: Usds.c:53
int J
Definition: Usds.c:62
void unmap_NN(int start, int pixels)
Definition: Usds.c:632
int RefPac
Definition: Usds.c:89
unsigned short MASKNOT[17]
Definition: Usds.c:67
int XMAX
Definition: Usds.c:97
void open_files(int argc, char *argv[])
#define INBLK_size
Definition: Usds.c:31
unsigned SIG[2048+1]
Definition: Usds.c:54
int REFREF_flag
Definition: Usds.c:72
int BYTE_pixel
Definition: Usds.c:75
int Kfactor
Definition: Usds.c:103
int REF2REF
Definition: Usds.c:71
int IDbits
Definition: Usds.c:69
int BlkPac
Definition: Usds.c:86
int EXT2_flag
Definition: Usds.c:91
char Buffer_in2[10000]
Definition: Usds.c:52
char Buffer_in[INBLK_size *2+2]
Definition: Usds.c:51
FILE * Ep_ptr
Definition: Usds.c:100
int ZBLKS
Definition: Usds.c:94
FILE * Out_ptr
Definition: Usds.c:100
int EP_mode
Definition: Usds.c:80
void data_output(uint16_t *decryptData, int *decryptPtr)
Definition: Usds.c:986
int Low_id
Definition: Usds.c:105
a context in which it is NOT documented to do so subscript which cannot be easily calculated when extracting TONS attitude data from the Terra L0 files Corrected several defects in extraction of entrained ephemeris and and as HDF file for both the L1A and Geolocation enabling retrieval of South Polar DEM data Resolved Bug by changing to opent the geolocation file only after a successful read of the L1A and also by checking for fatal errors from not restoring C5 and to report how many of those high resolution values were water in the new WaterPresent SDS Added valid_range attribute to Land SeaMask Changed to bilinearly interpolate the geoid_height to remove artifacts at one degree lines Made corrections to const qualification of pointers allowed by new version of M API library Removed casts that are no longer for same not the geoid Corrected off by one error in calculation of high resolution offsets Corrected parsing of maneuver list configuration parameter Corrected to set Height SDS to fill values when geolocation when for elevation and land water mask
Definition: HISTORY.txt:114
void do_uncoding(int32_t inBytes, uint8_t *encryptData, uint16_t *decryptData)
Definition: Usds.c:175
int M[]
Definition: Usds.c:107
int TRUEREF
Definition: Usds.c:73
void help()
Definition: Usds.c:1098
int NEG_data
Definition: Usds.c:68
#define TYP
Definition: Usds.c:32
data_t b[NROOTS+1]
Definition: decode_rs.h:77
int XP
Definition: Usds.c:63
void un_FS(int flag)
Definition: Usds.c:480
int BlkRef
Definition: Usds.c:88
void un_ext2()
Definition: Usds.c:457
int ZFLAG
Definition: Usds.c:95
int get_EP_IN(FILE *file_ptr, int pixels)
Definition: Usds.c:938
int MASK_IN
Definition: Usds.c:65
char In_file[MAXLINE]
Definition: Usds.c:101
#define J_DEFAULT
Definition: Usds.c:42
void unmap_TWOD(int start, int pixels)
Definition: Usds.c:680
void unmap_EP(int start, int pixels)
Definition: Usds.c:656
int SIGN_EXT_OUTPUT
Definition: Usds.c:47
int get_ref()
Definition: Usds.c:264
#define PIXREF_DEFAULT
Definition: Usds.c:40
int TWOD_line_mode
Definition: Usds.c:82
#define MAXLINE
Definition: Usds.c:30
#define EXT2
Definition: Usds.c:33
char * Bptr
Definition: Usds.c:57
int EXT2_bit
Definition: Usds.c:92
int get_id4()
Definition: Usds.c:348
char Buffer_out[10240]
Definition: Usds.c:56
int MASKN_neg
Definition: Usds.c:66
void unmap_MS(int start, int pixels)
Definition: Usds.c:719
int D_id
Definition: Usds.c:105
int i
Definition: decode_rs.h:71
int TWOD_mode
Definition: Usds.c:81
unsigned REFERENCE
Definition: Usds.c:102
int ZOP_flag
Definition: Usds.c:93
int XMAX_MS
Definition: Usds.c:98
int LAST_word
Definition: Usds.c:96
int MS[]
Definition: Usds.c:106
int REF
Definition: Usds.c:70
void un_K()
Definition: Usds.c:533
int get_id3()
Definition: Usds.c:303
float p[MODELMAX]
Definition: atrem_corl1.h:131
#define J_MAX
Definition: Usds.c:39
unsigned XP_IN[INBLK_size+1]
Definition: Usds.c:50
#define N_DEFAULT
Definition: Usds.c:41
FILE * In_ptr
Definition: Usds.c:100
int ENT_CODING
Definition: Usds.c:85