File: ceosrecipe.c

package info (click to toggle)
gdal 3.11.3%2Bdfsg-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 89,016 kB
  • sloc: cpp: 1,165,048; ansic: 208,864; python: 26,958; java: 5,972; xml: 4,611; sh: 3,776; cs: 2,508; yacc: 1,306; makefile: 213
file content (769 lines) | stat: -rw-r--r-- 30,420 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/******************************************************************************
 *
 * Project:  ASI CEOS Translator
 * Purpose:  CEOS field layout recipes.
 * Author:   Paul Lahaie, pjlahaie@atlsci.com
 *
 ******************************************************************************
 * Copyright (c) 2000, Atlantis Scientific Inc
 *
 * SPDX-License-Identifier: MIT
 ****************************************************************************/

#include "ceos.h"

/* Array of Datatypes and their names/values */

typedef struct
{
    const char *String;
    int Type;
} CeosStringType_t;

typedef struct
{
    int (*function)(CeosSARVolume_t *volume, const void *token);
    const void *token;
    const char *name;
} RecipeFunctionData_t;

static const CeosStringType_t CeosDataType[] = {
    {"IU1", CEOS_TYP_UCHAR},
    {"IU2", CEOS_TYP_USHORT},
    {"UI1", CEOS_TYP_UCHAR},
    {"UI2", CEOS_TYP_USHORT},
    {"CI*2", CEOS_TYP_COMPLEX_CHAR},
    {"CI*4", CEOS_TYP_COMPLEX_SHORT},
    {"CIS4", CEOS_TYP_COMPLEX_SHORT},
    {"CI*8", CEOS_TYP_COMPLEX_LONG},
    {"C*8", CEOS_TYP_COMPLEX_FLOAT},
    {"R*4", CEOS_TYP_FLOAT},
    {NULL, 0}};

static const CeosStringType_t CeosInterleaveType[] = {{"BSQ", CEOS_IL_BAND},
                                                      {" BSQ", CEOS_IL_BAND},
                                                      {"BIL", CEOS_IL_LINE},
                                                      {" BIL", CEOS_IL_LINE},
                                                      {NULL, 0}};

#define IMAGE_OPT                                                              \
    {                                                                          \
        63, 192, 18, 18                                                        \
    }
#define IMAGE_JERS_OPT                                                         \
    {                                                                          \
        50, 192, 18, 18                                                        \
    } /* Some JERS data uses this instead of IMAGE_OPT */
#define PROC_DATA_REC                                                          \
    {                                                                          \
        50, 11, 18, 20                                                         \
    }
#define PROC_DATA_REC_ALT                                                      \
    {                                                                          \
        50, 11, 31, 20                                                         \
    }
#define PROC_DATA_REC_ALT2                                                     \
    {                                                                          \
        50, 11, 31, 50                                                         \
    } /* Some cases of ERS 1, 2 */
#define DATA_SET_SUMMARY                                                       \
    {                                                                          \
        18, 10, 18, 20                                                         \
    }

