00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00080 #include <stdlib.h>
00081 #include <stdio.h>
00082 #include <assert.h>
00083 #include "desc.h"
00084 #include "bufr.h"
00085 #include "rlenc.h"
00086
00087 #define LBUFLEN 5000
00089 #define ENCBUFL 5000
00092
00093
00111 int rlenc (char* infile, int nrows, int ncols, varfl** vals, size_t* nvals)
00112
00113 {
00114 FILE *fp;
00115 unsigned char buf[LBUFLEN];
00116 int i, n;
00117
00118
00119
00120
00121 assert (ncols <= LBUFLEN);
00122
00123
00124
00125 fp = fopen (infile, "rb");
00126 if (fp == NULL) {
00127 fprintf (stderr, "error opening '%s'\n", infile);
00128 return 0;
00129 }
00130
00131
00132
00133 val_to_array (vals, (varfl) nrows, nvals);
00134
00135
00136
00137 for (i = 0; i < nrows; i ++) {
00138 n = fread (buf, 1, ncols, fp);
00139 if (n != ncols) {
00140 fprintf (stderr, "read error from file '%s'\n", infile);
00141 goto err;
00142 }
00143 if (!rlenc_compress_line (i, buf, ncols, vals, nvals)) goto err;
00144 }
00145 fclose (fp);
00146 return 1;
00147
00148 err:
00149 fclose (fp);
00150 return 0;
00151 }
00152
00153
00154
00172 int rlenc_compress_line (int line, unsigned char* src, int ncols,
00173 varfl** dvals, size_t* nvals)
00174
00175
00176 {
00177 int count, i, n, npar, lens[LBUFLEN], cw, ncgi, nngi = 0;
00178 unsigned char val, lval = 0, vals[LBUFLEN];
00179 varfl encbuf[ENCBUFL];
00180
00181
00182
00183 count = n = 0;
00184 for (i = 0; i < ncols; i ++) {
00185 val = *(src + i);
00186 if (i != 0 && (val != lval || count >= 255)) {
00187 lens[n] = count;
00188 vals[n] = lval;
00189 n ++;
00190 count = 0;
00191 lval = val;
00192 }
00193 lval = val;
00194 count ++;
00195 }
00196 lens[n] = count;
00197 vals[n] = lval;
00198 n ++;
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 npar = 0;
00209 for (i = 0; i < n - 1; i ++) if (lens[i] == 1 && lens[i+1] > 1) npar ++;
00210 npar ++;
00211
00212
00213
00214 for (i = 0; i < ENCBUFL; i ++) encbuf[i] = (varfl) 0.0;
00215 cw = 0;
00216 encbuf[cw++] = line;
00217
00218
00219
00220 encbuf[cw++] = npar;
00221
00222 ncgi = cw ++;
00223 encbuf[ncgi] = (varfl) 0.0;
00224
00225 i = 0;
00226 for (i = 0; i < n; i ++) {
00227 if (lens[i] > 1) {
00228 if (i > 0 && lens[i-1] == 1) {
00229 ncgi = cw ++;
00230 encbuf[ncgi] = (varfl) 0.0;
00231 }
00232 encbuf[ncgi] += 1.0;
00233 encbuf[cw++] = lens[i];
00234 encbuf[cw++] = vals[i];
00235 }
00236 else {
00237 if (i == 0 || lens[i-1] != 1) {
00238 nngi = cw ++;
00239 encbuf[nngi] = (varfl) 0.0;
00240 }
00241 encbuf[nngi] += 1.0;
00242 encbuf[cw++] = vals[i];
00243 }
00244 }
00245 if (lens[n-1] != 1) encbuf[cw++] = 0;
00246 assert (cw <= ENCBUFL);
00247
00248
00249
00250
00251 for (i = 0; i < cw; i ++)
00252 if (!val_to_array (dvals, encbuf[i], nvals)) return 0;
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 return 1;
00275 }
00276
00277
00295 int rldec (char* outfile, varfl* vals, size_t* nvals)
00296
00297 {
00298 FILE *fp;
00299 int i, j, k, l, ngr, nrows, npar, val, count, nup;
00300 varfl *ovals;
00301
00302
00303
00304 fp = fopen (outfile, "wb");
00305 if (fp == NULL) return 0;
00306
00307
00308
00309 ovals = vals;
00310 nrows = (int) *vals ++;
00311 for (i = 0; i < nrows; i ++) {
00312 vals ++;
00313 npar = (int) *vals ++;
00314 for (j = 0; j < npar; j ++) {
00315 ngr = (int) *vals ++;
00316 for (k = 0; k < ngr; k ++) {
00317 count = (int) *vals ++;
00318 val = (int) *vals ++;
00319 for (l = 0; l < count; l ++) {
00320 fputc (val, fp);
00321 }
00322 }
00323 nup = (int) *vals ++;
00324 for (k = 0; k < nup; k ++) {
00325 val = (int) *vals ++;
00326 fputc (val, fp);
00327 }
00328 }
00329 }
00330
00331
00332
00333 fclose (fp);
00334
00335
00336
00337 *nvals = vals - ovals;
00338 return 1;
00339 }
00340
00341
00342
00343
00344
00374 int rlenc_from_file (char* infile, int nrows, int ncols, varfl* *vals,
00375 int *nvals, int depth)
00376
00377 {
00378 FILE *fp;
00379 unsigned char cbuf[LBUFLEN * 2];
00380 unsigned int ibuf[LBUFLEN];
00381 int i, n, j;
00382
00383 if (depth > 2) {
00384 fprintf (stderr,
00385 "Unsupported number of bits per bixel!\n");
00386 return 0;
00387 }
00388
00389
00390
00391
00392 if (ncols > LBUFLEN) {
00393 fprintf (stderr, "ERROR: Number of columns larger than %d!\n",
00394 LBUFLEN);
00395 return 0;
00396 }
00397
00398
00399
00400 fp = fopen (infile, "rb");
00401 if (fp == NULL) {
00402 fprintf (stderr, "error opening '%s'\n", infile);
00403 return 0;
00404 }
00405
00406
00407
00408 bufr_val_to_array (vals, (varfl) nrows, nvals);
00409
00410
00411
00412 for (i = 0; i < nrows; i ++) {
00413
00414
00415
00416 n = fread (cbuf, 1, ncols * depth, fp);
00417 if (n != ncols * depth) {
00418 fprintf (stderr, "read error from file '%s'\n", infile);
00419 fclose (fp);
00420 return 0;
00421 }
00422
00423
00424
00425 if (depth == 1) {
00426
00427 for (j = 0; j < ncols; j ++)
00428 ibuf[j] = (unsigned int) cbuf[j];
00429 } else {
00430 for (j = 0; j < ncols; j++)
00431 ibuf[j] = (cbuf[j*2] << 8) + cbuf[j*2+1];
00432 }
00433
00434
00435
00436 if (!rlenc_compress_line_new (i, ibuf, ncols, vals, nvals)) {
00437 fclose (fp);
00438 return 0;
00439 }
00440 }
00441 fclose (fp);
00442 return 1;
00443 }
00444
00445
00469 int rldec_to_file (char* outfile, varfl* vals, int depth, int* nvals)
00470
00471 {
00472 FILE *fp;
00473 int i, j, nrows, ncols, nc, nv;
00474 unsigned int ibuf[LBUFLEN];
00475 unsigned char cbuf[LBUFLEN*2];
00476 varfl *ovals;
00477
00478 if (depth > 2) {
00479 fprintf (stderr,
00480 "Unsupported number of bits per bixel!\n");
00481 return 0;
00482 }
00483
00484
00485
00486 fp = fopen (outfile, "wb");
00487 if (fp == NULL) {
00488 fprintf (stderr, "Could not open file %s!\n", outfile);
00489 return 0;
00490 }
00491
00492 ovals = vals;
00493
00494
00495
00496 rldec_get_size (vals, &nrows, &ncols);
00497
00498
00499
00500
00501 if (ncols > LBUFLEN) {
00502 fprintf (stderr, "ERROR: Number of columns larger than %d!\n",
00503 LBUFLEN);
00504 return 0;
00505 }
00506
00507
00508
00509 *nvals = 0;
00510 vals ++;
00511 (*nvals) ++;
00512
00513
00514
00515 for (i = 0; i < nrows; i ++) {
00516
00517
00518
00519 rldec_decompress_line (vals, ibuf, &nc, &nv);
00520
00521
00522
00523 if (nc != ncols) {
00524 fprintf (stderr, "Error in run-length decoding!\n");
00525 fclose (fp);
00526 return 0;
00527 }
00528
00529
00530
00531 vals += nv;
00532 (*nvals) += nv;
00533
00534
00535
00536 if (depth == 1) {
00537 for (j = 0; j < ncols; j ++)
00538 cbuf[j] = (unsigned char) ibuf[j];
00539 } else {
00540 for (j = 0; j < ncols; j++) {
00541 cbuf[j*2] = (unsigned char) ((ibuf[j] >> 8) & 0xff);
00542 cbuf[j*2+1] = (unsigned char) (ibuf[j] & 0xff);
00543 }
00544 }
00545
00546
00547
00548 if (fwrite (cbuf, 1, ncols * depth, fp) != (size_t) ncols * depth) {
00549 fprintf (stderr, "Write error to file '%s'\n", outfile);
00550 fclose (fp);
00551 return 0;
00552 }
00553 }
00554
00555
00556
00557
00558 fclose (fp);
00559
00560 assert (*nvals == vals - ovals);
00561 return 1;
00562 }
00563
00588 int rlenc_from_mem (unsigned short* img, int nrows, int ncols, varfl* *vals,
00589 int *nvals)
00590
00591 {
00592 unsigned int ibuf[LBUFLEN];
00593 int i, j;
00594
00595 if (img == (unsigned short*) NULL) {
00596 fprintf (stderr, "Image for rlenc not available!\n");
00597 return 0;
00598 }
00599
00600
00601
00602
00603 if (ncols > LBUFLEN) {
00604 fprintf (stderr, "ERROR: Number of columns larger than %d!\n",
00605 LBUFLEN);
00606 return 0;
00607 }
00608
00609
00610
00611 bufr_val_to_array (vals, (varfl) nrows, nvals);
00612
00613
00614
00615 for (i = 0; i < nrows; i ++) {
00616
00617
00618
00619 for (j = 0; j < ncols; j ++)
00620 ibuf[j] = (unsigned int) img[i*ncols+j];
00621
00622
00623
00624 if (!rlenc_compress_line_new (i, ibuf, ncols, vals, nvals)) {
00625 return 0;
00626 }
00627 }
00628 return 1;
00629 }
00630
00631
00652 int rldec_to_mem (varfl* vals, unsigned short* *img, int* nvals, int* nrows,
00653 int* ncols)
00654
00655 {
00656 int i, j, nc, nv;
00657 unsigned int ibuf[LBUFLEN];
00658 varfl *ovals;
00659
00660 ovals = vals;
00661
00662
00663
00664 rldec_get_size (vals, nrows, ncols);
00665
00666
00667
00668 if (*img == NULL) {
00669 *img = (unsigned short*) calloc (*nrows * *ncols,
00670 sizeof (unsigned short));
00671 if (*img == NULL) {
00672 fprintf (stderr, "Could not allacote memory for radar image!\n");
00673 return 0;
00674 }
00675 }
00676
00677
00678
00679
00680 if (*ncols > LBUFLEN) {
00681 fprintf (stderr, "ERROR: Number of columns larger than %d!\n",
00682 LBUFLEN);
00683 return 0;
00684 }
00685
00686
00687
00688 *nvals = 0;
00689 vals ++;
00690 (*nvals) ++;
00691
00692
00693
00694 for (i = 0; i < *nrows; i ++) {
00695
00696
00697
00698 rldec_decompress_line (vals, ibuf, &nc, &nv);
00699
00700
00701
00702 if (nc != *ncols) {
00703 fprintf (stderr, "Error in run-length decoding!\n");
00704 return 0;
00705 }
00706
00707
00708
00709 vals += nv;
00710 (*nvals) += nv;
00711
00712
00713
00714 for (j = 0; j < *ncols; j ++)
00715 (*img)[i * *ncols + j] = (unsigned short) ibuf[j];
00716 }
00717
00718 assert (*nvals == vals - ovals);
00719 return 1;
00720 }
00721
00722
00746 int rlenc_compress_line_new (int line, unsigned int* src, int ncols,
00747 varfl* *dvals, int *nvals)
00748
00749 {
00750 int count, i, n, npar, lens[LBUFLEN], cw, ncgi, nngi = 0;
00751 unsigned int val, lval = 0, vals[LBUFLEN];
00752 varfl encbuf[ENCBUFL];
00753
00754
00755
00756
00757 count = n = 0;
00758 for (i = 0; i < ncols; i ++) {
00759 val = *(src + i);
00760
00761
00762
00763 if (i != 0 && (val != lval || count >= 255)) {
00764 lens[n] = count;
00765 vals[n] = lval;
00766 n ++;
00767 count = 0;
00768 lval = val;
00769 }
00770 lval = val;
00771 count ++;
00772 }
00773 lens[n] = count;
00774 vals[n] = lval;
00775 n ++;
00776
00777
00778
00779
00780 npar = 0;
00781 for (i = 0; i < n - 1; i ++) if (lens[i] == 1 && lens[i+1] > 1) npar ++;
00782 npar ++;
00783
00784
00785
00786 for (i = 0; i < ENCBUFL; i ++) encbuf[i] = (varfl) 0.0;
00787 cw = 0;
00788 encbuf[cw++] = line;
00789
00790
00791
00792 encbuf[cw++] = npar;
00793
00794 ncgi = cw ++;
00795
00796 encbuf[ncgi] = (varfl) 0.0;
00797
00798 i = 0;
00799 for (i = 0; i < n; i ++) {
00800 if (lens[i] > 1) {
00801 if (i > 0 && lens[i-1] == 1) {
00802 ncgi = cw ++;
00803
00804 encbuf[ncgi] = (varfl) 0.0;
00805 }
00806 encbuf[ncgi] += 1.0;
00807 encbuf[cw++] = lens[i];
00808 encbuf[cw++] = vals[i];
00809 }
00810 else {
00811 if (i == 0 || lens[i-1] != 1) {
00812
00813
00814 nngi = cw ++;
00815
00816 encbuf[nngi] = (varfl) 0.0;
00817
00818 }
00819 encbuf[nngi] += 1.0;
00820 encbuf[cw++] = vals[i];
00821 }
00822 }
00823 if (lens[n-1] != 1) encbuf[cw++] = 0;
00824
00825 assert (cw <= ENCBUFL);
00826
00827
00828
00829 for (i = 0; i < cw; i ++) {
00830 if (!bufr_val_to_array (dvals, encbuf[i], nvals))
00831 return 0;
00832 }
00833
00834 return 1;
00835 }
00836
00837
00855 void rldec_decompress_line (varfl* vals, unsigned int* dest, int* ncols,
00856 int* nvals)
00857 {
00858 int i = 0, j, k, l, count = 0, npar, ngr, nup;
00859 unsigned int val;
00860 varfl* ovals;
00861
00862 ovals = vals;
00863 vals ++;
00864 npar = (int) *vals ++;
00865 for (j = 0; j < npar; j ++) {
00866 ngr = (int) *vals ++;
00867 for (k = 0; k < ngr; k ++) {
00868 count = (int) *vals ++;
00869 if (*vals == MISSVAL) {
00870 val = 0xFFFF;
00871 vals ++;
00872 } else {
00873 val = (unsigned int) *vals ++;
00874 }
00875 for (l = 0; l < count; l ++) {
00876 dest[i++] = val;
00877 }
00878 }
00879 nup = (int) *vals ++;
00880 for (k = 0; k < nup; k ++) {
00881 if (*vals == MISSVAL) {
00882 dest[i++] = 0xFFFF;
00883 vals ++;
00884 } else {
00885 dest[i++] = (unsigned int) *vals ++;
00886 }
00887 }
00888 }
00889
00890
00891 *nvals = vals - ovals;
00892 *ncols = i;
00893 }
00894
00895
00908 void rldec_get_size (varfl* vals, int* nrows, int* ncols)
00909 {
00910 int npar, ngr, nup, j, k, l, count;
00911
00912 *nrows = (int) *vals ++;
00913 *ncols = 0;
00914 vals ++;
00915 npar = (int) *vals ++;
00916 for (j = 0; j < npar; j ++) {
00917 ngr = (int) *vals ++;
00918 for (k = 0; k < ngr; k ++) {
00919 count = (int) *vals ++;
00920 vals ++;
00921 for (l = 0; l < count; l ++) {
00922 (*ncols) ++;
00923 }
00924 }
00925 nup = (int) *vals ++;
00926 for (k = 0; k < nup; k ++) {
00927 vals ++;
00928 (*ncols) ++;
00929 }
00930 }
00931 }
00932
00933