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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
|
/********************************************************************************/
/* */
/* 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 osiris@sim.hcuge.ch */
/* 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 : PapyConvertFile3.c */
/* Function : contains all the Convertion function */
/* Authors : Marianne Logean */
/* */
/* History : 12.1999 version 1.0 */
/* 04.2001 version 3.7 */
/* 09.2001 version 3.7 on CVS */
/* 11.2001 Modify T1_taille for RGB */
/* */
/********************************************************************************/
/* ------------------------- includes ---------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CHECK_MEMORY
#define HAVE_BOOLEAN
#include "JPEGLESS.H" /* interface for JPEG lossless */
#include "JPEGLIB.H" /* interface for JPEG lossy */
#ifndef Papyrus3H
#include "Papyrus3.h"
#endif
struct color /* a color is defined by its red, green and blue intensity */
{
int r;
int g;
int b;
};
/********************************************************************************/
/* */
/* ExtractSelection : */
/* return : */
/* */
/********************************************************************************/
PapyUShort *
ExtractSelection(PapyUShort *image_buffer, int image_width,
int depth, int x1, int y1, int x2, int y2)
{
PapyUShort i, j, decal;
PapyULong sel_dim;
sel_dim = (PapyULong)((PapyULong)(x2 - x1) * (y2 - y1));
decal = image_width * y1;
if (depth == 8)
{
PapyUChar *sel_bufferC, *bufferC, *imageChar;
PapyUChar *image = (PapyUChar *) emalloc3 ((PapyULong) sel_dim + 1L);
sel_bufferC = image;
/*bufferC = (PapyUChar *) image_buffer;
bufferC += decal;
for (i = y1; i < y2; i++)
{
bufferC += x1;
for (j = x1; j < x2; j++)
{
*sel_bufferC++ = *bufferC++;
}
bufferC += (image_width - x2);
}*/
imageChar = (PapyUChar *) image_buffer;
for (j = 0; j < (y2 - y1); j++)
{
bufferC = imageChar + ((long)x1 + image_width * (y1 + j));
for (i = 0; i < (x2 - x1); i++)
*sel_bufferC++ = *bufferC++;
}/* endfor */
return ((PapyUShort *) image);
}
else
{
PapyUShort *sel_bufferS, *bufferS;
PapyUShort *image = (PapyUShort *) emalloc3 ((PapyULong) sel_dim * sizeof (PapyUShort) + 1L);
sel_bufferS = image;
/*bufferS = (PapyUShort *) image_buffer;
bufferS += decal;
for (i = y1; i < y2; i++)
{
bufferS += x1;
for (j = x1; j < x2; j++)
{
*sel_bufferS++ = *bufferS++;
}
bufferS += (image_width - x2);
}*/
for (j = 0; j < (y2 - y1); j++)
{
bufferS = image_buffer + ((long)x1 + image_width * (y1 + j));
for (i = 0; i < (x2 - x1); i++)
*sel_bufferS++ = *bufferS++;
}/* endfor */
return ((PapyUShort *) image);
}
} /* endof ExtractSelection */
/********************************************************************************/
/* */
/* GetPapyFileType : */
/* return : the format of the given file */
/* */
/********************************************************************************/
int GetPapyFileType (char *filename, int *imageNb, int *imageNo, enum EModality *modality)
{
/* verify if it is Papyrus3 or dicom file or Papyrus2 */
PapyShort file;
int fileKind;
PapyShort theErr;
int theElemType;
PapyULong theNbVal;
UValue_T *theValP;
SElement *theGroup20P;
if ((file = Papy3FileOpen (filename, (PAPY_FILE) 0, TRUE, 0)) >= 0)
{
*imageNb = (int) Papy3GetNbImages (file);
*modality = (enum EModality) Papy3GetModality (file);
/* if it is a DICOM file: search the dicom image number from the serie */
if (gIsPapyFile [file] == DICOM10 || gIsPapyFile [file] == DICOM_NOT10)
{
/* Image no in Dicom serie */
*imageNo = 0;
Papy3GotoNumber (file, 1, DataSetID);
/* goto group 0x0020 */
if ((theErr = Papy3GotoGroupNb (file, 0x0020)) == 0)
{
/* read group 0x0020 from the file */
if ((theErr = Papy3GroupRead (file, &theGroup20P)) > 0)
{
/* ACQUISITION NUMBER */
theValP = Papy3GetElement (theGroup20P, papAcquisitionNumberGr, &theNbVal, &theElemType);
if (theValP != NULL)
*imageNo = atoi(theValP->a);
/* IMAGE NUMBER */
theValP = Papy3GetElement (theGroup20P, papImageNumberGr, &theNbVal, &theElemType);
if (theValP != NULL)
*imageNo = atoi(theValP->a);
/* free the group 20 */
theErr = Papy3GroupFree (&theGroup20P, TRUE);
}/* read group 0x0020 */
}/* goto group 0x0020 */
}/* if it is a DICOM file */
/* close the file a la Papyrus 3 */
Papy3FileClose (file, TRUE);
fileKind = Papy3GetFileKind (file);
if (fileKind == 1)
return PAPYRUS3;
if (fileKind == 0 || fileKind == 2)
return DICOM10;
else if (fileKind == 3)
return other;
}
else return file;
} /* endof GetPapyFileType */
/********************************************************************************/
/* */
/* TI_taille : resize the original image */
/* return : */
/* */
/********************************************************************************/
unsigned char* TI_taille(unsigned char *ori, int orix, int oriy, int dstx, int dsty,
int depth, int numPlans, long *taille)
{
int n, i, j, k, l, m, linescanned, dstimx, dstimy, diffx, diffy;
float nf;
long oritaille;
unsigned char *tmp;
unsigned char *oriplane;
if (orix > oriy)
nf=(float)((float)orix/dstx);
else
nf=(float)((float)oriy/dsty);
n = (int) nf;
/* use round when divide result is not an integer */
if ((nf - n) > 0) n++;
dstimx = orix/n;
dstimy = oriy/n;
/* bords de l'image a remplir */
diffx = (dstx - dstimx) / 2;
diffy = (dsty - dstimy) / 2;
oritaille = (long)orix * (long)oriy;
*taille = (long)dstx * (long)dsty;
tmp = (unsigned char *) emalloc3 (*taille * (long) numPlans * sizeof(unsigned char));
/*for (i=0;i<dstx;i++)
for(j=0;j<dsty;j++)
tmp[i+j*dstx]=ori[i*n+j*n*orix];
*/
/* image RGB contient trois plans entrelacs: numPlans = 3 */
oriplane = (unsigned char *) ori;
/* top of the image */
for (i = 0, l = 0; l < diffy; l++)
for (j = 0; j < dstx; j++)
for (m = 0; m < numPlans; m++) tmp [i++] = 0;
for (linescanned = 0,j=0, k=0; i<*taille * (long) numPlans; )
{
/* left side */
if (j == 0)
for (j = 0; j < diffx; j++)
for (m = 0; m < numPlans; m++) tmp [i++] = 0;
/* right side */
if (j == dstimx + diffx)
{
for (j = dstimx + diffx; j < dstx; j++)
for (m = 0; m < numPlans; m++) tmp [i++] = 0;
/* eof line : j == dstx */
j = 0;
linescanned += n;
k = linescanned * orix * numPlans;
}
else if (k < oritaille * (long) numPlans)
{
for (m = 0; m < numPlans; m++) tmp[i++] = oriplane[k + m];
k += n * (long) numPlans;
j++;
}
/* bottom of the image */
else for (m = 0; m < numPlans; m++) tmp [i++] = 0;
}
efree3((void**)&ori);
ori = (unsigned char*) emalloc3 (*taille*(long)numPlans*sizeof(unsigned char));
(void) memcpy(ori,tmp,*taille*(long)numPlans*sizeof(unsigned char));
efree3((void**)&tmp);
return(ori);
} /* endof TI_taille */
/********************************************************************************/
/* */
/* Compute8bitsImage : */
/* return : */
/* */
/********************************************************************************/
PapyUChar *Compute8bitsImage (PapyUShort *inPixmap, int inRows, int inColumns,
int inDepth, int inMin, int inMax)
{
long i, size;
PapyUShort *p16b; /* pixels of image */
PapyUChar *newPixmap, *p8b;
/* ======================== NEW ORealImage ================================= */
/* as we do not want to write separate zoom algorithms */
/* for 8 bit and 16 bit images, we just work on 16 bit images */
/* so in case of a 16 bit image, just take the pointer on it */
/* in case of an 8 bit image, create a new 16 bit pixmap */
/* get original color fork min and max */
/*forkMin = fOriginalImage->GetColorManager()->FromCalibratedToRaw (fOriginalImage->GetColorManager()->fMinForkCalib);
forkMax = fOriginalImage->GetColorManager()->FromCalibratedToRaw (fOriginalImage->GetColorManager()->fMaxForkCalib);
*/
/* ============================ 8BITS IMAGE ================================ */
size = inColumns * inRows;
newPixmap = (PapyUChar *) emalloc3 ((PapyULong) size);
p8b = newPixmap;
p16b = inPixmap;
if (inDepth > 8)
{
/* conversion to 8 bit image
convert the 16 bit image into 8 bit image */
unsigned short *tab; /* conversion array */
unsigned short dminmax = inMax - inMin;
int min = inMin;
int max = inMax;
if (min < 0) min = 0;
if (max < 0) max = 0;
tab = (unsigned short *) emalloc3 (65535L * sizeof(unsigned short)); /* conversion array */
for (i=min; i<=max; i++)
tab [i] = (unsigned short) (((long)(i - min)) * 255 / dminmax);
for (i=0; i<min; i++)
tab [i] = 0;
for (i=max; i<65535; i++)
tab [i] = 255;
for (i=0; i<size; i++)
*p8b++ = (unsigned char) tab [*p16b++];
efree3 ( (void **) &tab);
}/* endif */
else
{
/* just copy the pixmap */
for (i=0; i<size; i++)
*p8b++ = (unsigned char) *p16b++;
}/* endelse */
return newPixmap;
} /* endof Compute8bitsImage */
/********************************************************************************/
/* */
/* InitClut : initialize clut */
/* return : */
/* */
/********************************************************************************/
void InitClut (int val, struct color thisClut[])
{
int i;
if (val < 0)
for (i=0; i<256; i++)
{
thisClut[i].r = i;
thisClut[i].g = i;
thisClut[i].b = i;
}/* endfor */
else
for (i=0; i<256; i++)
{
thisClut[i].r = val;
thisClut[i].g = val;
thisClut[i].b = val;
} /* endfor */
}/* endof InitClut */
/********************************************************************************/
/* */
/* Papyrus2Papyrus : convert Papyrus3 file into Papyrus3 format */
/* return : */
/* */
/********************************************************************************/
int Papyrus2Papyrus (char *inPapyrusFilename, char *outPapyrusFilename,
PAPY_FILE aRefNum, int nbImages, int *tabImage)
{
PapyShort fp, fpOrig, nbElemInModule;
int nbImagesOrig, imageNo, *tabIm, moduleCreated;
Item *dataSet;
Module *module;
SElement *gr, *group;
PapyUShort *image, valUS, bitsAllocated, rows, columns, selectedrows, selectedcolumns;
int loop, err;
enum EModality mod;
char myString [256], *myStringPtr;
Data_Set *wrkDS;
UValue_T *val;
int valType, isOpenOrClose;
PapyULong nbVal;
int leftcolumns, toprows, rightcolumns, bottomrows;
enum ETransf_Syntax syntax;
PapyUShort *selectedImage;
/* initialize */
myStringPtr = myString;
tabIm = tabImage;
/* open the original file a la Papyrus 3 */
fpOrig = Papy3FileOpen (inPapyrusFilename, (PAPY_FILE) 0, TRUE, 0);
/* test if the file has been opened correctly */
if (fpOrig < 0) return (int) fpOrig;
nbImagesOrig = (int) Papy3GetNbImages (fpOrig);
mod = (enum EModality) Papy3GetModality (fpOrig);
/* enum EPap_Compression {NONE, JPEG_LOSSLESS, JPEG_LOSSY, RLE}; */
syntax = LITTLE_ENDIAN_EXPL;
#ifdef Mac
isOpenOrClose = FALSE;
#else
isOpenOrClose = TRUE;
aRefNum = (PAPY_FILE)1;
#endif
fp = Papy3FileCreate (outPapyrusFilename, aRefNum,
(PapyUShort) nbImages, syntax, gCompression,
mod, isOpenOrClose, PAPYRUS3, NULL);
if (fp < 0) { PAPY3PRINTERRMSG (); return -1;}
/* get a pointer to the group 2 (File Meta Information) */
gr = Papy3GetGroup2 (fp);
/* fill the necessary elements of this group */
/* SOP instance UID of this data set */
strcpy (myStringPtr, "64.572.218.916");
Papy3PutElement (gr, papMediaStorageSOPInstanceUIDGr, &myStringPtr);
/* who is the creator of this wonderfull file ? */
strcpy (myStringPtr, "PAPYRUS 3.0");
Papy3PutElement (gr, papSourceApplicationEntityTitleGr, &myStringPtr);
/* loop on the images */
for (imageNo = 1; imageNo <= nbImagesOrig, *tabIm == 1; imageNo++, tabIm++)
{
/* creation of the data set object for this image */
dataSet = Papy3CreateDataSet (fp);
/* loop on the modules building a image */
wrkDS = gArrModalities [mod];
nbElemInModule = Papy3GetNbElemInModule (mod);
for (loop = 0; loop < nbElemInModule; loop++)
{
moduleCreated = FALSE;
/* get the module from the original file but it could be blank ... */
module = Papy3GetModule (fpOrig, (short) imageNo, wrkDS->moduleName);
if (module == NULL)
{
/* we have to create the module */
module = Papy3CreateModule (dataSet, wrkDS->moduleName);
moduleCreated = TRUE;
} /* if ...an error occured (bad DICOM file ?) */
/* depending on the module add the modified elements */
switch (wrkDS->moduleName)
{
case Patient :
/* if we dont want to save the patient name (anonymous file) */
/*if (hidePatName == TRUE)
{
err = Papy3ClearElement (module, papPatientsNameP, TRUE);
strcpy (myStringPtr, "Anonymous");
Papy3PutElement (module, papPatientsNameP, &myStringPtr);
err = Papy3ClearElement (module, papOtherPatientNamesP, TRUE);
}*/ /* if ...hide patient name */
break;
case GeneralStudy :
/* build a foo Study Instance UID */
if (module [papStudyInstanceUIDGS].nb_val == 0)
{
strcpy (myStringPtr, "1.2.756.9999.999.99.9");
Papy3PutElement (module, papStudyInstanceUIDGS, &myStringPtr);
} /* if */
break;
case GeneralSeries :
/* build a foo Series Instance UID */
if (module [papSeriesInstanceUIDGS].nb_val == 0)
{
strcpy (myStringPtr, "1.2.756.9999.999.99.9");
Papy3PutElement (module, papSeriesInstanceUIDGS, &myStringPtr);
} /* if */
break;
case FrameOfReference :
if (module [papFrameofReferenceUID].nb_val == 0L)
{
strcpy (myStringPtr, "41.22.333.444.555.666.00.1");
Papy3PutElement (module, papFrameofReferenceUID, &myStringPtr);
} /* if */
break;
case GeneralImage :
if (gCompression == NONE)
strcpy (myStringPtr, "00");
else
strcpy (myStringPtr, "01");
Papy3PutElement (module, papLossyImageCompressionGI, &myStringPtr);
break;
case ImagePixel :
{
Papy3ClearElement (module, papSamplesperPixelIP, TRUE);
valUS = 1;
Papy3PutElement (module, papSamplesperPixelIP, &valUS);
if (module [papPixelRepresentationIP].nb_val == 0)
{
valUS = 0;
Papy3PutElement (module, papPixelRepresentationIP, &valUS);
}
val = Papy3GetElement (module, papRows, &nbVal, &valType);
rows = val->us;
val = Papy3GetElement (module, papColumns, &nbVal, &valType);
columns = val->us;
leftcolumns = (int) (gLeftX*columns);
toprows = (int) (gTopY*rows);
rightcolumns = (int) (gRightX*columns);
bottomrows = (int) (gBottomY*rows);
Papy3ClearElement (module, papRows, TRUE);
selectedrows = (unsigned short) (bottomrows - toprows);
Papy3PutElement (module, papRows, &selectedrows);
Papy3ClearElement (module, papColumns, TRUE);
selectedcolumns = (unsigned short) (rightcolumns - leftcolumns);
Papy3PutElement (module, papColumns, &selectedcolumns);
/* bits allocated and stored */
val = Papy3GetElement (module, papBitsAllocatedIP, &nbVal, &valType);
bitsAllocated = val->us;
/* min and max pixel value in the image */
/* valUS = (PapyUShort) newRealImage->GetMinImage ();
Papy3PutElement (module, papSmallestImagePixelValue, &valUS);
valUS = (PapyUShort) newRealImage->GetMaxImage ();
Papy3PutElement (module, papLargestImagePixelValue, &valUS);
*/
/* PIXEL DATA */
/*image = (PapyUShort *)Papy3GetPixelData (fpOrig, imageNo, module, ImagePixel);*/
Papy3GotoNumber (fpOrig, imageNo, DataSetID);
/* then goto group 0x7FE0 */
Papy3GotoGroupNb (fpOrig, 0x7FE0);
Papy3GroupRead (fpOrig, &group);
/* get the original image because image not present in the module */
/* with Papy3GetModule () */
image = (PapyUShort *)Papy3GetPixelData (fpOrig, imageNo, group, ImagePixel);
selectedImage = ExtractSelection (image, columns, bitsAllocated,
leftcolumns, toprows,
rightcolumns, bottomrows);
efree3 ((void **) &image);
/* put the image */
Papy3PutImage ((PapyShort)fp, module, papPixelData, selectedImage,
(PapyUShort) selectedrows, (PapyUShort) selectedcolumns,
(PapyUShort) bitsAllocated, 0L);
Papy3GotoNumber (fpOrig, imageNo, DataSetID);
}
break;
case CTImage:
{
Papy3ClearElement (module, papReconstructionDiameterCTI, TRUE);
/* if (!strncmp (fMedStudy->GetPixelSizeUnit (), "mm", 2))
{
float pixelSize = fMedStudy->GetPixelSize ();
pixelSize = pixelSize * 100 / fParam->zoomFactor;
sprintf (myStringPtr, "%.5f", (float) (pixelSize * selectedcolumns));
Papy3PutElement (module, papReconstructionDiameterCTI, &myStringPtr);
} */ /* if ...image has been callibrated */
}
break;
case VOILUT :
{
/* compute the WW and WL of the image */
/* int WW = fSaveImage->GetForkMax () - fSaveImage->GetForkMin () + 1;
int WL = (int) ((WW / 2) + fSaveImage->GetForkMin ());
*/
/* clear any previous value */
Papy3ClearElement (module, papWindowCenter, TRUE);
Papy3ClearElement (module, papWindowWidth, TRUE);
/* then put the new one */
/* IntToString (WL, myStringPtr);
Papy3PutElement (module, papWindowCenter, &myStringPtr);
IntToString (WW, myStringPtr);
Papy3PutElement (module, papWindowWidth, &myStringPtr);
*/ }
break;
default :
break;
} /* switch ...add the modified elements */
/* link the read module to the list of modules of the data set to write */
if (!moduleCreated)
Papy3LinkModuleToDS (dataSet, module, wrkDS->moduleName);
wrkDS++;
} /* for ...loop on the modules of the CT images */
/* close the data set and frees the modules */
err = Papy3CloseDataSet (fp, dataSet, TRUE, TRUE);
} /* endfor ...loop on the images */
/* close the original file */
err = (int) Papy3FileClose (fpOrig, TRUE);
/* close the file */
err = Papy3WriteAndCloseFile (fp, TRUE);
if (err < 0) { PAPY3PRINTERRMSG (); return -1;}
} /* endof Papyrus2Papyrus */
/********************************************************************************/
/* */
/* Papyrus2Dicom : convert Papyrus3 file into DICOM format */
/* return : */
/* */
/********************************************************************************/
int Papyrus2Dicom (char *inPapyrusFilename, char *outDicomFilename, PAPY_FILE aRefNum,
int nbImages, int *tabImage)
{
PapyShort fp, fpOrig;
int nbImagesOrig, imageNo, *tabIm;
/* PapyUShort *image, bitsAllocated, rows, columns; */
enum EModality mod;
char myString [256], *myStringPtr;
int isOpenOrClose;
/*int leftcolumns, toprows, rightcolumns, bottomrows; */
enum ETransf_Syntax syntax;
/* PapyUShort *selectedImage; */
PapyShort theErr = 0;
long theFileSize;
PAPY_FILE thePapyFile;
/* initialize */
myStringPtr = myString;
tabIm = tabImage;
/* open the original file a la Papyrus 3 */
fpOrig = Papy3FileOpen (inPapyrusFilename, (PAPY_FILE) 0, TRUE, 0);
/* test if the file has been opened correctly */
if (fpOrig < 0) return (int) fpOrig;
thePapyFile = (PAPY_FILE) Papy3GetFile (fpOrig);
/* get the fileSize */
theErr = Papy3FSeek (thePapyFile, SEEK_END, 0L);
theErr = Papy3FTell (thePapyFile, (PapyLong *) &theFileSize);
theErr = Papy3FSeek (thePapyFile, SEEK_SET, 0L);
nbImagesOrig = (int) Papy3GetNbImages (fpOrig);
mod = (enum EModality) Papy3GetModality (fpOrig);
/* enum EPap_Compression {NONE, JPEG_LOSSLESS, JPEG_LOSSY, RLE}; */
syntax = LITTLE_ENDIAN_EXPL;
#ifdef Mac
isOpenOrClose = FALSE;
#else
isOpenOrClose = TRUE;
aRefNum = (PAPY_FILE) 1;
#endif
SetCompression (Papy3GetCompression (fpOrig));
fp = Papy3FileCreate (outDicomFilename, aRefNum,
(PapyUShort) nbImages, syntax, gCompression,
mod, isOpenOrClose, DICOM10, NULL);
if (fp < 0) { PAPY3PRINTERRMSG (); return -1;}
/* loop on the images */
for (imageNo = 1; imageNo <= nbImagesOrig, *tabIm == 1; imageNo++, tabIm++)
{
PAPY_FILE theFp;
PapyULong theBufSize, theMetaInfoSize;
unsigned char *theBuffP;
void *theVoidP;
/* create the temporary file that will contain the given data set */
if ((theErr = CreateTmpFile3 (fp, &theFp, &theVoidP)) < 0)
RETURN (papFileCreationFailed);
/* if the file is a DICOM one, then put the DICOM header to the temp file */
/* in order to get a real DICOM file part 10 compliant */
theErr = WriteDicomHeader3 (theFp, fp, &theMetaInfoSize);
if (imageNo == nbImagesOrig)
/* compute DataSet size for this image */
theBufSize = theFileSize - *(gRefImagePointer [fpOrig] + imageNo - 1);
else
/* compute DataSet size for this image */
theBufSize = *(gRefImagePointer [fpOrig] + imageNo) -
*(gRefImagePointer [fpOrig] + imageNo - 1);
/* alloc the buffer that will contain the ready to write group */
theBuffP = (unsigned char *) emalloc3 ((PapyULong) theBufSize);
/* position the file pointer to the begining of the data set */
theErr = Papy3GotoNumber (fpOrig, imageNo, DataSetID);
theErr = (PapyShort) Papy3FRead (gPapyFile [fpOrig], &theBufSize, 1L, theBuffP);
/* write the buffer to the temporary file */
if ((theErr = WriteGroup3 (theFp, theBuffP, theBufSize)) < 0)
RETURN (theErr);
/* frees the allocated buffer */
efree3 ((void **) &theBuffP);
/* close the file */
Papy3FClose (&theFp);
} /* loop on the images */
} /* endof Papyrus2Dicom */
/********************************************************************************/
/* */
/* ReadDicomFile : */
/* return : */
/* */
/********************************************************************************/
int ReadDicomFile (char *inDicomFilename, PapyShort inPapyrusFilePointer)
{
enum EModality mod;
PapyShort dicomFilePointer;
Data_Set *wrkDS;
Module *module;
Item *dataSet;
int nbImagesOrig, noImage;
int moduleCreated, loop, err = 0;
PapyUShort *image, bitsAllocated, rows, columns, samplesPerPixel;
UValue_T *val;
int valType;
PapyULong nbVal;
SElement *group;
/* open the original file a la Papyrus 3 */
dicomFilePointer = Papy3FileOpen (inDicomFilename, (PAPY_FILE) 0, TRUE, 0);
/* test if the file has been opened correctly */
if (dicomFilePointer < 0) return (int) dicomFilePointer;
mod = (enum EModality) Papy3GetModality (dicomFilePointer);
/* same MODALITY */
if (mod != gFileModality [inPapyrusFilePointer]) return (-1);
/* is it a multi-frame dicom file */
nbImagesOrig = (int) Papy3GetNbImages (dicomFilePointer);
for (noImage = 1; noImage <= nbImagesOrig; noImage++)
{
/* goto the image */
Papy3GotoNumber (dicomFilePointer, noImage, DataSetID);
/* creation of the data set object for this image */
dataSet = Papy3CreateDataSet (inPapyrusFilePointer);
/* loop on the modules */
wrkDS = gArrModalities [mod];
for (loop = 0; loop < gArrModuleNb [mod]; loop++)
{
moduleCreated = FALSE;
/* get the module from the original file but it could be blank ... */
module = Papy3GetModule (dicomFilePointer, (short) noImage, wrkDS->moduleName);
/*if (module == NULL)
{
/* we have to create the module */
/* module = Papy3CreateModule (dataSet, wrkDS->moduleName);
moduleCreated = TRUE;
} /* if ...an error occured (bad DICOM file ?) */
/* depending on the module add the modified elements */
switch (wrkDS->moduleName)
{
case ImagePixel :
{
val = Papy3GetElement (module, papRows, &nbVal, &valType);
rows = val->us;
val = Papy3GetElement (module, papColumns, &nbVal, &valType);
columns = val->us;
/*
leftcolumns = (int) (leftX*columns);
toprows = (int) (topY*rows);
rightcolumns = (int) (rightX*columns);
bottomrows = (int) (bottomY*rows);
Papy3ClearElement (module, papRows, TRUE);
selectedrows = (unsigned short) (bottomrows - toprows);
Papy3PutElement (module, papRows, &selectedrows);
Papy3ClearElement (module, papColumns, TRUE);
selectedcolumns = (unsigned short) (rightcolumns - leftcolumns);
Papy3PutElement (module, papColumns, &selectedcolumns);
*/
/* bits allocated and stored */
val = Papy3GetElement (module, papBitsAllocatedIP, &nbVal, &valType);
bitsAllocated = val->us;
/* samples per pixel */
val = Papy3GetElement (module, papSamplesperPixelIP, &nbVal, &valType);
samplesPerPixel = val->us;
/* min and max pixel value in the image */
/*
valUS = (PapyUShort) newRealImage->GetMinImage ();
Papy3PutElement (module, papSmallestImagePixelValue, &valUS);
valUS = (PapyUShort) newRealImage->GetMaxImage ();
Papy3PutElement (module, papLargestImagePixelValue, &valUS);
*/
/* PIXEL DATA */
/*image = (PapyUHShort *)Papy3GetPixelData (fpOrig, imageNb, module, ImagePixel);*/
Papy3GotoNumber (dicomFilePointer, noImage, DataSetID);
/* then goto group 0x7FE0 */
Papy3GotoGroupNb (dicomFilePointer, 0x7FE0);
Papy3GroupRead (dicomFilePointer, &group);
/* get the original image because image not present in the module */
/* with Papy3GetModule () */
image = (PapyUShort *)Papy3GetPixelData (dicomFilePointer, noImage, group, ImagePixel);
/*selectedImage = ExtractSelection (image, columns, bitsAllocated,
leftcolumns, toprows,
rightcolumns, bottomrows);
efree3 ((void **) &image);
/* put the image */
/*Papy3PutImage ((PapyShort)inPapyrusFilePointer, module, papPixelData, selectedImage,
(PapyUShort) selectedrows, (PapyUShort) selectedcolumns,
(PapyUShort) bitsAllocated, 0L);*/
Papy3PutImage ((PapyShort)inPapyrusFilePointer, module, papPixelData, image,
(PapyUShort) rows, (PapyUShort) columns,
(PapyUShort) (bitsAllocated * samplesPerPixel), 0L);
Papy3GotoNumber (dicomFilePointer, noImage, DataSetID);
}
break;
default :
break;
} /* switch ...add the modified elements */
/* link the read module to the list of modules of the data set to write */
/*if (!moduleCreated)*/
if (module != NULL)
Papy3LinkModuleToDS (dataSet, module, wrkDS->moduleName);
wrkDS++;
} /* for ...loop on the modules of the image */
/* close the data set and frees the modules */
err = Papy3CloseDataSet (inPapyrusFilePointer, dataSet, TRUE, FALSE);
} /* endfor ...loop on the images */
/* close the original file */
err = (int) Papy3FileClose (dicomFilePointer, TRUE);
} /* endof ReadDicomFile */
/********************************************************************************/
/* */
/* Dicom2Papyrus : convert DICOM serie into a single papyrus file */
/* return : */
/* */
/********************************************************************************/
int Dicom2Papyrus (char *outPapyrusFilename, int inNbDicomImages, char **inDicomFilename,
int inIsSerie, enum EModality modality)
{
int i, isOpenOrClose, err = 0;
PAPY_FILE aRefNum;
PapyShort papyrusFilePointer;
char *dicomPath;
enum ETransf_Syntax syntax;
SElement *gr;
char myString [256], *myStringPtr;
int nbImages, imageNo;
/* initialize */
myStringPtr = myString;
if (inNbDicomImages == 1)
GetPapyFileType (inDicomFilename[1], &nbImages, &imageNo, &modality);
else nbImages = inNbDicomImages;
/* enum EPap_Compression {NONE, JPEG_LOSSLESS, JPEG_LOSSY, RLE}; */
syntax = LITTLE_ENDIAN_EXPL;
isOpenOrClose = TRUE;
aRefNum = (PAPY_FILE)1;
papyrusFilePointer = Papy3FileCreate (outPapyrusFilename, aRefNum,
(PapyUShort) nbImages, syntax, gCompression,
modality, isOpenOrClose, PAPYRUS3, NULL);
/*if (papyrusFilePointer < 0) { PAPY3PRINTERRMSG (); return -1;}*/
if (papyrusFilePointer < 0) { PAPY3PRINTERRMSG (); return (int)papyrusFilePointer;}
/* get a pointer to the group 2 (File Meta Information) */
gr = Papy3GetGroup2 (papyrusFilePointer);
/* fill the necessary elements of this group */
/* SOP instance UID of this data set */
strcpy (myStringPtr, "64.572.218.916");
Papy3PutElement (gr, papMediaStorageSOPInstanceUIDGr, &myStringPtr);
/* who is the creator of this wonderfull file ? */
strcpy (myStringPtr, "PAPYRUS 3.0");
Papy3PutElement (gr, papSourceApplicationEntityTitleGr, &myStringPtr);
for (i = 1; i <= inNbDicomImages; i++)
ReadDicomFile (inDicomFilename[i], papyrusFilePointer);
/* close the file */
err = Papy3WriteAndCloseFile (papyrusFilePointer, TRUE);
if (err < 0) { PAPY3PRINTERRMSG (); return -1;}
/* efree3 ((void **)&dicomPath);*/
} /* endof Dicom2Papyrus */
/********************************************************************************/
/* */
/* Papyrus2Jpeg : convert Papyrus3 file into Jpeg format */
/* return : */
/* */
/********************************************************************************/
int Papyrus2Jpeg (char *inPapyrusFilename, char *outJpegBaseFilename, short aRefNum,
int *inTabImage, int inJpegWidth, int inJpegHeight,
enum EPap_Compression inCompression, int inQuality)
{
char jpegFilename[512];
PapyUChar *theCompPixP, *image;
PapyUShort *pixel, *rgbPixel;
int imageHeight, imageWidth, imageDepth, isSigned, planarConf;
float pixOffset = 0.0, pixSlope = 1.0;
int pixMin = 0, pixMax = 0, windowWidth = 1, windowLevel = 0;
int pixMinCalib = 0, pixMaxCalib = 0;
PapyShort fpOrig;
int nbImagesOrig, noImage, numPlans = 1;
UValue_T *val, *tmpVal;
int err, valType;
PapyULong i, j, nbVal, imageSize, bytesInImage;
SElement *group;
long longWW, longWL;
struct color thisClut[256];
int dimImageJpeg;
/* keep it in order to use it when computing the jpeg filename */
dimImageJpeg = inJpegWidth;
/* open the original file a la Papyrus 3 */
fpOrig = Papy3FileOpen (inPapyrusFilename, (PAPY_FILE) 0, TRUE, 0);
/* test if the file has been opened correctly */
if (fpOrig < 0) return (int) fpOrig;
nbImagesOrig = (int) Papy3GetNbImages (fpOrig);
for (noImage = 1; noImage <= nbImagesOrig; noImage++)
{
/* selected image */
if (inTabImage[noImage - 1] == 1)
{
/* goto the image */
Papy3GotoNumber (fpOrig, noImage, DataSetID);
/* then goto group 0x0028 */
if ((err = Papy3GotoGroupNb (fpOrig, 0x0028)) == 0)
{
/* read group 0x0028 from the file */
if ((err = Papy3GroupRead (fpOrig, &group)) > 0)
{
/* rows */
val = Papy3GetElement (group, papRowsGr, &nbVal, &valType);
imageHeight = (int) val->us;
if (inJpegHeight == 0) inJpegHeight = imageHeight;
/* columns */
val = Papy3GetElement (group, papColumnsGr, &nbVal, &valType);
imageWidth = (int) val->us;
if (inJpegWidth == 0) inJpegWidth = imageWidth;
/* depth */
val = Papy3GetElement (group, papBitsAllocatedGr, &nbVal, &valType);
imageDepth = (int) val->us;
/* image size */
imageSize = (PapyULong)imageHeight * (PapyULong)imageWidth;
bytesInImage = imageSize * ((long) imageDepth / 8L);
/* pixel representation */
val = Papy3GetElement (group, papPixelRepresentationGr, &nbVal, &valType);
if (val != NULL && val->us == 1) isSigned = TRUE;
/* planar configuration */
val = Papy3GetElement (group, papPlanarConfigurationGr, &nbVal, &valType);
if (val != NULL) planarConf = (int) val->us;
/* pixmin */
val = Papy3GetElement (group, papSmallestImagePixelValueGr, &nbVal, &valType);
if (val != NULL)
{
pixMin = (int) val->us;
if (imageDepth == 8 && pixMin > 255) pixMin = 255;
}
/* pixmax */
val = Papy3GetElement (group, papLargestImagePixelValueGr, &nbVal, &valType);
if (val != NULL)
{
pixMax = (int) val->us;
if (imageDepth == 8 && pixMax > 255) pixMax = 255;
}
/* offset */
val = Papy3GetElement (group, papRescaleInterceptGr, &nbVal, &valType);
if (val != NULL)
{
tmpVal = val;
/* get the last offset of the image */
for (i = 1L; i < nbVal; i++) tmpVal++;
pixOffset = (float)atof ((char *)(tmpVal->a));
if (pixOffset < 0) pixOffset = -(pixOffset);
}
/* slope */
val = Papy3GetElement (group, papRescaleSlopeGr, &nbVal, &valType);
if (val != NULL)
{
tmpVal = val;
/* get the last slope of the image */
for (i = 1L; i < nbVal; i++) tmpVal++;
pixSlope = (float)atof ((char *)(tmpVal->a));
}
/* window level */
val = Papy3GetElement (group, papWindowCenterGr, &nbVal, &valType);
if (val != NULL)
{
tmpVal = val;
/* get the last window level of the image */
for (i = 1L; i < nbVal; i++) tmpVal++;
longWL = (long)atof (tmpVal->a);
/* compute the calibrated value */
windowLevel = (int)((longWL * pixSlope) - pixOffset);
} /* if ...val not NULL */
/* window width */
val = Papy3GetElement (group, papWindowWidthGr, &nbVal, &valType);
if (val != NULL)
{
tmpVal = val;
/* get the last window width of the image */
for (i = 1L; i < nbVal; i++) tmpVal++;
longWW = (long)atof (tmpVal->a);
windowWidth = (int)longWW;
} /* if ...val not NULL */
/* look for the presence of an eventual Color Palette */
if (gArrPhotoInterpret [fpOrig] == PALETTE)
{
PapyUShort clutEntryR, clutEntryG, clutEntryB;
PapyUShort clutDepthR, clutDepthG, clutDepthB;
/* initialisation */
clutEntryR = clutEntryG = clutEntryB = 0;
clutDepthR = clutDepthG = clutDepthB = 0;
InitClut (0, thisClut);
/* read the RED descriptor of the color lookup table */
val = Papy3GetElement (group, papRedPaletteColorLookupTableDescriptorGr, &nbVal, &valType);
tmpVal = val;
if (val != NULL)
{
clutEntryR = tmpVal->us;
tmpVal++;tmpVal++;
clutDepthR = tmpVal->us;
} /* if ...read Red palette color descriptor */
/* read the GREEN descriptor of the color lookup table */
val = Papy3GetElement (group, papGreenPaletteColorLookupTableDescriptorGr, &nbVal, &valType);
if (val != NULL)
{
clutEntryG = val->us;
tmpVal = val + 2;
clutDepthG = tmpVal->us;
} /* if ...read Green palette color descriptor */
/* read the BLUE descriptor of the color lookup table */
val = Papy3GetElement (group, papBluePaletteColorLookupTableDescriptorGr, &nbVal, &valType);
if (val != NULL)
{
clutEntryB = val->us;
tmpVal = val + 2;
clutDepthB = tmpVal->us;
} /* if ...read Blue palette color descriptor */
/* EXTRACT THE PALETTE data only if there is 256 entries and depth is 16 bits */
if (clutEntryR == 256 && clutEntryG == 256 && clutEntryB == 256 &&
clutDepthR == 16 && clutDepthG == 16 && clutDepthB == 16)
{
/* extract the RED palette clut data */
val = Papy3GetElement (group, papRedPaletteCLUTDataGr, &nbVal, &valType);
if (val != NULL)
{
for (j = 0, tmpVal = val; j < clutEntryR; j++, tmpVal++)
(thisClut[j]).r = (int) (tmpVal->us/256);
} /* endif */
/* extract the GREEN palette clut data */
val = Papy3GetElement (group, papGreenPaletteCLUTDataGr, &nbVal, &valType);
if (val != NULL)
for (j = 0, tmpVal = val; j < clutEntryG; j++, tmpVal++)
(thisClut[j]).g = (int) (tmpVal->us/256);
/* extract the BLUE palette clut data */
val = Papy3GetElement (group, papBluePaletteCLUTDataGr, &nbVal, &valType);
if (val != NULL)
for (j = 0, tmpVal = val; j < clutEntryB; j++, tmpVal++)
(thisClut[j]).b = (int) (tmpVal->us/256);
} /* if ...the palette has 256 entries and thus we extract the clut datas */
} /* endif ...extraction of the color palette */
else
InitClut (-1, thisClut);
/* free group 28 */
err = Papy3GroupFree (&group, TRUE);
} /* endif ...group 28 read */
} /* endif ...group 28 found */
/* goto the image */
Papy3GotoNumber (fpOrig, noImage, DataSetID);
/* then goto group 0x7FE0 */
if ((err = Papy3GotoGroupNb (fpOrig, 0x7FE0)) == 0)
{
/* read group 0x7FE0 from the file */
if ((err = Papy3GroupRead (fpOrig, &group)) > 0)
{
/* PIXEL DATA */
pixel = (PapyUShort *)Papy3GetPixelData (fpOrig, noImage, group, ImagePixel);
/* if it is a YBR image convert it to a RGB image */
if (gArrPhotoInterpret [fpOrig] == YBR_FULL ||
gArrPhotoInterpret [fpOrig] == YBR_FULL_422 ||
gArrPhotoInterpret [fpOrig] == YBR_PARTIAL_422)
{
rgbPixel = (PapyUShort *) ConvertYbrToRgb ( (PapyUChar *) pixel, imageWidth,
imageHeight, gArrPhotoInterpret [fpOrig],
(char) planarConf);
efree3 ((void **) &pixel);
pixel = rgbPixel;
numPlans = 3;
} /* if ...YBR image */
/* if it is a RGB or a converted to RGB image, convert it to an indexed image */
if (gArrPhotoInterpret [fpOrig] == RGB ||
gArrPhotoInterpret [fpOrig] == YBR_FULL ||
gArrPhotoInterpret [fpOrig] == YBR_FULL_422 ||
gArrPhotoInterpret [fpOrig] == YBR_PARTIAL_422)
{
numPlans = 3;
/* make the rgbPixel point to what it needs to */
/*rgbPixel = pixel;*/
/* allocate room for the indexed resulting image */
/*pixel = (PapyUShort *) ecalloc3 ((PapyULong)imageSize, (PapyULong) sizeof (PapyUShort));
*/
/* interlaced image */
if (planarConf == 0 ||
gArrPhotoInterpret [fpOrig] == YBR_FULL ||
gArrPhotoInterpret [fpOrig] == YBR_FULL_422 ||
gArrPhotoInterpret [fpOrig] == YBR_PARTIAL_422)
/* SetRGBImage ((PapyUChar *) rgbPixel, imageWidth, imageHeight, TRUE); */
;/*ConvertRGBToIndexed((PapyUChar *) pixel, (PapyUChar *) rgbPixel, imageWidth, imageHeight, thisClut); */
/* contiguous plane */
else
{
PapyUChar *Rplane, *Gplane, *Bplane;
Rplane = (PapyUChar *) rgbPixel;
Gplane = Rplane + (imageWidth * imageHeight);
Bplane = Gplane + (imageWidth * imageHeight);
/* SetRGBImage (Rplane, Gplane, Bplane, imageWidth, imageHeight, TRUE); */
;/*ConvertRGBPixToIndexed((PapyUChar *) pixel, Rplane, Gplane, Bplane,
imageWidth, imageHeight, thisClut);*/
} /* else ...contiguous plane */
/* frees the no more needed memory */
/*efree3 ((void **) &rgbPixel);*/
} /* endif ...RGB image */
/* **************** 8 bits image **************** */
if (imageDepth == 8)
{
/* if min and max values of the image not in the file : computes them */
if (pixMin == pixMax || isSigned)
{
pixMin = 0;
pixMax = 255;
}/* endif ...compute min and max val of the image */
/* if ww and wl not in the file, computes them */
if (windowWidth == 1 && windowLevel == 0)
{
windowWidth = 256;
windowLevel = windowWidth / 2;
} /* if ...we have to compute ww and wl */
/* test if the image is stored with inverted pixel values */
if (gArrPhotoInterpret [fpOrig] == MONOCHROME1)
{
PapyULong theLLoop;
PapyUChar *theUCharPix;
PapyUShort invertPix;
theUCharPix = (PapyUChar *) pixel;
invertPix=(PapyUShort)pixMax+(PapyUShort)pixMin;
for (theLLoop = 0L; theLLoop < imageSize; theLLoop++)
{
*theUCharPix = invertPix- *theUCharPix;
theUCharPix++;
} /* for ...image inversion */
} /* endif ...inversion de l'image */
} /* then ...8 bits images */
/* **************** 12 or 16 bits image **************** */
else if (imageDepth == 12 || imageDepth == 16)
{
/* if no min and max image value in the file */
if (pixMin == pixMax || isSigned)
{
/* compute the max allowed value of the pixel given the bits stored */
long maxPixValAllowed = 1;
for (i = 0; i < imageDepth; i++) maxPixValAllowed *= 2;
maxPixValAllowed -= 1;
/* different ways of computing the values given the pixel representation field */
if (isSigned)
{
PapyShort *signImage;
PapyShort *tmpSh;
PapyUShort *tmpUSh;
if (maxPixValAllowed > 32766) maxPixValAllowed = 32766;
signImage = (PapyShort *) pixel;
tmpSh = signImage;
pixMin = (int) *signImage;
if (*signImage > maxPixValAllowed)
{
pixMax = maxPixValAllowed;
pixMin = maxPixValAllowed;
}/* endif */
else
{
pixMax = (int) *signImage;
pixMin = pixMax;
}/* else */
for (nbVal = 0L; nbVal < imageSize; nbVal++, tmpSh++)
{
/* cut the too big values */
if ((int) *tmpSh > maxPixValAllowed) *tmpSh = (short)maxPixValAllowed;
/* look for the min and max pixel values */
if ((int) *tmpSh < pixMin && (int) *tmpSh <= maxPixValAllowed)
pixMin = (int) *tmpSh;
if ((int) *tmpSh > pixMax && (int) *tmpSh <= maxPixValAllowed)
pixMax = (int) *tmpSh;
} /* for ...extract min and max val from the file */
/* we can set a correct offset to have positiv pixel values */
pixOffset += (float) -pixMin;
/* then offset all the pixel values to get a positiv image */
for (nbVal = 0, tmpSh = signImage, tmpUSh = pixel;
nbVal < imageSize;
nbVal++, tmpSh++, tmpUSh++)
*tmpUSh = (PapyUShort) ((PapyShort) *tmpSh - pixMin);
pixMax -= pixMin;
pixMin = 0;
} /* endif ...signed pixel values */
/* else unsigned pixel values */
else
{
PapyUShort *tmpSh;
tmpSh = pixel;
if (*pixel > (PapyUShort) maxPixValAllowed)
{
pixMax = (int) maxPixValAllowed;
pixMin = (int) maxPixValAllowed;
} /* endif */
else
{
pixMax = (int) *pixel;
pixMin = pixMax;
} /* else */
for (nbVal = 0L; nbVal < imageSize; nbVal++, tmpSh++)
{
/* cut the too big values */
if (*tmpSh > (unsigned short) maxPixValAllowed)
*tmpSh = (unsigned short) maxPixValAllowed;
/* look for the min and max pixel values */
if ((int) *tmpSh < pixMin && (long) *tmpSh <= maxPixValAllowed)
pixMin = (int) *tmpSh;
if ((int) *tmpSh > pixMax && (long) *tmpSh <= maxPixValAllowed)
pixMax = (int) *tmpSh;
} /* for ...extract min and max val from the file */
} /* else ...unsigned pixel values */
} /* endif ...have to compute min and max values from the file */
/* if ww and wl not in the file, computes them */
if (windowWidth == 1 && windowLevel == 0)
{
pixMinCalib = (int) (((float) pixMin * pixSlope) - pixOffset);
pixMaxCalib = (int) (((float) pixMax * pixSlope) - pixOffset);
windowWidth = pixMaxCalib - pixMinCalib + 1;
windowLevel = ((windowWidth / 2) + pixMinCalib);
} /* if ...we have to compute ww and wl */
/* test if the image is stored with inverted pixel values */
if (gArrPhotoInterpret [fpOrig] == MONOCHROME1)
{
PapyULong theLLoop;
PapyUShort *theUShortPix = pixel;
PapyUShort fInvertPix = (PapyUShort)pixMax+(PapyUShort)pixMin;
for (theLLoop = 0L; theLLoop < imageSize; theLLoop++)
{
*theUShortPix = fInvertPix - *theUShortPix;
theUShortPix++;
} /* for ...image inversion */
} /* endif ...inversion de l'image */
} /* else ...12 or 16 bits images */
/* free group 7FE0 */
err = Papy3GroupFree (&group, TRUE);
} /* endif ...group 7FE0 read */
} /* endif ...group 7FE0 found */
/* we do allow only to compress 8bits images.
so if 16 bits, please convert first */
if (imageDepth > 8 && inCompression == JPEG_LOSSY)
{
image = Compute8bitsImage ((PapyUShort *)pixel, imageHeight, imageWidth, imageDepth,
pixMin, pixMax);
imageDepth = 8;
}
else
image = (PapyUChar *)pixel;
/* Attention voir pour resize 16 bits et 3 * 8 bits*/
/* resize image */
if (imageHeight != inJpegHeight || imageWidth != inJpegWidth)
image=TI_taille(image, imageWidth, imageHeight, inJpegWidth, inJpegHeight,
imageDepth, numPlans, &bytesInImage);
sprintf(jpegFilename,"%s.%d_%d.jpg",outJpegBaseFilename, dimImageJpeg, noImage);
/*MAL added: when converting Papyrus file (in order to have same name as in cache anubis )
Anubis convert from Dicom and not from Papyrus ! */
/* sprintf(jpegFilename,"%s.%d.%d_1.jpg",outJpegBaseFilename, noImage, dimImageJpeg);*/
if (inCompression == JPEG_LOSSY)
{
err = (int)JPEGLossyEncodeImage (fpOrig, inQuality, (PapyUChar *) jpegFilename, (PapyUChar *) image,
(PapyUChar **) &theCompPixP, (PapyULong *) &bytesInImage,
(int) inJpegHeight, (int) inJpegWidth, 8, TRUE);
if (err < 0) return err;
}
else if (inCompression == JPEG_LOSSLESS)
JPEGLosslessEncodeImage ((PapyUShort *) image, (PapyUChar **) &theCompPixP,
(PapyULong *) &bytesInImage, (int) inJpegWidth, (int) inJpegHeight,
(int) imageDepth);
#ifdef MAYO_WAVE
{
err = (int)WaveletEncodeImage (10, 5, (PapyUChar *) image, (PapyUChar **) &theCompPixP,
(PapyULong *) &bytesInImage,(int) inJpegHeight, (int) inJpegWidth,
(int) imageDepth, (enum EModality) gFileModality[inFileNb] );
if (err != 0) return (-1);
}
#endif /* MAYO_WAVE */
} /* if selected image */
}/* loop on all images of the papyrus or dicom file */
} /* endof Papyrus2Jpeg */
|