/* NOTE: This seems to be the generic recipe used for most things */
static const CeosRecipeType_t RadarSatRecipe[] = {
    {CEOS_REC_NUMCHANS, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 233, 4,
     CEOS_REC_TYP_I}, /* Number of channels */
    {CEOS_REC_INTERLEAVE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 269, 4,
     CEOS_REC_TYP_A}, /* Interleaving type */
    {CEOS_REC_DATATYPE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 429, 4,
     CEOS_REC_TYP_A}, /* Data type */
    {CEOS_REC_BPR, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 0, 0,
     CEOS_REC_TYP_A}, /* For Defeault CEOS, this is done using other vals */
    {CEOS_REC_LINES, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 237, 8,
     CEOS_REC_TYP_I}, /* How many lines */
    {CEOS_REC_TBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 261, 4, CEOS_REC_TYP_I},
    {CEOS_REC_BBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 265, 4,
     CEOS_REC_TYP_I}, /* Bottom border pixels */
    {CEOS_REC_PPL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 249, 8,
     CEOS_REC_TYP_I}, /* Pixels per line */
    {CEOS_REC_LBP, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 245, 4,
     CEOS_REC_TYP_I}, /* Left Border Pixels */
    {CEOS_REC_RBP, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 257, 4,
     CEOS_REC_TYP_I}, /* Right Border Pixels */
    {CEOS_REC_BPP, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 225, 4,
     CEOS_REC_TYP_I}, /* Bytes Per Pixel */
    {CEOS_REC_RPL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 273, 2,
     CEOS_REC_TYP_I}, /* Records per line */
    {CEOS_REC_PPR, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 0, 0,
     CEOS_REC_TYP_I}, /* Pixels Per Record -- need to fill record type */
    {CEOS_REC_PDBPR, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 281, 8,
     CEOS_REC_TYP_I}, /* pixel data bytes per record */
    {CEOS_REC_IDS, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 277, 4,
     CEOS_REC_TYP_I}, /* Prefix data per record */
    {CEOS_REC_FDL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 9, 4,
     CEOS_REC_TYP_B}, /* Length of Imagry Options Header */
    {CEOS_REC_PIXORD, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 0, 0,
     CEOS_REC_TYP_I}, /* Must be calculated */
    {CEOS_REC_LINORD, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 0, 0,
     CEOS_REC_TYP_I}, /* Must be calculated */
    {CEOS_REC_PRODTYPE, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 0, 0,
     CEOS_REC_TYP_I},

    {CEOS_REC_RECORDSIZE, 1, CEOS_IMAGRY_OPT_FILE, PROC_DATA_REC, 9, 4,
     CEOS_REC_TYP_B}, /* The processed image record size */

    /* Some ERS-1 products use an alternate data record subtype2. */
    {CEOS_REC_RECORDSIZE, 1, CEOS_IMAGRY_OPT_FILE, PROC_DATA_REC_ALT, 9, 4,
     CEOS_REC_TYP_B}, /* The processed image record size */

    /* Yet another ERS-1 and ERS-2 alternate data record subtype2. */
    {CEOS_REC_RECORDSIZE, 1, CEOS_IMAGRY_OPT_FILE, PROC_DATA_REC_ALT2, 9, 4,
     CEOS_REC_TYP_B}, /* The processed image record size */

    {CEOS_REC_SUFFIX_SIZE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 289, 4,
     CEOS_REC_TYP_I},                /* Suffix data per record */
    {0, 0, 0, {0, 0, 0, 0}, 0, 0, 0} /* Last record is Zero */
};

static const CeosRecipeType_t JersRecipe[] = {
    {CEOS_REC_NUMCHANS, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 233, 4,
     CEOS_REC_TYP_I}, /* Number of channels */
    {CEOS_REC_INTERLEAVE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 269, 4,
     CEOS_REC_TYP_A}, /* Interleaving type */
    {CEOS_REC_DATATYPE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 429, 4,
     CEOS_REC_TYP_A}, /* Data type */
    {CEOS_REC_BPR, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 0, 0,
     CEOS_REC_TYP_A}, /* For Defeault CEOS, this is done using other vals */
    {CEOS_REC_LINES, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 237, 8,
     CEOS_REC_TYP_I}, /* How many lines */
    {CEOS_REC_TBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 261, 4,
     CEOS_REC_TYP_I},
    {CEOS_REC_BBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 265, 4,
     CEOS_REC_TYP_I}, /* Bottom border pixels */
    {CEOS_REC_PPL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 249, 8,
     CEOS_REC_TYP_I}, /* Pixels per line */
    {CEOS_REC_LBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 245, 4,
     CEOS_REC_TYP_I}, /* Left Border Pixels */
    {CEOS_REC_RBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 257, 4,
     CEOS_REC_TYP_I}, /* Isn't available for RadarSAT */
    {CEOS_REC_BPP, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 225, 4,
     CEOS_REC_TYP_I}, /* Bytes Per Pixel */
    {CEOS_REC_RPL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 273, 2,
     CEOS_REC_TYP_I}, /* Records per line */
    {CEOS_REC_PPR, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 0, 0,
     CEOS_REC_TYP_I}, /* Pixels Per Record -- need to fill record type */
    {CEOS_REC_PDBPR, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 281, 8,
     CEOS_REC_TYP_I}, /* pixel data bytes per record */
    {CEOS_REC_IDS, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 277, 4,
     CEOS_REC_TYP_I}, /* Prefix data per record */
    {CEOS_REC_FDL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 9, 4,
     CEOS_REC_TYP_B}, /* Length of Imagry Options Header */
    {CEOS_REC_PIXORD, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 0, 0,
     CEOS_REC_TYP_I}, /* Must be calculated */
    {CEOS_REC_LINORD, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 0, 0,
     CEOS_REC_TYP_I}, /* Must be calculated */
    {CEOS_REC_PRODTYPE, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 0, 0,
     CEOS_REC_TYP_I},

    {CEOS_REC_RECORDSIZE, 1, CEOS_IMAGRY_OPT_FILE, PROC_DATA_REC, 9, 4,
     CEOS_REC_TYP_B}, /* The processed image record size */

    {CEOS_REC_SUFFIX_SIZE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_JERS_OPT, 289, 4,
     CEOS_REC_TYP_I},                /* Suffix data per record */
    {0, 0, 0, {0, 0, 0, 0}, 0, 0, 0} /* Last record is Zero */
};

