This repository was archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPapyRead3.c
2389 lines (1934 loc) · 88 KB
/
PapyRead3.c
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************/
/* */
/* Papyrus 3 library. */
/* This library constitutes a DICOM file system which helps reading and writing */
/* DICOM files and DICOMDIR files. */
/* */
/* Copyright (C) 2004 - Service of Medical Informatics - */
/* University Hospitals of Geneva (HUG), Geneva, Switzerland */
/* */
/* This library is a free software; you can redistribute it and/or modify it */
/* under the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation; either version 2.1 of the License, or */
/* (at your option) any later version. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
/* See the GNU Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with this library; if not, write to */
/* the Free Software Foundation, Inc., */
/* 59 Temple Place, Suite 330, */
/* Boston, MA 02111-1307 USA */
/* */
/* You can contact us for more information at [email protected] */
/* or by writing to Papyrus, */
/* Unite d'Imagerie Numerique / Service d'Informatique Medicale / HUG, */
/* 24, Micheli-du-Crest street, 1211 Geneva 14, Switzerland. */
/* */
/* The University Hopitals of Geneva, hereby disclaims all copyright interest */
/* in the library `Papyrus' (a library for reading and writing DICOM files). */
/* */
/* Geneva, april 2004 */
/* Antoine Geissbuhler, head of the Service of Medical Informatics, */
/* University Hospitals of Geneva, Switzerland */
/* */
/********************************************************************************/
/********************************************************************************/
/* */
/* Project : P A P Y R U S Toolkit */
/* File : PapyRead3.c */
/* Function : contains all the reading functions */
/* Authors : Matthieu Funk */
/* Christian Girard */
/* Jean-Francois Vurlod */
/* Marianne Logean */
/* */
/* History : 12.1990 version 1.0 */
/* 04.1991 version 1.1 */
/* 12.1991 version 1.2 */
/* 06.1993 version 2.0 */
/* 06.1994 version 3.0 */
/* 06.1995 version 3.1 */
/* 02.1996 version 3.3 */
/* 02.1999 version 3.6 */
/* 04.2001 version 3.7 */
/* 09.2001 version 3.7 on CVS */
/* 10.2001 version 3.71 MAJ Dicom par CHG */
/* */
/********************************************************************************/
#ifdef Mac
#pragma segment papy3
#endif
/* ------------------------- includes ---------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "setjmp.h"
#include "JPEGLESS.H" /* interface for JPEG lossless decompressor */
#include "JPEGLIB.H" /* interface for JPEG lossy decompressor */
#ifdef MAYO_WAVE
#include "Mayo.h" /* interface for wavelet decompressor */
#define TO_SWAP_MAYO
#endif /* MAYO_WAVE */
#ifndef Papyrus3H
#include "Papyrus3.h"
#endif
/********************************************************************************/
/* */
/* Papy3GetElement : gets the value(s) of the specified element */
/* return : the value(s) of the element */
/* */
/********************************************************************************/
UValue_T * CALLINGCONV
Papy3GetElement (SElement *inGrOrModP, int inElement, PapyULong *outNbValueP, int *outElemTypeP)
/*SElement *inGrOrModP; ptr on the group or the module */
/*PapyShort inElement; the position of the element in the group */
/*PapyULong *outNbValueP; the number of values to read */
/*PapyShort *outElemTypeP; what is the type of the element */
{
SElement *theElemP; /* work pointer on the elements of the group */
UValue_T *theValueP; /* the value we are looking for */
if (inGrOrModP == NULL) return NULL;
theElemP = inGrOrModP;
theElemP += inElement; /* points on the desired element */
*outElemTypeP = theElemP->vr; /* is it a short a long or an ASCII char ? */
if (theElemP->nb_val > 0L) /* there is an introduced value */
{
*outNbValueP = theElemP->nb_val;
theValueP = theElemP->value;
} /* then */
else
{
*outNbValueP = 0L;
theValueP = NULL;
} /* else */
return theValueP;
} /* endof Papy3GetElement */
/********************************************************************************/
/* */
/* ExtractJPEGlosslessDicom : gets and decode JPEG lossless pixel data */
/* Nota : the PAPYRUS toolkit JPEG utility is based in part on the work of */
/* the Independent JPEG Group (see copyright file included) */
/* return : the image */
/* */
/********************************************************************************/
PapyShort
ExtractJPEGlosslessDicom (PapyShort inFileNb, PapyUChar *outBufferP, PapyULong inPixelStart,
PapyULong *inOffsetTableP, int inImageNb)
{
PapyUChar theTmpBuf [256];
PapyUChar *theTmpBufP;
PapyShort theErr;
PapyUShort theGroup, theElement;
PapyULong i, thePos, theLength;
/*
void *aFSSpec;
PAPY_FILE tmpFile;
PapyUChar *myBufPtr;
*/
/* position the file pointer at the begining of the pixel datas */
Papy3FSeek (gPapyFile [inFileNb], SEEK_SET, (PapyLong) (inPixelStart + inOffsetTableP [inImageNb - 1]));
/* read 8 chars from the file */
theTmpBufP = (PapyUChar *) &theTmpBuf [0];
i = 8L; /* grNb, elemNb & elemLength */
if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &i, 1L, theTmpBufP)) < 0)
{
Papy3FClose (&gPapyFile [inFileNb]);
RETURN (theErr);
} /* if */
thePos = 0L;
theGroup = Extract2Bytes (theTmpBufP, &thePos);
theElement = Extract2Bytes (theTmpBufP, &thePos);
/* extract the element length */
theLength = Extract4Bytes (theTmpBufP, &thePos);
/* if length is 0xFFFFFFFF (undefined) we have to extract it HERE !!! */
/* Pixel data fragment not found when expected */
if ((theGroup != 0xFFFE) || (theElement != 0xE000)) RETURN (papBadArgument);
/******/
/* extract the compressed datas from the file and put it in temp file */
/******/
/* first : create a new file and opens it */
/* avoid to create more than one image */
/* allocate the buffer to store the temp compressed datas */
/* read the compressed stream from the file */
/* and put it in the temp file */
/* close the temp file */
/* and free the allocated memory */
/* then reset the file pointer to its previous position */
/*
strcpy ((char *) theTmpBufP, "Compressed.jpg");
theErr = Papy3FCreate ((char *) theTmpBufP, 0, NULL, &aFSSpec);
if (theErr == 0)
{
theErr = Papy3FOpen (NULL, 'w', 0, &tmpFile, &aFSSpec);
myBufPtr = (PapyUChar *) emalloc3 (theLength + 1L);
theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &theLength, 1L, myBufPtr);
theErr = (PapyShort) Papy3FWrite (tmpFile, &theLength, 1L, (void *) myBufPtr);
theErr = Papy3FClose (&tmpFile);
efree3 ((void **) &myBufPtr);
theErr = (PapyShort) Papy3FSeek (gPapyFile [inFileNb], SEEK_CUR, - (PapyLong) theLength);
} /* if ...no error creating the temp file */
/******/
/******/
/* Get ready to receive decompressed rows */
JPEGLosslessDecodeImage (gPapyFile [inFileNb], (PapyUShort *) outBufferP,
gx0028BitsAllocated [inFileNb], theLength);
return 0;
} /* endof ExtractJPEGlosslessDicom */
/********************************************************************************/
/* */
/* ExtractJPEGlosslessPap : gets and decode JPEG lossless pixel data */
/* Nota : the PAPYRUS toolkit JPEG utility is based in part on the work of */
/* the Independent JPEG Group (see copyright file included) */
/* return : the image */
/* */
/********************************************************************************/
PapyShort
ExtractJPEGlosslessPap (PapyShort inFileNb, PapyUChar *outBufferP, PapyULong inPixelStart,
PapyULong inLength)
{
/* position the file pointer at the begining of the pixel datas */
Papy3FSeek (gPapyFile [inFileNb], SEEK_SET, (PapyLong) inPixelStart);
/* Get ready to receive decompressed rows */
JPEGLosslessDecodeImage (gPapyFile [inFileNb], (PapyUShort *) outBufferP,
gx0028BitsAllocated [inFileNb], inLength);
return 0;
} /* endof ExtractJPEGlosslessPap */
/********************************************************************************/
/* */
/* Needed for the error manager of the JPEG lossy library */
/* */
/********************************************************************************/
struct SErrorMgr
{
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
}; /* struct */
typedef struct SErrorMgr *SErrorMgrP;
/********************************************************************************/
/* */
/* Here's the routine that will replace the standard error_exit method: */
/* for JPEG lossy */
/* */
/********************************************************************************/
METHODDEF(void)
my_error_exit (j_common_ptr ioCInfo)
{
/* ioCInfo->err really points to a SErrorMgr struct, so coerce pointer */
SErrorMgrP theErr = (SErrorMgrP) ioCInfo->err;
/* Always display the message. */
/* We could postpone this until after returning, if we chose. */
(*ioCInfo->err->output_message) (ioCInfo);
/* Return control to the setjmp point */
#ifdef Mac
longjmp (theErr->setjmp_buffer, 1);
#endif
} /* endofunction my_error_exit */
/********************************************************************************/
/* */
/* ExtractJPEGlossy : gets and decode JPEG lossy pixel data */
/* Nota : the PAPYRUS toolkit JPEG utility is based in part on the work of */
/* the Independent JPEG Group (see copyright file included) */
/* return : the image */
/* */
/********************************************************************************/
PapyShort
ExtractJPEGlossy (PapyShort inFileNb, PapyUChar *ioImage8P, PapyULong inPixelStart,
PapyULong *inOffsetTableP, int inImageNb, int inDepth)
{
struct SErrorMgr theJErr; /* the JPEG error manager var */
struct jpeg_decompress_struct theCInfo;
PapyUChar theTmpBuf [256];
PapyUChar *theTmpBufP;
PapyUShort theGroup, theElement;
PapyShort theErr = 0;
PapyULong i, thePos, theLimit;
int theRowStride; /* physical row width in output buffer */
int theLoop;
PapyUChar *theWrkChP; /* ptr to the image */
PapyUChar *theWrkCh8P; /* ptr to the image 8 bits */
PapyUShort *theWrkCh16P; /* ptr to the image 16 bits */
PapyUShort *theBuffer16P;
PapyUChar *theBuffer8P;
/* position the file pointer to the begining of the image */
Papy3FSeek (gPapyFile [inFileNb], SEEK_SET, (PapyLong) (inPixelStart + inOffsetTableP [inImageNb - 1]));
/* read 8 chars from the file */
theTmpBufP = (PapyUChar *) &theTmpBuf [0];
i = 8L; /* grNb, elemNb & elemLength */
if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &i, 1L, theTmpBufP)) < 0)
{
Papy3FClose (&gPapyFile [inFileNb]);
RETURN (theErr);
} /* if */
thePos = 0L;
theGroup = Extract2Bytes (theTmpBufP, &thePos);
theElement = Extract2Bytes (theTmpBufP, &thePos);
/* Pixel data fragment not found when expected */
if ((theGroup != 0xFFFE) || (theElement != 0xE000)) RETURN (papBadArgument);
/* We set up the normal JPEG error routines, then override error_exit. */
theCInfo.err = jpeg_std_error (&theJErr.pub);
theJErr.pub.error_exit = my_error_exit;
/* Establish the setjmp return context for my_error_exit to use. */
#ifdef Mac
if (setjmp (theJErr.setjmp_buffer))
{
jpeg_destroy_decompress (&theCInfo);
return NULL;
}/* if */
#endif
/* initialize the JPEG decompression object */
jpeg_create_decompress (&theCInfo);
/* specify the data source */
jpeg_stdio_src (&theCInfo, gPapyFile [inFileNb]);
/* read file parameter */
(void) jpeg_read_header (&theCInfo, TRUE);
if (gArrPhotoInterpret [inFileNb] == MONOCHROME1 ||
gArrPhotoInterpret [inFileNb] == MONOCHROME2)
theCInfo.out_color_space = JCS_GRAYSCALE;
if (gArrPhotoInterpret [inFileNb] == RGB)
theCInfo.out_color_space = JCS_RGB;
/* theCInfo.out_color_space = JCS_YCbCr; */
/* start the decompressor (set the decompression default params) */
(void) jpeg_start_decompress (&theCInfo);
/* JSAMPLEs per row in output buffer */
theRowStride = theCInfo.output_width * theCInfo.output_components;
if (inDepth == 16)
theRowStride *= 2;
/* allocate a one-row-high sample array that will go away when done with image */
if (inDepth == 16)
{
theBuffer16P = (PapyUShort *) emalloc3 ((PapyULong) theRowStride);
theWrkCh16P = (PapyUShort *) ioImage8P;
}
else
{
theBuffer8P = (PapyUChar *) emalloc3 ((PapyULong) theRowStride);
theWrkCh8P = (PapyUChar *) ioImage8P;
}
theWrkChP = (PapyUChar *) ioImage8P;
theLimit = theCInfo.output_width * theCInfo.output_components;
/* decompress the image line by line 8 bits */
if (inDepth == 8)
{
while (theCInfo.output_scanline < theCInfo.output_height)
{
(void) jpeg_read_scanlines (&theCInfo, (JSAMPARRAY) &theBuffer8P, 1);
/* put the scanline in the image */
for (theLoop = 0; theLoop < (int) theLimit; theLoop ++)
{
if (theCInfo.out_color_space == JCS_GRAYSCALE)
if (theBuffer8P [theLoop] > 255)
theBuffer8P [theLoop] = 255;
*theWrkChP = (PapyUChar) theBuffer8P [theLoop];
theWrkChP++;
} /* for */
} /* while ...line by line decompression of the image */
/* frees the row used by the decompressor */
efree3 ((void **) &theBuffer8P);
} /* if ...depth = 8 */
/* decompress the image line by line 16 bits */
else if (inDepth == 16)
{
while (theCInfo.output_scanline < theCInfo.output_height)
{
(void) jpeg_read_scanlines (&theCInfo, (JSAMPARRAY) &theBuffer16P, 1);
/* put the scanline in the image */
for (theLoop = 0; theLoop < (int) theLimit; theLoop ++)
{
*theWrkCh16P = theBuffer16P [theLoop];
theWrkCh16P++;
} /* for */
} /* while ...line by line decompression of the image */
/* frees the row used by the decompressor */
efree3 ((void **) &theBuffer16P);
} /* else ...depth = 16 bits */
/* tell the JPEG decompressor we have finish the decompression */
(void) jpeg_finish_decompress (&theCInfo);
/* MAL added : cf Example.c */
/* Step 8: Release JPEG decompression object */
/* This is an important step since it will release a good deal of memory. */
jpeg_destroy_decompress(&theCInfo);
return theErr;
} /* endof ExtractJPEGlossy */
/********************************************************************************/
/* */
/* ExtractWavelet : gets and decode Wavelet pixel data */
/* return : the image */
/* */
/********************************************************************************/
#ifdef MAYO_WAVE
PapyShort
ExtractWavelet (PapyShort inFileNb, PapyUChar *ioImage8P, PapyULong inPixelStart,
PapyULong *inOffsetTableP, int inImageNb, int inDepth)
{
PapyUShort theGroup, theElement;
MayoCompressedImage *theCompressedP;
MayoRawImage *theRawP ;
int theJs, theIs;
PapyUChar theTmpBuf [256];
PapyUChar *theTmpBufP;
PapyUChar *tmpBufPtr2;
PapyULong i, thePos, theSize, theLength;
PapyShort theErr;
PapyUShort *theImage16P;
PapyUChar *theValTempP, *theValFinalP;
PapyUChar theHigh, theLow;
Papy3FSeek (gPapyFile [inFileNb], SEEK_SET, (PapyLong) (inPixelStart + inOffsetTableP [inImageNb - 1]));
theTmpBufP = (PapyUChar *) &theTmpBuf [0];
i = 8L;
if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &i, 1L, theTmpBufP)) < 0)
{
Papy3FClose (&gPapyFile [inFileNb]);
RETURN (theErr);
}
thePos = 0L;
theGroup = Extract2Bytes (theTmpBufP, &thePos);
theElement = Extract2Bytes (theTmpBufP, &thePos);
/* Pixel data fragment not found when expected */
if ((theGroup != 0xFFFE) || (theElement != 0xE000)) RETURN (papBadArgument);
/* Load the compressed file into memory */
/*
theCompressedP = MayoReadCompressed (gPapyFile [inFileNb]) ;
if ( theCompressedP == NULL ) {
exit(MayoGetError()) ; }
*/
tmpBufPtr2 = (PapyUChar *) &theTmpBuf [0];
theCompressedP = (MayoCompressedImage *) emalloc3(sizeof(MayoCompressedImage)) ;
if ( theCompressedP == NULL )
{
return (-1);
} /* if */
i = 8L;
if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &i, 1L,theTmpBufP)) < 0)
{
Papy3FClose (&gPapyFile [inFileNb]);
efree3((void **) &theCompressedP) ;
RETURN (theErr);
} /* if */
thePos = 0L;
theCompressedP->length = Extract4Bytes (theTmpBufP, &thePos);
theCompressedP->version = Extract4Bytes (theTmpBufP, &thePos);
/* Allocate memory for the image data */
theCompressedP->buf = (unsigned char *) emalloc3 (theCompressedP->length) ;
if (theCompressedP->buf == NULL)
{
efree3 ((void **) &theCompressedP);
return(-1);
} /* if */
/* Read the image data */
theLength = (PapyULong) theCompressedP->length;
if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &theLength, 1L, theCompressedP->buf)) < 0)
{
Papy3FClose (&gPapyFile [inFileNb]);
efree3((void **) &theCompressedP->buf) ;
efree3((void **) &theCompressedP) ;
RETURN (theErr);
} /* if */
theCompressedP->length = (int) theLength;
/* Run the decompressor */
theRawP = MayoDecompress (theCompressedP);
if (theRawP == NULL)
{
return (MayoGetError());
} /* if */
/* Copy decompressed image */
theSize = (PapyULong) theRawP->xsize * theRawP->ysize * theRawP->bytesperpixel;
if (inDepth == 8) memcpy (ioImage8P, theRawP->buf, theSize);
else
{
theImage16P = (PapyUShort *) ioImage8P;
memcpy (ioImage8P, theRawP->buf, theSize);
} /* else */
/* Swap bytes if it is a 16-bit image*/
#ifdef TO_SWAP_MAYO
theValTempP = (PapyUChar *) ioImage8P;
theValFinalP = (PapyUChar *) ioImage8P;
if (inDepth == 16)
{
for (theJs = 0; theJs < theRawP->xsize; theJs++)
{
for (theIs = 0; theIs < theRawP->ysize; theIs++)
{
theLow = *theValTempP;
theValTempP++;
theHigh = *theValTempP;
theValTempP++;
*theValFinalP = theHigh;
theValFinalP++;
*theValFinalP = theLow;
theValFinalP++;
} /* for */
} /* for */
} /* if ...inDepth = 16 */
#endif /* TO_SWAP_MAYO */
/* Free allocated memory
MayoFreeCompressed(theCompressedP) ;
MayoFreeRaw(theRawP) ; */
if (theRawP != NULL )
{
if (theRawP->buf != NULL )
{
efree3 ((void **) &(theRawP->buf));
} /* if */
efree3 ((void **) &theRawP);
} /* if */
if (theCompressedP != NULL)
{
if (theCompressedP->buf != NULL)
{
efree3 ((void **) &(theCompressedP->buf));
} /* if */
efree3 ((void **) &theCompressedP);
} /* if */
return (0);
} /* endof ExtractWavelet */
#endif /* MAYO_WAVE */
/********************************************************************************/
/* */
/* DecodeRLESegment */
/* */
/********************************************************************************/
void
DecodeRLESegment (PapyShort inFileNb, PapyUShort *ioImageP, PapyUChar *inRleP,
PapyULong inLength, int inSegtot, int inSegNb)
/* decode a RLE segment */
/* ioImageP : pointer on real image (8 or 16 bits) */
/* inRleP : pointer on rle buffer (8bits) */
/* inLength : length of rle buffer */
/* inSegtot : total number of segments (1, 2 or 3) */
/* inSegNb : number of current segment (1, 2 or 3) (only if inSegtot > 2) */
{
PapyLong j, theIndj;
PapyUChar *thePixP;
PapyUChar theVal;
char theCode;
PapyShort i, theIMax;
/* *** single segment *** */
/* ********************** */
if (inSegtot == 1)
{
/* convert rle into real image */
thePixP = (PapyUChar *) ioImageP;
theIndj = 0L;
for (j = 0L; j < (int) inLength;)
{
theCode = (char) inRleP [j];
j++; /* yes, I know but do not move it */
/* sequence of different bytes */
if (theCode == 0)
{
if (j < (int) (inLength - 1)) thePixP [theIndj++] = inRleP [j++];
} /* if */
/* repetition of the same byte */
else if ((theCode <= -1) && (theCode >= -127))
{
theVal = inRleP [j++];
theIMax = -theCode;
for (i = 0; i <= theIMax; i++)
thePixP [theIndj++] = theVal;
} /* if */
else /* if ((theCode > 0) && (theCode <= 127)) */
{
for (i = 0; i < (theCode + 1); i++)
thePixP [theIndj++] = inRleP [j++];
} /* if */
} /* for */
} /* if ...single segment */
/* *** two segments *** */
/* ******************** */
else if (inSegtot == 2)
{
/* we assume it is a 16 bit image */
/* convert rle into real image */
thePixP = (PapyUChar *) ioImageP;
theIndj = 0L;
if (inSegNb == 2) theIndj++;
for (j = 0L; j < (int)inLength; )
{
theCode = (char) inRleP [j];
j++; /* yes, I know but do not move it */
/* sequence of different bytes */
if (theCode == 0)
{
if (j < (int) (inLength - 1)) thePixP [theIndj] = inRleP [j++];
theIndj = theIndj + 2;
} /* if */
/* repetition of the same byte */
else if ((theCode <= -1) && (theCode >= -127))
{
theVal = inRleP [j++];
theIMax = -theCode;
for (i = 0; i <= theIMax; i++)
{
thePixP [theIndj] = theVal;
theIndj = theIndj + 2;
} /* for */
} /* if */
else /* if ((theCode > 0) && (theCode <= 127)) */
{
for (i = 0; i < (theCode + 1); i++)
{
thePixP [theIndj] = inRleP [j++];
theIndj = theIndj + 2;
} /* for */
} /* if */
} /* for */
} /* if ...two segments */
/* *** three segments *** */
/* ******************** */
else if (inSegtot == 3)
{
/* this must be a RGB or YBR image */
/* so convert each channel at a time */
thePixP = (PapyUChar *) ioImageP;
/* computes the offset in the resulting pixmap */
/* assuming that each plane is 8 bits depth */
theIndj = 0L;
theIndj += ((PapyLong) gx0028Rows [inFileNb] * (PapyLong) gx0028Columns [inFileNb])
* (PapyLong) (inSegNb - 1);
for (j = 0L; j < (int)inLength; )
{
theCode = (char) inRleP [j];
j++; /* yes, I know but do not move it */
/* sequence of different bytes */
if (theCode == 0)
{
if (j < (int)(inLength - 1)) thePixP [theIndj++] = inRleP [j++];
}/* if */
/* repetition of the same byte */
else if ((theCode <= -1) && (theCode >= -127))
{
theVal = inRleP [j++];
theIMax = -theCode;
for (i = 0; i <= theIMax; i++) thePixP [theIndj++] = theVal;
} /* if */
else /* if ((theCode > 0) && (theCode <= 127)) */
{
for (i = 0; i < (theCode + 1); i++) thePixP [theIndj++] = inRleP [j++];
} /* if */
} /* for */
} /* if ...three segments */
} /* endof DecodeRLESegment */
/********************************************************************************/
/* */
/* ExtractRLE : gets and decode a RLE pixel data element */
/* return : the image */
/* */
/********************************************************************************/
PapyShort
ExtractRLE (PapyShort inFileNb, PapyUShort *ioImage16P, PapyULong inPixelStart,
PapyULong *inOffsetTableP, int inImageNb)
{
PapyUChar theTmpBuf [256];
PapyUChar *theTmpBufP;
PapyUShort theGroup, theElement;
PapyShort theErr;
PapyULong theNbOfSegments, i, thePos, theLength;
PapyUChar *theRleP;
long theOffset1, theOffset2, theOffset3, theRleLen;
/* for each image */
/* FFFE E000 length RLE_header RLE_segment1 RLE_segment2 ... */
/* length is 4 bytes, in the case of a single image */
Papy3FSeek (gPapyFile [inFileNb], SEEK_SET, (PapyLong) (inPixelStart + inOffsetTableP [inImageNb - 1]));
/* read 8 chars from the file */
theTmpBufP = (PapyUChar *) &theTmpBuf [0];
i = 8L; /* grNb, elemNb & elemLength */
if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &i, 1L, theTmpBufP)) < 0)
{
Papy3FClose (&gPapyFile [inFileNb]);
RETURN (theErr);
} /* if */
thePos = 0L;
theGroup = Extract2Bytes (theTmpBufP, &thePos);
theElement = Extract2Bytes (theTmpBufP, &thePos);
theLength = Extract4Bytes (theTmpBufP, &thePos);
/* Pixel data fragment not found when expected */
if ((theGroup != 0xFFFE) || (theElement != 0xE000)) RETURN (papBadArgument);
/* read 4 chars from the file = number of segments */
theTmpBufP = (PapyUChar *) &theTmpBuf [0];
i = 4L;
thePos = 0L; /* grNb, elemNb & elemLength */
if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &i, 1L, theTmpBufP)) < 0)
{
Papy3FClose (&gPapyFile [inFileNb]);
RETURN (theErr);
} /* if */
theNbOfSegments = Extract4Bytes (theTmpBufP, &thePos);
if (theNbOfSegments > 3L) RETURN (papWrongValue); /* we allow to read 8, 16 and 32 bit images */
/* read theOffset1, theOffset2, theOffset3 and skip 48 bytes */
theTmpBufP = (PapyUChar *) &theTmpBuf [0];
i = 12L;
thePos = 0L;
/* grNb, elemNb & elemLength */
if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &i, 1L, theTmpBufP)) < 0)
{
Papy3FClose (&gPapyFile [inFileNb]);
RETURN (theErr);
} /* if */
theOffset1 = Extract4Bytes (theTmpBufP, &thePos);
theOffset2 = Extract4Bytes (theTmpBufP, &thePos);
theOffset3 = Extract4Bytes (theTmpBufP, &thePos);
Papy3FSeek (gPapyFile [inFileNb], SEEK_CUR, (PapyLong) 48L);
if (theNbOfSegments == 1)
{
/* read rle image */
theRleLen = theLength - 64L;
theRleP = (PapyUChar *) emalloc3 ((PapyULong) (theRleLen + 10L));
/* extract the image from the file */
theErr = Papy3FRead (gPapyFile [inFileNb], (PapyULong *) &theRleLen, 1L, (void *) theRleP);
DecodeRLESegment (inFileNb, ioImage16P, theRleP, theRleLen, theNbOfSegments, 1);
/* delete rle image */
efree3 ((void **) &theRleP);
}/* if ...single segment */
else if (theNbOfSegments == 2)
{
/* deal with first segment */
theRleLen = theOffset2 - 64L;
theRleP = (PapyUChar *) emalloc3 ((PapyULong) (theRleLen + 10L));
/* extract the image from the file */
theErr = Papy3FRead (gPapyFile [inFileNb], (PapyULong *) &theRleLen, 1L, (void *) theRleP);
DecodeRLESegment (inFileNb, ioImage16P, theRleP, theRleLen, theNbOfSegments, 2);
/* delete rle image */
efree3 ((void **) &theRleP);
/* deal with second segment */
theRleLen = theLength - theOffset2;
theRleP = (PapyUChar *) emalloc3 ((PapyULong) (theRleLen + 10L));
/* extract the image from the file */
theErr = Papy3FRead (gPapyFile [inFileNb], (PapyULong *) &theRleLen, 1L, (void *) theRleP);
DecodeRLESegment (inFileNb, ioImage16P, theRleP, theRleLen, theNbOfSegments, 1);
/* delete rle image */
efree3 ((void **) &theRleP);
}/* if ...two segments */
else if (theNbOfSegments == 3)
{
/* deal with first segment */
theRleLen = theOffset2 - 64L;
theRleP = (PapyUChar *) emalloc3 ((PapyULong) (theRleLen + 10L));
/* extract the image from the file */
theErr = Papy3FRead (gPapyFile [inFileNb], (PapyULong *) &theRleLen, 1L, (void *) theRleP);
DecodeRLESegment (inFileNb, ioImage16P, theRleP, theRleLen, theNbOfSegments, 1);
/* delete rle image */
efree3 ((void **) &theRleP);
/* deal with second segment */
theRleLen = theOffset3 - theOffset2;
theRleP = (PapyUChar *) emalloc3 ((PapyULong) (theRleLen + 10L));
/* extract the image from the file */
theErr = Papy3FRead (gPapyFile [inFileNb], (PapyULong *) &theRleLen, 1L, (void *) theRleP);
DecodeRLESegment (inFileNb, ioImage16P, theRleP, theRleLen, theNbOfSegments, 2);
/* delete rle image */
efree3 ((void **) &theRleP);
/* deal with third segment */
theRleLen = theLength - theOffset3;
theRleP = (PapyUChar *) emalloc3 ((PapyULong) (theRleLen + 10L));
/* extract the image from the file */
theErr = Papy3FRead (gPapyFile [inFileNb], (PapyULong *) &theRleLen, 1L, (void *) theRleP);
DecodeRLESegment (inFileNb, ioImage16P, theRleP, theRleLen, theNbOfSegments, 3);
/* delete rle image */
efree3 ((void **) &theRleP);
} /* if ...three segments */
return 0;
} /* endof ExtractRLE */
/********************************************************************************/
/* */
/* Papy3GetPixelData : gets the specified image or icon and put it either */
/* in the passed module or group. The moduleId parameter should contain */
/* the value IconImage if one wants to extract an icon or ImagePixel if */
/* one wants to extract the image itsself, wether a group or a module has */
/* been passed to the routine (this is important). */
/* BEWARE : in case of extracting the pixel data to a module, you should */
/* have gotten the module before calling this routine. */
/* in case of extracting the pixel data to a group, you should */
/* have read the group 0x0028 and the group 0x7FE0 before calling this */
/* routine. */
/* return : the image, or NULL if something went wrong */
/* */
/********************************************************************************/
PapyUShort * CALLINGCONV
Papy3GetPixelData (PapyShort inFileNb, int inImageNb, SElement *inGrOrModP, int inModuleId)
{
PapyUChar *theBufP, theTmpBuf [256], *theTmpBufP;
PapyUChar *theCharP, theChar0, theChar1;
PapyUShort *theUShortP, theUShort1, theUShort2;
PapyShort theErr;
int theFrameCount = 1, theLoop, ok, theIsModule;
PAPY_FILE theFp;
PapyULong theBytesToRead, i, theULong, thePos, *theOffsetTableP;
PapyULong theRefPoint, thePixelStart;
SElement *theElemP; /* work pointer on the element of the module */
/* some usefull tests */
if (inImageNb > gArrNbImages [inFileNb] || inModuleId > END_MODULE) return NULL;
/* test to learn if the routine was passed a module or a group in parameter */
if (inGrOrModP->group == 0x0028) theIsModule = TRUE;
else theIsModule = FALSE;
theOffsetTableP = NULL;
/* get the file pointer from the file number */
theFp = gPapyFile [inFileNb];
/* position the file pointer to the pixel data to read */
switch (inModuleId)
{
case IconImage :
/* only allow to get an icon from a PAPYRUS 3 file */
if (gIsPapyFile [inFileNb] != PAPYRUS3) return NULL;
/* it is one of the pointer sequence module, so go to the given ptr sequence */
if (Papy3FSeek (gPapyFile [inFileNb], (int) SEEK_SET, (PapyLong) gOffsetToPtrSeq [inFileNb] + 8L) != 0)
return NULL;
/* look for the given item of the ptr seq */
for (i = 1L; i < (PapyULong) inImageNb; i++)
{
theBytesToRead = Papy3ExtractItemLength (inFileNb);
if (Papy3FSeek (gPapyFile [inFileNb], (int) SEEK_CUR, (PapyLong) theBytesToRead) != 0)
return NULL;
} /* for */
/* then points to the first element of the item */
if (Papy3FSeek (gPapyFile [inFileNb], (int) SEEK_CUR, (PapyLong) 8L) != 0)
return NULL;
/* look now for the right group, i.e. image */
if ((theErr = Papy3GotoGroupNb (inFileNb, 0x7FE0)) < 0) return NULL;
/* ... then the right element */
theErr = Papy3GotoElemNb (inFileNb, 0x7FE0, 0x0010, &theBytesToRead);
/* jump over the description of the element */
if (gArrTransfSyntax [inFileNb] == LITTLE_ENDIAN_IMPL)
{
if (Papy3FSeek (gPapyFile [inFileNb], (int) SEEK_CUR, (PapyLong) 8L) != 0)
return NULL;
} /* if */
else if (gArrTransfSyntax [inFileNb] == LITTLE_ENDIAN_EXPL)
{
if (Papy3FSeek (gPapyFile [inFileNb], (int) SEEK_CUR, (PapyLong) 12L) != 0)
return NULL;
} /* else */
/* position to the right element knowing if it is a group or a module */
if (theIsModule)
theElemP = inGrOrModP + papPixelDataII;
else
theElemP = inGrOrModP + papPixelDataGr;
break;
case ImagePixel :
/* go to the begining of the specified image */
if (Papy3FSeek (gPapyFile [inFileNb], (int) SEEK_SET, (PapyLong) *(gRefPixelOffset [inFileNb] + inImageNb - 1)) != 0)
return NULL;