USGS

Isis 3.0 Application Source Code Reference

Home

initBlock.cpp

Go to the documentation of this file.
00001 /*
00002 NOTICE
00003 
00004 The software accompanying this notice (the "Software") is provided to you
00005 free of charge to facilitate your use of the data collected by the Mars
00006 Orbiter Camera (the "MOC Data").  Malin Space Science Systems ("MSSS")
00007 grants to you (either as an individual or entity) a personal,
00008 non-transferable, and non-exclusive right (i) to use and reproduce the
00009 Software solely for the purpose of accessing the MOC Data; (ii) to modify
00010 the source code of the Software as necessary to maintain or adapt the
00011 Software to run on alternate computer platforms; and (iii) to compile, use
00012 and reproduce the modified versions of the Software solely for the purpose
00013 of accessing the MOC Data.  In addition, you may distribute the Software,
00014 including any modifications thereof, solely for use with the MOC Data,
00015 provided that (i) you must include this notice with all copies of the
00016 Software to be distributed; (ii) you may not remove or alter any
00017 proprietary notices contained in the Software; (iii) you may not charge any
00018 third party for the Software; and (iv) you will not export the Software
00019 without the appropriate United States and foreign government licenses.
00020 
00021 You acknowledge that no title to the intellectual property in the Software
00022 is transferred to you.  You further acknowledge that title and full
00023 ownership rights to the Software will remain the exclusive property of MSSS
00024 or its suppliers, and you will not acquire any rights to the Software
00025 except as expressly set forth above.  The Software is provided to you AS
00026 IS.  MSSS MAKES NO WARRANTY, EXPRESS OR IMPLIED, WITH RESPECT TO THE
00027 SOFTWARE, AND SPECIFICALLY DISCLAIMS THE IMPLIED WARRANTIES OF
00028 NON-INFRINGEMENT OF THIRD PARTY RIGHTS, MERCHANTABILITY AND FITNESS FOR A
00029 PARTICULAR PURPOSE.  SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR
00030 LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO SUCH LIMITATIONS OR
00031 EXCLUSIONS MAY NOT APPLY TO YOU.
00032 
00033 Your use or reproduction of the Software constitutes your agreement to the
00034 terms of this Notice.  If you do not agree with the terms of this notice,
00035 promptly return or destroy all copies of the Software in your possession.
00036 
00037 Copyright (C) 1999 Malin Space Science Systems.  All Rights Reserved.
00038 */
00039 //static char *sccsid = "@(#)initBlock.c  1.1 10/04/99";
00040 #if (!defined(NOSCCSID) && (!defined(LINT)))
00041 #endif
00042 /*
00043 * DESCRIPTION
00044 *
00045 * COMMENTARY
00046 */
00047 
00048 #include <stdio.h>
00049 #include <stdlib.h>
00050 
00051 #include "fs.h"
00052 #include "limits.h"
00053 #include "encodeCoefs.static.h"
00054 
00055 #include "initBlock.h"
00056 
00057 extern void exit();
00058 
00059 BITTREE *encodeTrees[MAXCODES];
00060 
00061 static uint32 bitReverse(register uint32 num)
00062 {
00063   register uint32 rev;
00064   register uint32 i;
00065 
00066   rev = 0;
00067 
00068   for(i = 0; ; i++) {
00069     if(num & 0x1) {
00070       rev |= 0x1;
00071     };
00072 
00073     if(i == 31) {
00074       break;
00075     };
00076 
00077     num >>= 1;
00078     rev <<= 1;
00079   };
00080 
00081   return(rev);
00082 }
00083 
00084 static int compare(const void *v1, const void *v2)
00085 {
00086   BITTREE *e1 = (BITTREE *)v1;
00087   BITTREE *e2 = (BITTREE *)v2; 
00088   register uint32 c1, c2;
00089   c1 = e1->code;
00090   c2 = e2->code;
00091 
00092   if(c1 < c2) {
00093     return(-1);
00094   }
00095   else {
00096     if(c1 > c2) {
00097       return(1);
00098     }
00099     else {
00100       return(0);
00101     };
00102   };
00103 }
00104 
00105 BITTREE *makeTree(BITTREE *start, uint32 size, uint32 bit)
00106 {
00107   BITTREE *cur;
00108   uint32 count;
00109   BITTREE *scan;
00110 
00111   if(size == 1) {
00112     cur = start;
00113   }
00114   else {
00115     if((cur = (BITTREE *)malloc(sizeof(*cur))) == NULL) {
00116       (void)fprintf(stderr, "Not enough memory for huffman trees\n");
00117       exit(1);
00118     };
00119 
00120     for(count = 0, scan = start; count < size && (scan->code & bit) == 0; count++, scan++) {
00121     };
00122 
00123     cur->zero = makeTree(start, count, bit << 1);
00124     cur->one  = makeTree(start + count, size - count, bit << 1);
00125   };
00126 
00127   return(cur);
00128 }
00129 
00130 void initBlock() {
00131   uint32 which, n;
00132   uint32 size;
00133   uint8 *count;
00134   uint32 *encoding;
00135   BITTREE *curTree;
00136 
00137   for(which = 0; which < MAXCODES; which++) {
00138     BITTREE *scanTree;
00139 
00140     size = sizes[which];
00141     count = counts[which];
00142     encoding = encodings[which];
00143 
00144     if((encodeTrees[which] = (BITTREE *)malloc((uint32)(size * sizeof(*encodeTrees[which])))) == NULL) {
00145       (void)fprintf(stderr, "Not enough memory for huffman trees\n");
00146       exit(1);
00147     };
00148 
00149     curTree = encodeTrees[which];
00150     scanTree = curTree;
00151 
00152     for(n = 0; n < size; n++, scanTree++) {
00153       if(n == 0) {
00154         scanTree->value = LARGE_NEGATIVE;
00155       }
00156       else {
00157         if(n == size - 1) {
00158           scanTree->value = LARGE_POSITIVE;
00159         }
00160         else {
00161           scanTree->value = n - size / 2;
00162         };
00163       };
00164       scanTree->count = count[n];
00165       scanTree->code  = bitReverse(encoding[n]);
00166       scanTree->zero  = NULL;
00167       scanTree->one   = NULL;
00168     };
00169 
00170     qsort((char *)curTree, (int32)size, sizeof(*curTree), compare);
00171 
00172     scanTree = curTree;
00173 
00174     for(n = 0; n < size; n++, scanTree++) {
00175       scanTree->code  = bitReverse(scanTree->code);
00176     };
00177 
00178     encodeTrees[which] = makeTree(curTree, size, 0x1);
00179   };
00180 }
00181 
00182 void freeTree(BITTREE *p)
00183 {
00184   if(p->zero) freeTree(p->zero);
00185   if(p->one) freeTree(p->one);
00186   free(p);
00187 }
00188 
00189 void dumpTree(BITTREE *p, int top)
00190 {
00191   if(top) printf("dumping root ");
00192   if(p->zero) dumpTree(p->zero, 0);
00193   if(p->one) dumpTree(p->one, 0);
00194   //printf("%#x(%#x,%#x)", p, p->zero, p->one);
00195   if(top) putchar('\n');
00196 }
00197 
00198 void freeAllTrees() {
00199   int i;
00200 
00201   for(i = 0; i < MAXCODES; i++) {
00202     if(0 && encodeTrees[i]) freeTree(encodeTrees[i]);
00203     if(encodeTrees[i]) free(encodeTrees[i]);
00204   }
00205 }
00206 
00207 /* seems like maybe the standard Solaris malloc has a bug in it, since this crashes in freeTree, not dumpTree, indicating corruption during the freeing process.   Actually, looking at the code maybe it's just our bug, since it looks like we free stuff that was never allocated. */