static const CeosRecipeType_t ScanSARRecipe[] = {
    {CEOS_REC_NUMCHANS, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 233, 4,
     CEOS_REC_TYP_I}, /* Number of channels */
    {CEOS_REC_INTERLEAVE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 269, 4,
     CEOS_REC_TYP_A}, /* Interleaving type */
    {CEOS_REC_DATATYPE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 429, 4,
     CEOS_REC_TYP_A}, /* Data type */
    {CEOS_REC_LINES, 1, CEOS_ANY_FILE, DATA_SET_SUMMARY, 325, 8,
     CEOS_REC_TYP_I}, /* How many lines */
    {CEOS_REC_PPL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 249, 8,
     CEOS_REC_TYP_I}, /* Pixels per line */
    {CEOS_REC_BPP, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 225, 4,
     CEOS_REC_TYP_I}, /* Bytes Per Pixel */
    {CEOS_REC_RPL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 273, 2,
     CEOS_REC_TYP_I}, /* Records per line */
    {CEOS_REC_IDS, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 277, 4,
     CEOS_REC_TYP_I}, /* Prefix data per record */
    {CEOS_REC_FDL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 9, 4,
     CEOS_REC_TYP_B}, /* Length of Imagry Options Header */
    {CEOS_REC_RECORDSIZE, 1, CEOS_IMAGRY_OPT_FILE, PROC_DATA_REC, 9, 4,
     CEOS_REC_TYP_B}, /* The processed image record size */
    {CEOS_REC_SUFFIX_SIZE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 289, 4,
     CEOS_REC_TYP_I},                /* Suffix data per record */
    {0, 0, 0, {0, 0, 0, 0}, 0, 0, 0} /* Last record is Zero */
};

static const CeosRecipeType_t SIRCRecipe[] = {
    {CEOS_REC_NUMCHANS, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 233, 4,
     CEOS_REC_TYP_I}, /* Number of channels */
    {CEOS_REC_INTERLEAVE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 269, 4,
     CEOS_REC_TYP_A}, /* Interleaving type */
    {CEOS_REC_DATATYPE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 429, 4,
     CEOS_REC_TYP_A}, /* Data type */
    {CEOS_REC_LINES, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 237, 8,
     CEOS_REC_TYP_I}, /* How many lines */
    {CEOS_REC_TBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 261, 4, CEOS_REC_TYP_I},
    {CEOS_REC_BBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 265, 4,
     CEOS_REC_TYP_I}, /* Bottom border pixels */
    {CEOS_REC_PPL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 249, 8,
     CEOS_REC_TYP_I}, /* Pixels per line */
    {CEOS_REC_LBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 245, 4,
     CEOS_REC_TYP_I}, /* Left Border Pixels */
    {CEOS_REC_RBP, 0, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 257, 4,
     CEOS_REC_TYP_I}, /* Right Border Pixels */
    {CEOS_REC_BPP, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 225, 4,
     CEOS_REC_TYP_I}, /* Bytes Per Pixel */
    {CEOS_REC_RPL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 273, 2,
     CEOS_REC_TYP_I}, /* Records per line */
    {CEOS_REC_IDS, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 277, 4,
     CEOS_REC_TYP_I}, /* Prefix data per record */
    {CEOS_REC_FDL, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 9, 4,
     CEOS_REC_TYP_B}, /* Length of Imagry Options Header */
    {CEOS_REC_RECORDSIZE, 1, CEOS_IMAGRY_OPT_FILE, PROC_DATA_REC, 9, 4,
     CEOS_REC_TYP_B}, /* The processed image record size */
    {CEOS_REC_SUFFIX_SIZE, 1, CEOS_IMAGRY_OPT_FILE, IMAGE_OPT, 289, 4,
     CEOS_REC_TYP_I}, /* Suffix data per record */

    {0, 0, 0, {0, 0, 0, 0}, 0, 0, 0} /* Last record is Zero */
};

#undef PROC_DATA_REC

static void ExtractInt(CeosRecord_t *record, int type, unsigned int offset,
                       unsigned int length, int *value);

static char *ExtractString(CeosRecord_t *record, unsigned int offset,
                           unsigned int length, char *string);

static int GetCeosStringType(const CeosStringType_t *CeosType,
                             const char *string);

static int SIRCRecipeFCN(CeosSARVolume_t *volume, const void *token);
static int PALSARRecipeFCN(CeosSARVolume_t *volume, const void *token);

Link_t *RecipeFunctions = NULL;

void RegisterRecipes(void)
{

    AddRecipe(SIRCRecipeFCN, SIRCRecipe, "SIR-C");
    AddRecipe(ScanSARRecipeFCN, ScanSARRecipe, "ScanSAR");
    AddRecipe(CeosDefaultRecipe, RadarSatRecipe, "RadarSat");
    AddRecipe(CeosDefaultRecipe, JersRecipe, "Jers");
    AddRecipe(PALSARRecipeFCN, RadarSatRecipe, "PALSAR-ALOS");
    /*  AddRecipe( CeosDefaultRecipe, AtlantisRecipe ); */
}

void FreeRecipes(void)

{
    Link_t *l_link;

    for (l_link = RecipeFunctions; l_link != NULL; l_link = l_link->next)
        HFree(l_link->object);

    DestroyList(RecipeFunctions);
    RecipeFunctions = NULL;
}

void AddRecipe(int (*function)(CeosSARVolume_t *volume, const void *token),
               const void *token, const char *name)
{

    RecipeFunctionData_t *TempData;

    Link_t *Link;

    TempData = HMalloc(sizeof(RecipeFunctionData_t));

    TempData->function = function;
    TempData->token = token;
    TempData->name = name;

    Link = ceos2CreateLink(TempData);

    if (RecipeFunctions == NULL)
    {
        RecipeFunctions = Link;
    }
    else
    {
        RecipeFunctions = InsertLink(RecipeFunctions, Link);
    }
}

int CeosDefaultRecipe(CeosSARVolume_t *volume, const void *token)
{
    const CeosRecipeType_t *recipe;
    CeosRecord_t *record;
    CeosTypeCode_t TypeCode = {0};
    struct CeosSARImageDesc *ImageDesc = &(volume->ImageDesc);
    char temp_str[1024];
    int i /*, temp_int */;

#define DoExtractInt(a)                                                        \
    ExtractInt(record, recipe[i].Type, recipe[i].Offset, recipe[i].Length, &a)

    if (token == NULL)
    {
        return 0;
    }

    memset(ImageDesc, 0, sizeof(struct CeosSARImageDesc));

    /*    temp_imagerecipe = (CeosSARImageDescRecipe_t *) token;
        recipe = temp_imagerecipe->Recipe; */

    recipe = token;

    for (i = 0; recipe[i].ImageDescValue != 0; i++)
    {
        if (recipe[i].Override)
        {
            TypeCode.UCharCode.Subtype1 = recipe[i].TypeCode.Subtype1;
            TypeCode.UCharCode.Type = recipe[i].TypeCode.Type;
            TypeCode.UCharCode.Subtype2 = recipe[i].TypeCode.Subtype2;
            TypeCode.UCharCode.Subtype3 = recipe[i].TypeCode.Subtype3;

            record = FindCeosRecord(volume->RecordList, TypeCode,
                                    recipe[i].FileId, -1, -1);

            if (record == NULL)
            {
                /* temp_int = 0; */
            }
            else
            {

                switch (recipe[i].ImageDescValue)
                {
                    case CEOS_REC_NUMCHANS:
                        DoExtractInt(ImageDesc->NumChannels);
                        break;
                    case CEOS_REC_LINES:
                        DoExtractInt(ImageDesc->Lines);
                        break;
                    case CEOS_REC_BPP:
                        DoExtractInt(ImageDesc->BytesPerPixel);
                        break;
                    case CEOS_REC_RPL:
                        DoExtractInt(ImageDesc->RecordsPerLine);
                        break;
                    case CEOS_REC_PDBPR:
                        DoExtractInt(ImageDesc->PixelDataBytesPerRecord);
                        break;
                    case CEOS_REC_FDL:
                        DoExtractInt(ImageDesc->FileDescriptorLength);
                        break;
                    case CEOS_REC_IDS:
                        DoExtractInt(ImageDesc->ImageDataStart);
                        /*
                        ** This is really reading the quantity of prefix data
                        ** per data record.  We want the offset from the very
                        ** beginning of the record to the data, so we add
                        *another
                        ** 12 to that.  I think some products incorrectly
                        *indicate
                        ** 192 (prefix+12) instead of 180 so if we see 192
                        *assume
                        ** the 12 bytes of record start data has already been
                        ** added.  Frank Warmerdam.
                        */
                        if (ImageDesc->ImageDataStart != 192)
                            ImageDesc->ImageDataStart += 12;
                        break;
                    case CEOS_REC_SUFFIX_SIZE:
                        DoExtractInt(ImageDesc->ImageSuffixData);
                        break;
                    case CEOS_REC_RECORDSIZE:
                        DoExtractInt(ImageDesc->BytesPerRecord);
                        break;
                    case CEOS_REC_PPL:
                        DoExtractInt(ImageDesc->PixelsPerLine);
                        break;
                    case CEOS_REC_TBP:
                        DoExtractInt(ImageDesc->TopBorderPixels);
                        break;
                    case CEOS_REC_BBP:
                        DoExtractInt(ImageDesc->BottomBorderPixels);
                        break;
                    case CEOS_REC_LBP:
                        DoExtractInt(ImageDesc->LeftBorderPixels);
                        break;
                    case CEOS_REC_RBP:
                        DoExtractInt(ImageDesc->RightBorderPixels);
                        break;
                    case CEOS_REC_INTERLEAVE:
                        ExtractString(record, recipe[i].Offset,
                                      recipe[i].Length, temp_str);

                        ImageDesc->ChannelInterleaving =
                            GetCeosStringType(CeosInterleaveType, temp_str);
                        break;
                    case CEOS_REC_DATATYPE:
                        ExtractString(record, recipe[i].Offset,
                                      recipe[i].Length, temp_str);

                        ImageDesc->DataType =
                            GetCeosStringType(CeosDataType, temp_str);
                        break;
                }
            }
        }
    }

    /* Some files (Telaviv) don't record the number of pixel groups per line.
     * Try to derive it from the size of a data group, and the number of
     * bytes of pixel data if necessary.
     */

    if (ImageDesc->PixelsPerLine == 0 &&
        ImageDesc->PixelDataBytesPerRecord != 0 &&
        ImageDesc->BytesPerPixel != 0)
    {
        ImageDesc->PixelsPerLine =
            ImageDesc->PixelDataBytesPerRecord / ImageDesc->BytesPerPixel;
        CPLDebug("SAR_CEOS", "Guessing PixelPerLine to be %d\n",
                 ImageDesc->PixelsPerLine);
    }

    /* Some files don't have the BytesPerRecord stuff, so we calculate it if
     * possible */

    if (ImageDesc->BytesPerRecord == 0 && ImageDesc->RecordsPerLine == 1 &&
        ImageDesc->PixelsPerLine > 0 && ImageDesc->BytesPerPixel > 0)
    {
        CeosRecord_t *img_rec;

        ImageDesc->BytesPerRecord =
            ImageDesc->PixelsPerLine * ImageDesc->BytesPerPixel +
            ImageDesc->ImageDataStart + ImageDesc->ImageSuffixData;

        TypeCode.UCharCode.Subtype1 = 0xed;
        TypeCode.UCharCode.Type = 0xed;
        TypeCode.UCharCode.Subtype2 = 0x12;
        TypeCode.UCharCode.Subtype3 = 0x12;

        img_rec = FindCeosRecord(volume->RecordList, TypeCode,
                                 CEOS_IMAGRY_OPT_FILE, -1, -1);
        if (img_rec == NULL)
        {
            CPLDebug("SAR_CEOS",
                     "Unable to find imagery rec to check record length.");
            return 0;
        }

        if (img_rec->Length != ImageDesc->BytesPerRecord)
        {
            CPLDebug("SAR_CEOS",
                     "Guessed record length (%d) did not match\n"
                     "actual imagery record length (%d), recipe fails.",
                     ImageDesc->BytesPerRecord, img_rec->Length);
            return 0;
        }
    }

    if (ImageDesc->PixelsPerRecord == 0 && ImageDesc->BytesPerRecord != 0 &&
        ImageDesc->BytesPerPixel != 0)
    {
        ImageDesc->PixelsPerRecord =
            ((ImageDesc->BytesPerRecord -
              (ImageDesc->ImageSuffixData + ImageDesc->ImageDataStart)) /
             ImageDesc->BytesPerPixel);

        if (ImageDesc->PixelsPerRecord > ImageDesc->PixelsPerLine)
            ImageDesc->PixelsPerRecord = ImageDesc->PixelsPerLine;
    }

    /* If we didn't get a data type, try guessing. */
    if (ImageDesc->DataType == 0 && ImageDesc->BytesPerPixel != 0 &&
        ImageDesc->NumChannels != 0)
    {
        int nDataTypeSize = ImageDesc->BytesPerPixel / ImageDesc->NumChannels;

        if (nDataTypeSize == 1)
            ImageDesc->DataType = CEOS_TYP_UCHAR;
        else if (nDataTypeSize == 2)
            ImageDesc->DataType = CEOS_TYP_USHORT;
    }

    /* Sanity checking */

    if (ImageDesc->PixelsPerLine == 0 || ImageDesc->Lines == 0 ||
        ImageDesc->RecordsPerLine == 0 || ImageDesc->ImageDataStart == 0 ||
        ImageDesc->FileDescriptorLength == 0 || ImageDesc->DataType == 0 ||
        ImageDesc->NumChannels == 0 || ImageDesc->BytesPerPixel == 0 ||
        ImageDesc->ChannelInterleaving == 0 || ImageDesc->BytesPerRecord == 0)
    {
        return 0;
    }
    else
    {

        ImageDesc->ImageDescValid = TRUE;
        return 1;
    }
}

int ScanSARRecipeFCN(CeosSARVolume_t *volume, const void *token)
{
    struct CeosSARImageDesc *ImageDesc = &(volume->ImageDesc);

    memset(ImageDesc, 0, sizeof(struct CeosSARImageDesc));

    if (CeosDefaultRecipe(volume, token))
    {
        ImageDesc->Lines *= 2;
        return 1;
    }

    return 0;
}

static int SIRCRecipeFCN(CeosSARVolume_t *volume, const void *token)
{
    struct CeosSARImageDesc *ImageDesc = &(volume->ImageDesc);
    CeosTypeCode_t TypeCode = {0};
    CeosRecord_t *record;
    char szSARDataFormat[29];

    memset(ImageDesc, 0, sizeof(struct CeosSARImageDesc));

    /* -------------------------------------------------------------------- */
    /*      First, we need to check if the "SAR Data Format Type            */
    /*      identifier" is set to "COMPRESSED CROSS-PRODUCTS" which is      */
    /*      pretty idiosyncratic to SIRC products.  It might also appear    */
    /*      for some other similarly encoded Polarimetric data I suppose.    */
    /* -------------------------------------------------------------------- */
    /* IMAGE_OPT */
    TypeCode.UCharCode.Subtype1 = 63;
    TypeCode.UCharCode.Type = 192;
    TypeCode.UCharCode.Subtype2 = 18;
    TypeCode.UCharCode.Subtype3 = 18;

    record = FindCeosRecord(volume->RecordList, TypeCode, CEOS_IMAGRY_OPT_FILE,
                            -1, -1);
    if (record == NULL)
        return 0;

    ExtractString(record, 401, 28, szSARDataFormat);
    if (!STARTS_WITH_CI(szSARDataFormat, "COMPRESSED CROSS-PRODUCTS"))
        return 0;

    /* -------------------------------------------------------------------- */
    /*      Apply normal handling...                                        */
    /* -------------------------------------------------------------------- */
    CeosDefaultRecipe(volume, token);

    /* -------------------------------------------------------------------- */
    /*      Make sure this looks like the SIRC product we are expecting.    */
    /* -------------------------------------------------------------------- */
    if (ImageDesc->BytesPerPixel != 10)
        return 0;

    /* -------------------------------------------------------------------- */
    /*      Then fix up a few values.                                       */
    /* -------------------------------------------------------------------- */
    /* It seems the bytes of pixel data per record is just wrong.  Fix. */
    ImageDesc->PixelDataBytesPerRecord =
        ImageDesc->BytesPerPixel * ImageDesc->PixelsPerLine;

    ImageDesc->DataType = CEOS_TYP_CCP_COMPLEX_FLOAT;

    /* -------------------------------------------------------------------- */
    /*      Sanity checking                                                 */
    /* -------------------------------------------------------------------- */
    if (ImageDesc->PixelsPerLine == 0 || ImageDesc->Lines == 0 ||
        ImageDesc->RecordsPerLine == 0 || ImageDesc->ImageDataStart == 0 ||
        ImageDesc->FileDescriptorLength == 0 || ImageDesc->DataType == 0 ||
        ImageDesc->NumChannels == 0 || ImageDesc->BytesPerPixel == 0 ||
        ImageDesc->ChannelInterleaving == 0 || ImageDesc->BytesPerRecord == 0)
    {
        return 0;
    }
    else
    {
        ImageDesc->ImageDescValid = TRUE;
        return 1;
    }
}

static int PALSARRecipeFCN(CeosSARVolume_t *volume, const void *token)
{
    struct CeosSARImageDesc *ImageDesc = &(volume->ImageDesc);
    CeosTypeCode_t TypeCode = {0};
    CeosRecord_t *record;
    char szSARDataFormat[29], szProduct[32];

    memset(ImageDesc, 0, sizeof(struct CeosSARImageDesc));

    /* -------------------------------------------------------------------- */
    /*      First, we need to check if the "SAR Data Format Type            */
    /*      identifier" is set to "COMPRESSED CROSS-PRODUCTS" which is      */
    /*      pretty idiosyncratic to SIRC products.  It might also appear    */
    /*      for some other similarly encoded Polarimetric data I suppose.    */
    /* -------------------------------------------------------------------- */
    /* IMAGE_OPT */
    TypeCode.UCharCode.Subtype1 = 63;
    TypeCode.UCharCode.Type = 192;
    TypeCode.UCharCode.Subtype2 = 18;
    TypeCode.UCharCode.Subtype3 = 18;

    record = FindCeosRecord(volume->RecordList, TypeCode, CEOS_IMAGRY_OPT_FILE,
                            -1, -1);
    if (record == NULL)
        return 0;

    ExtractString(record, 401, 28, szSARDataFormat);
    if (!STARTS_WITH_CI(szSARDataFormat, "INTEGER*18                 "))
        return 0;

    ExtractString(record, 49, 16, szProduct);
    if (!STARTS_WITH_CI(szProduct, "ALOS-"))
        return 0;

    /* -------------------------------------------------------------------- */
    /*      Apply normal handling...                                        */
    /* -------------------------------------------------------------------- */
    CeosDefaultRecipe(volume, token);

    /* -------------------------------------------------------------------- */
    /*      Make sure this looks like the SIRC product we are expecting.    */
    /* -------------------------------------------------------------------- */
    if (ImageDesc->BytesPerPixel != 18)
        return 0;

    /* -------------------------------------------------------------------- */
    /*      Then fix up a few values.                                       */
    /* -------------------------------------------------------------------- */
    ImageDesc->DataType = CEOS_TYP_PALSAR_COMPLEX_SHORT;
    ImageDesc->NumChannels = 6;

    /* -------------------------------------------------------------------- */
    /*      Sanity checking                                                 */
    /* -------------------------------------------------------------------- */
    if (ImageDesc->PixelsPerLine == 0 || ImageDesc->Lines == 0 ||
        ImageDesc->RecordsPerLine == 0 || ImageDesc->ImageDataStart == 0 ||
        ImageDesc->FileDescriptorLength == 0 || ImageDesc->DataType == 0 ||
        ImageDesc->NumChannels == 0 || ImageDesc->BytesPerPixel == 0 ||
        ImageDesc->ChannelInterleaving == 0 || ImageDesc->BytesPerRecord == 0)
    {
        return 0;
    }
    else
    {
        ImageDesc->ImageDescValid = TRUE;
        return 1;
    }
}

void GetCeosSARImageDesc(CeosSARVolume_t *volume)
{
    Link_t *l_link;
    RecipeFunctionData_t *rec_data;
    int (*function)(CeosSARVolume_t * volume, const void *token);

    if (RecipeFunctions == NULL)
    {
        RegisterRecipes();
    }

    if (RecipeFunctions == NULL)
    {
        return;
    }

    for (l_link = RecipeFunctions; l_link != NULL; l_link = l_link->next)
    {
        if (l_link->object)
        {
            rec_data = l_link->object;
            function = rec_data->function;
            if ((*function)(volume, rec_data->token))
            {
                CPLDebug("CEOS", "Using recipe '%s'.", rec_data->name);
                return;
            }
        }
    }

    return;
}

static void ExtractInt(CeosRecord_t *record, int type, unsigned int offset,
                       unsigned int length, int *value)
{
    void *buffer;
    char format[32];

    buffer = HMalloc(length + 1);

    switch (type)
    {
        case CEOS_REC_TYP_A:
            snprintf(format, sizeof(format), "A%u", length);
            GetCeosField(record, offset, format, buffer);
            *value = atoi(buffer);
            break;
        case CEOS_REC_TYP_B:
            snprintf(format, sizeof(format), "B%u", length);
#ifdef notdef
            GetCeosField(record, offset, format, buffer);
            if (length <= 4)
                CeosToNative(value, buffer, length, length);
            else
                *value = 0;
#else
            GetCeosField(record, offset, format, value);
#endif
            break;
        case CEOS_REC_TYP_I:
            snprintf(format, sizeof(format), "I%u", length);
            GetCeosField(record, offset, format, value);
            break;
    }

    HFree(buffer);
}

static char *ExtractString(CeosRecord_t *record, unsigned int offset,
                           unsigned int length, char *string)
{
    char format[12];

    if (string == NULL)
    {
        string = HMalloc(length + 1);
    }

    snprintf(format, sizeof(format), "A%u", length);

    GetCeosField(record, offset, format, string);

    return string;
}

static int GetCeosStringType(const CeosStringType_t *CeosStringType,
                             const char *string)
{
    int i;

    for (i = 0; CeosStringType[i].String != NULL; i++)
    {
        if (strncmp(CeosStringType[i].String, string,
                    strlen(CeosStringType[i].String)) == 0)
        {
            return CeosStringType[i].Type;
        }
    }

    return 0;
}