Changeset 822 for soft/giet_vm/applications/rosenfeld/nrc2/src/nrio2f.c
- Timestamp:
- Jun 1, 2016, 10:25:43 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/applications/rosenfeld/nrc2/src/nrio2f.c
r772 r822 15 15 #include <stddef.h> 16 16 #include <stdlib.h> 17 17 #include <string.h> 18 #include <ctype.h> 19 20 #include "nrc_os_config.h" 18 21 #include "mypredef.h" 19 22 #include "nrtype.h" … … 34 37 */ 35 38 36 /* ---------------------------------------------------------------------------------------------------------- */ 37 IMAGE_EXPORT(void) write_si8matrix(sint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 38 /* ---------------------------------------------------------------------------------------------------------- */ 39 { 40 int i,j; 41 42 FILE *f; 43 44 f = fopen(filename, "wt"); 45 if(f == NULL) { 46 nrerror("Can't open file in write_si8matrix"); 47 } 48 49 for(i=nrl; i<=nrh; i++) { 50 for(j=ncl; j<=nch; j++) { 51 fprintf(f, format, m[i][j]); 52 } 53 fputc('\n', f); 54 } 55 fclose(f); 56 } 57 /* ---------------------------------------------------------------------------------------------------------- */ 58 IMAGE_EXPORT(void) write_ui8matrix(uint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 59 /* ---------------------------------------------------------------------------------------------------------- */ 60 { 61 int i,j; 62 63 FILE *f; 64 65 f = fopen(filename, "wt"); 66 if(f == NULL) { 67 nrerror("Can't open file in write_ui8matrix"); 68 } 69 70 for(i=nrl; i<=nrh; i++) { 71 for(j=ncl; j<=nch; j++) { 72 fprintf(f, format, m[i][j]); 73 } 74 fputc('\n', f); 75 } 76 fclose(f); 77 } 78 /* ------------------------------------------------------------------------------------------------------------ */ 79 IMAGE_EXPORT(void) write_si16matrix(sint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 80 /* ------------------------------------------------------------------------------------------------------------ */ 81 { 82 int i,j; 83 84 FILE *f; 85 86 f = fopen(filename, "wt"); 87 if(f == NULL) { 88 nrerror("Can't open file in write_si16matrix"); 89 } 90 91 for(i=nrl; i<=nrh; i++) { 92 for(j=ncl; j<=nch; j++) { 93 fprintf(f, format, m[i][j]); 94 } 95 fputc('\n', f); 96 } 97 fclose(f); 98 } 99 /* ------------------------------------------------------------------------------------------------------------ */ 100 IMAGE_EXPORT(void) write_ui16matrix(uint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 101 /* ------------------------------------------------------------------------------------------------------------ */ 102 { 103 int i,j; 104 105 FILE *f; 106 107 f = fopen(filename, "wt"); 108 if(f == NULL) { 109 nrerror("Can't open file in write_ui16matrix"); 110 } 111 112 for(i=nrl; i<=nrh; i++) { 113 for(j=ncl; j<=nch; j++) { 114 fprintf(f, format, m[i][j]); 115 } 116 fputc('\n', f); 117 } 118 fclose(f); 119 } 120 /* ------------------------------------------------------------------------------------------------------------ */ 121 IMAGE_EXPORT(void) write_si32matrix(sint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 122 /* ------------------------------------------------------------------------------------------------------------ */ 123 { 124 int i,j; 125 126 FILE *f; 127 128 f = fopen(filename, "wt"); 129 if(f == NULL) { 130 nrerror("Can't open file in write_si32matrix"); 131 } 132 133 for(i=nrl; i<=nrh; i++) { 134 for(j=ncl; j<=nch; j++) { 135 fprintf(f, format, m[i][j]); 136 } 137 fputc('\n', f); 138 } 139 fclose(f); 140 } 141 /* ------------------------------------------------------------------------------------------------------------ */ 142 IMAGE_EXPORT(void) write_ui32matrix(uint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 143 /* ------------------------------------------------------------------------------------------------------------ */ 144 { 145 int i,j; 146 147 FILE *f; 148 149 f = fopen(filename, "wt"); 150 if(f == NULL) { 151 nrerror("Can't open file in write_ui32matrix"); 152 } 153 154 for(i=nrl; i<=nrh; i++) { 155 for(j=ncl; j<=nch; j++) { 156 fprintf(f, format, m[i][j]); 157 } 158 fputc('\n', f); 159 } 160 fclose(f); 161 } 162 /* ------------------------------------------------------------------------------------------------------------ */ 163 IMAGE_EXPORT(void) write_si64matrix(sint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 164 /* ------------------------------------------------------------------------------------------------------------ */ 165 { 166 int i,j; 167 168 FILE *f; 169 170 f = fopen(filename, "wt"); 171 if(f == NULL) { 172 nrerror("Can't open file in write_si64matrix"); 173 } 174 175 for(i=nrl; i<=nrh; i++) { 176 for(j=ncl; j<=nch; j++) { 177 fprintf(f, format, m[i][j]); 178 } 179 fputc('\n', f); 180 } 181 fclose(f); 182 } 183 /* ------------------------------------------------------------------------------------------------------------ */ 184 IMAGE_EXPORT(void) write_ui64matrix(uint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 185 /* ------------------------------------------------------------------------------------------------------------ */ 186 { 187 int i,j; 188 189 FILE *f; 190 191 f = fopen(filename, "wt"); 192 if(f == NULL) { 193 nrerror("Can't open file in write_ui64matrix"); 194 } 195 196 for(i=nrl; i<=nrh; i++) { 197 for(j=ncl; j<=nch; j++) { 198 fprintf(f, format, m[i][j]); 199 } 200 fputc('\n', f); 201 } 202 fclose(f); 203 } 204 /* ------------------------------------------------------------------------------------------------------------ */ 205 IMAGE_EXPORT(void) write_f32matrix(float32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 206 /* ------------------------------------------------------------------------------------------------------------ */ 207 { 208 int i,j; 209 210 FILE *f; 211 212 f = fopen(filename, "wt"); 213 if(f == NULL) { 214 nrerror("Can't open file in write_f32matrix"); 215 } 216 217 for(i=nrl; i<=nrh; i++) { 218 for(j=ncl; j<=nch; j++) { 219 fprintf(f, format, m[i][j]); 220 } 221 fputc('\n', f); 222 } 223 fclose(f); 224 } 225 /* ------------------------------------------------------------------------------------------------------------ */ 226 IMAGE_EXPORT(void) write_f64matrix(float64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 227 /* ------------------------------------------------------------------------------------------------------------ */ 228 { 229 int i,j; 230 231 FILE *f; 232 233 f = fopen(filename, "wt"); 234 if(f == NULL) { 235 nrerror("Can't open file in write_f64matrix"); 236 } 237 238 for(i=nrl; i<=nrh; i++) { 239 for(j=ncl; j<=nch; j++) { 240 fprintf(f, format, m[i][j]); 241 } 242 fputc('\n', f); 243 } 244 fclose(f); 245 } 246 /* ---------------------------------------------------------------------------------------------------------- */ 247 IMAGE_EXPORT(void) write_rgb8matrix(rgb8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 248 /* ---------------------------------------------------------------------------------------------------------- */ 249 { 250 int i,j; 251 252 FILE *f; 253 254 f = fopen(filename, "wt"); 255 if(f == NULL) { 256 nrerror("Can't open file in write_rgb8matrix"); 257 } 258 259 for(i=nrl; i<=nrh; i++) { 260 for(j=ncl; j<=nch; j++) { 261 fprintf(f, format, m[i][j].r, m[i][j].g, m[i][j].b); 262 } 263 fputc('\n', f); 264 } 265 fclose(f); 266 } 267 /* ------------------------------------------------------------------------------------------------------------ */ 268 IMAGE_EXPORT(void) write_rgbx8matrix(rgbx8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 269 /* ------------------------------------------------------------------------------------------------------------ */ 270 { 271 int i,j; 272 273 FILE *f; 274 275 f = fopen(filename, "wt"); 276 if(f == NULL) { 277 nrerror("Can't open file in write_rgbx8matrix"); 278 } 279 280 for(i=nrl; i<=nrh; i++) { 281 for(j=ncl; j<=nch; j++) { 282 fprintf(f, format, m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x); 283 } 284 fputc('\n', f); 285 } 286 fclose(f); 287 } 39 #undef write_type_matrix 40 #define write_type_matrix(t) \ 41 void short_name(t,write_,matrix)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * filename) \ 42 { \ 43 FILE * f = fopen(filename, "wt"); \ 44 if (f == NULL) { \ 45 nrerror("Can't open file %s in %s", filename, __func__); \ 46 } \ 47 for (int32_t i = nrl; i <= nrh; i++) { \ 48 for (int32_t j = ncl; j <= nch; j++) { \ 49 fprintf(f, format, elem); \ 50 } \ 51 fprintf(f, "\n"); \ 52 } \ 53 fclose(f); \ 54 } 55 56 #undef elem 57 #define elem m[i][j] 58 write_type_matrix(int8_t); 59 write_type_matrix(uint8_t); 60 write_type_matrix(int16_t); 61 write_type_matrix(uint16_t); 62 write_type_matrix(int32_t); 63 write_type_matrix(uint32_t); 64 write_type_matrix(int64_t); 65 write_type_matrix(uint64_t); 66 write_type_matrix(float); 67 write_type_matrix(double); 68 #undef elem 69 #define elem m[i][j].r, m[i][j].g, m[i][j].b 70 write_type_matrix(rgb8); 71 #undef elem 72 #define elem m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x 73 write_type_matrix(rgbx8); 74 288 75 289 76 /* … … 293 80 */ 294 81 295 /* ------------------------------------------------------------------------------------------------------------ */ 296 IMAGE_EXPORT(void) write_si8matrix_T(sint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 297 /* ------------------------------------------------------------------------------------------------------------ */ 298 { 299 long i,j; 300 301 FILE *f; 302 303 f = fopen(filename, "wt"); 304 if(f == NULL) { 305 nrerror("Can't open file in write_si8matrix_T"); 306 } 307 308 for(j=ncl; j<=nch; j++) { 309 for(i=nrl; i<=nrh; i++) { 310 fprintf(f, format, m[i][j]); 311 } 312 fputc('\n', f); 313 } 314 fclose(f); 315 } 316 /* ------------------------------------------------------------------------------------------------------------ */ 317 IMAGE_EXPORT(void) write_ui8matrix_T(uint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 318 /* ------------------------------------------------------------------------------------------------------------ */ 319 { 320 long i,j; 321 322 FILE *f; 323 324 f = fopen(filename, "wt"); 325 if(f == NULL) { 326 nrerror("Can't open file in write_ui8matrix_T"); 327 } 328 329 for(j=ncl; j<=nch; j++) { 330 for(i=nrl; i<=nrh; i++) { 331 fprintf(f, format, m[i][j]); 332 } 333 fputc('\n', f); 334 } 335 fclose(f); 336 } 337 /* -------------------------------------------------------------------------------------------------------------- */ 338 IMAGE_EXPORT(void) write_si32matrix_T(sint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 339 /* -------------------------------------------------------------------------------------------------------------- */ 340 { 341 long i,j; 342 343 FILE *f; 344 345 f = fopen(filename, "wt"); 346 if(f == NULL) { 347 nrerror("Can't open file in write_si32matrix_T"); 348 } 349 350 for(j=ncl; j<=nch; j++) { 351 for(i=nrl; i<=nrh; i++) { 352 fprintf(f, format, m[i][j]); 353 } 354 fputc('\n', f); 355 } 356 fclose(f); 357 } 358 /* -------------------------------------------------------------------------------------------------------------- */ 359 IMAGE_EXPORT(void) write_ui32matrix_T(uint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 360 /* -------------------------------------------------------------------------------------------------------------- */ 361 { 362 long i,j; 363 364 FILE *f; 365 366 f = fopen(filename, "wt"); 367 if(f == NULL) { 368 nrerror("Can't open file in write_ui32matrix_T"); 369 } 370 371 for(j=ncl; j<=nch; j++) { 372 for(i=nrl; i<=nrh; i++) { 373 fprintf(f, format, m[i][j]); 374 } 375 fputc('\n', f); 376 } 377 fclose(f); 378 } 379 /* -------------------------------------------------------------------------------------------------------------- */ 380 IMAGE_EXPORT(void) write_si64matrix_T(sint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 381 /* -------------------------------------------------------------------------------------------------------------- */ 382 { 383 long i,j; 384 385 FILE *f; 386 387 f = fopen(filename, "wt"); 388 if(f == NULL) { 389 nrerror("Can't open file in write_si64matrix_T"); 390 } 391 392 for(j=ncl; j<=nch; j++) { 393 for(i=nrl; i<=nrh; i++) { 394 fprintf(f, format, m[i][j]); 395 } 396 fputc('\n', f); 397 } 398 fclose(f); 399 } 400 /* -------------------------------------------------------------------------------------------------------------- */ 401 IMAGE_EXPORT(void) write_ui64matrix_T(uint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 402 /* -------------------------------------------------------------------------------------------------------------- */ 403 { 404 long i,j; 405 406 FILE *f; 407 408 f = fopen(filename, "wt"); 409 if(f == NULL) { 410 nrerror("Can't open file in write_ui64matrix_T"); 411 } 412 413 for(j=ncl; j<=nch; j++) { 414 for(i=nrl; i<=nrh; i++) { 415 fprintf(f, format, m[i][j]); 416 } 417 fputc('\n', f); 418 } 419 fclose(f); 420 } 421 /* -------------------------------------------------------------------------------------------------------------- */ 422 IMAGE_EXPORT(void) write_f32matrix_T(float32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 423 /* -------------------------------------------------------------------------------------------------------------- */ 424 { 425 long i,j; 426 427 FILE *f; 428 429 f = fopen(filename, "wt"); 430 if(f == NULL) { 431 nrerror("Can't open file in write_f32matrix"); 432 } 433 434 for(j=ncl; j<=nch; j++) { 435 for(i=nrl; i<=nrh; i++) { 436 fprintf(f, format, m[i][j]); 437 } 438 fputc('\n', f); 439 } 440 fclose(f); 441 } 442 /* -------------------------------------------------------------------------------------------------------------- */ 443 IMAGE_EXPORT(void) write_f64matrix_T(float64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 444 /* -------------------------------------------------------------------------------------------------------------- */ 445 { 446 long i,j; 447 448 FILE *f; 449 450 f = fopen(filename, "wt"); 451 if(f == NULL) { 452 nrerror("Can't open file in write_f64matrix"); 453 } 454 455 for(j=ncl; j<=nch; j++) { 456 for(i=nrl; i<=nrh; i++) { 457 fprintf(f, format, m[i][j]); 458 } 459 fputc('\n', f); 460 } 461 fclose(f); 462 } 463 /* ------------------------------------------------------------------------------------------------------------ */ 464 IMAGE_EXPORT(void) write_rgb8matrix_T(rgb8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 465 /* ------------------------------------------------------------------------------------------------------------ */ 466 { 467 long i,j; 468 469 FILE *f; 470 471 f = fopen(filename, "wt"); 472 if(f == NULL) { 473 nrerror("Can't open file in write_rgb8matrix"); 474 } 475 476 for(j=ncl; j<=nch; j++) { 477 for(i=nrl; i<=nrh; i++) { 478 fprintf(f, format, m[i][j].r, m[i][j].g, m[i][j].b); 479 } 480 fputc('\n', f); 481 } 482 fclose(f); 483 } 484 /* -------------------------------------------------------------------------------------------------------------- */ 485 IMAGE_EXPORT(void) write_rgbx8matrix_T(rgbx8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 486 /* -------------------------------------------------------------------------------------------------------------- */ 487 { 488 long i,j; 489 490 FILE *f; 491 492 f = fopen(filename, "wt"); 493 if(f == NULL) { 494 nrerror("Can't open file in write_rgb8matrix"); 495 } 496 497 for(j=ncl; j<=nch; j++) { 498 for(i=nrl; i<=nrh; i++) { 499 fprintf(f, format, m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x); 500 } 501 fputc('\n', f); 502 } 503 fclose(f); 504 } 82 83 #undef write_type_matrix_T 84 #define write_type_matrix_T(t) \ 85 void short_name(t,write_,matrix_T)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * filename) \ 86 { \ 87 FILE * f = fopen(filename, "wt"); \ 88 if (f == NULL) { \ 89 nrerror("Can't open file %s in %s", filename, __func__); \ 90 } \ 91 for (int32_t j = ncl; j <= nch; j++) { \ 92 for (int32_t i = nrl; i <= nrh; i++) { \ 93 fprintf(f, format, elem); \ 94 } \ 95 fprintf(f, "\n"); \ 96 } \ 97 fclose(f); \ 98 } 99 100 #undef elem 101 #define elem m[i][j] 102 write_type_matrix_T(int8_t); 103 write_type_matrix_T(uint8_t); 104 write_type_matrix_T(int16_t); 105 write_type_matrix_T(uint16_t); 106 write_type_matrix_T(int32_t); 107 write_type_matrix_T(uint32_t); 108 write_type_matrix_T(int64_t); 109 write_type_matrix_T(uint64_t); 110 write_type_matrix_T(float); 111 write_type_matrix_T(double); 112 #undef elem 113 #define elem m[i][j].r, m[i][j].g, m[i][j].b 114 write_type_matrix_T(rgb8); 115 #undef elem 116 #define elem m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x 117 write_type_matrix_T(rgbx8); 118 119 505 120 506 121 /* … … 510 125 */ 511 126 512 /* ----------------------------------------------------------------------------------------------------------------- */ 513 IMAGE_EXPORT(void) write_si8matrix_number(sint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 514 /* ----------------------------------------------------------------------------------------------------------------- */ 515 { 516 int i,j; 517 518 FILE *f; 519 520 f = fopen(filename, "wt"); 521 if(f == NULL) { 522 nrerror("Can't open file in write_si8matrix_number"); 523 } 524 525 // entete 526 fprintf(f, "%5c", '#'); 527 for(j=ncl; j<=nch; j++) { 528 fprintf(f, format, j); 529 } 530 fputc('\n', f); 531 532 for(i=nrl; i<=nrh; i++) { 533 fprintf(f, "[%3d]", i); 534 for(j=ncl; j<=nch; j++) { 535 fprintf(f, format, m[i][j]); 536 } 537 fputc('\n', f); 538 } 539 fclose(f); 540 } 541 /* ----------------------------------------------------------------------------------------------------------------- */ 542 IMAGE_EXPORT(void) write_ui8matrix_number(uint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 543 /* ----------------------------------------------------------------------------------------------------------------- */ 544 { 545 int i,j; 546 547 FILE *f; 548 549 f = fopen(filename, "wt"); 550 if(f == NULL) { 551 nrerror("Can't open file in write_ui8matrix_number"); 552 } 553 554 // entete 555 fprintf(f, "%5c", '#'); 556 for(j=ncl; j<=nch; j++) { 557 fprintf(f, format, j); 558 } 559 fputc('\n', f); 560 561 for(i=nrl; i<=nrh; i++) { 562 fprintf(f, "[%3d]", i); 563 for(j=ncl; j<=nch; j++) { 564 fprintf(f, format, m[i][j]); 565 } 566 fputc('\n', f); 567 } 568 fclose(f); 569 } 570 /* ------------------------------------------------------------------------------------------------------------------- */ 571 IMAGE_EXPORT(void) write_si16matrix_number(sint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 572 /* ------------------------------------------------------------------------------------------------------------------- */ 573 { 574 int i,j; 575 576 FILE *f; 577 578 f = fopen(filename, "wt"); 579 if(f == NULL) { 580 nrerror("Can't open file in write_si16matrix"); 581 } 582 583 // entete 584 fprintf(f, "%5c", '#'); 585 for(j=ncl; j<=nch; j++) { 586 fprintf(f, format, j); 587 } 588 fputc('\n', f); 589 590 for(i=nrl; i<=nrh; i++) { 591 fprintf(f, "[%3d]", i); 592 for(j=ncl; j<=nch; j++) { 593 fprintf(f, format, m[i][j]); 594 } 595 fputc('\n', f); 596 } 597 fclose(f); 598 } 599 /* ------------------------------------------------------------------------------------------------------------------- */ 600 IMAGE_EXPORT(void) write_ui16matrix_number(uint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 601 /* ------------------------------------------------------------------------------------------------------------------- */ 602 { 603 int i,j; 604 605 FILE *f; 606 607 f = fopen(filename, "wt"); 608 if(f == NULL) { 609 nrerror("Can't open file in write_ui16matrix"); 610 } 611 612 // entete 613 fprintf(f, "%5c", '#'); 614 for(j=ncl; j<=nch; j++) { 615 fprintf(f, format, j); 616 } 617 fputc('\n', f); 618 619 for(i=nrl; i<=nrh; i++) { 620 fprintf(f, "[%3d]", i); 621 for(j=ncl; j<=nch; j++) { 622 fprintf(f, format, m[i][j]); 623 } 624 fputc('\n', f); 625 } 626 fclose(f); 627 } 628 /* ------------------------------------------------------------------------------------------------------------------- */ 629 IMAGE_EXPORT(void) write_si32matrix_number(sint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 630 /* ------------------------------------------------------------------------------------------------------------------- */ 631 { 632 int i,j; 633 634 FILE *f; 635 636 f = fopen(filename, "wt"); 637 if(f == NULL) { 638 nrerror("Can't open file in write_si32matrix"); 639 } 640 641 // entete 642 fprintf(f, "%5c", '#'); 643 for(j=ncl; j<=nch; j++) { 644 fprintf(f, format, j); 645 } 646 fputc('\n', f); 647 648 for(i=nrl; i<=nrh; i++) { 649 fprintf(f, "[%3d]", i); 650 for(j=ncl; j<=nch; j++) { 651 fprintf(f, format, m[i][j]); 652 } 653 fputc('\n', f); 654 } 655 fclose(f); 656 } 657 /* ------------------------------------------------------------------------------------------------------------------- */ 658 IMAGE_EXPORT(void) write_ui32matrix_number(uint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 659 /* ------------------------------------------------------------------------------------------------------------------- */ 660 { 661 int i,j; 662 663 FILE *f; 664 665 f = fopen(filename, "wt"); 666 if(f == NULL) { 667 nrerror("Can't open file in write_ui32matrix"); 668 } 669 670 // entete 671 fprintf(f, "%5c", '#'); 672 for(j=ncl; j<=nch; j++) { 673 fprintf(f, format, j); 674 } 675 fputc('\n', f); 676 677 for(i=nrl; i<=nrh; i++) { 678 fprintf(f, "[%3d]", i); 679 for(j=ncl; j<=nch; j++) { 680 fprintf(f, format, m[i][j]); 681 } 682 fputc('\n', f); 683 } 684 fclose(f); 685 } 686 /* ------------------------------------------------------------------------------------------------------------------- */ 687 IMAGE_EXPORT(void) write_si64matrix_number(sint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 688 /* ------------------------------------------------------------------------------------------------------------------- */ 689 { 690 int i,j; 691 692 FILE *f; 693 694 f = fopen(filename, "wt"); 695 if(f == NULL) { 696 nrerror("Can't open file in write_si64matrix"); 697 } 698 699 // entete 700 fprintf(f, "%5c", '#'); 701 for(j=ncl; j<=nch; j++) { 702 fprintf(f, format, j); 703 } 704 fputc('\n', f); 705 706 for(i=nrl; i<=nrh; i++) { 707 fprintf(f, "[%3d]", i); 708 for(j=ncl; j<=nch; j++) { 709 fprintf(f, format, m[i][j]); 710 } 711 fputc('\n', f); 712 } 713 fclose(f); 714 } 715 /* ------------------------------------------------------------------------------------------------------------------- */ 716 IMAGE_EXPORT(void) write_ui64matrix_number(uint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 717 /* ------------------------------------------------------------------------------------------------------------------- */ 718 { 719 int i,j; 720 721 FILE *f; 722 723 f = fopen(filename, "wt"); 724 if(f == NULL) { 725 nrerror("Can't open file in write_ui64matrix"); 726 } 727 728 // entete 729 fprintf(f, "%5c", '#'); 730 for(j=ncl; j<=nch; j++) { 731 fprintf(f, format, j); 732 } 733 fputc('\n', f); 734 735 for(i=nrl; i<=nrh; i++) { 736 fprintf(f, "[%3d]", i); 737 for(j=ncl; j<=nch; j++) { 738 fprintf(f, format, m[i][j]); 739 } 740 fputc('\n', f); 741 } 742 fclose(f); 743 } 744 /* ------------------------------------------------------------------------------------------------------------------- */ 745 IMAGE_EXPORT(void) write_f32matrix_number(float32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 746 /* ------------------------------------------------------------------------------------------------------------------- */ 747 { 748 int i,j; 749 750 FILE *f; 751 752 f = fopen(filename, "wt"); 753 if(f == NULL) { 754 nrerror("Can't open file in write_f32matrix_number"); 755 } 756 757 // entete 758 fprintf(f, "%4d", 0); 759 for(j=ncl; j<=nch; j++) { 760 fprintf(f, format, (float32)j); 761 } 762 fputc('\n', f); 763 764 for(i=nrl; i<=nrh; i++) { 765 fprintf(f, "%4d", i); 766 for(j=ncl; j<=nch; j++) { 767 fprintf(f, format, m[i][j]); 768 } 769 fputc('\n', f); 770 } 771 fclose(f); 772 } 773 /* ------------------------------------------------------------------------------------------------------------------- */ 774 IMAGE_EXPORT(void) write_f64matrix_number(float64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 775 /* ------------------------------------------------------------------------------------------------------------------- */ 776 { 777 int i,j; 778 779 FILE *f; 780 781 f = fopen(filename, "wt"); 782 if(f == NULL) { 783 nrerror("Can't open file in write_f64matrix_number"); 784 } 785 786 // entete 787 fprintf(f, "%4d", 0); 788 for(j=ncl; j<=nch; j++) { 789 fprintf(f, format, (float32)j); 790 } 791 fputc('\n', f); 792 793 for(i=nrl; i<=nrh; i++) { 794 fprintf(f, "%4d", i); 795 for(j=ncl; j<=nch; j++) { 796 fprintf(f, format, m[i][j]); 797 } 798 fputc('\n', f); 799 } 800 fclose(f); 801 } 802 /* ----------------------------------------------------------------------------------------------------------------- */ 803 IMAGE_EXPORT(void) write_rgb8matrix_number(rgb8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 804 /* ----------------------------------------------------------------------------------------------------------------- */ 805 { 806 int i,j; 807 808 FILE *f; 809 810 f = fopen(filename, "wt"); 811 if(f == NULL) { 812 nrerror("Can't open file in write_rgb8matrix_number"); 813 } 814 815 // entete 816 fprintf(f, "%4d", 0); 817 for(j=ncl; j<=nch; j++) { 818 fprintf(f, format, (float32)j); 819 } 820 fputc('\n', f); 821 822 for(i=nrl; i<=nrh; i++) { 823 fprintf(f, "%4d", i); 824 for(j=ncl; j<=nch; j++) { 825 fprintf(f, format, m[i][j].r, m[i][j].g, m[i][j].b); 826 } 827 fputc('\n', f); 828 } 829 fclose(f); 830 } 831 /* ------------------------------------------------------------------------------------------------------------------- */ 832 IMAGE_EXPORT(void) write_rgb8xmatrix_number(rgbx8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 833 /* ------------------------------------------------------------------------------------------------------------------- */ 834 { 835 int i,j; 836 837 FILE *f; 838 839 f = fopen(filename, "wt"); 840 if(f == NULL) { 841 nrerror("Can't open file in write_rgbx8matrix_number"); 842 } 843 844 // entete 845 fprintf(f, "%4d", 0); 846 for(j=ncl; j<=nch; j++) { 847 fprintf(f, format, (float32)j); 848 } 849 fputc('\n', f); 850 851 for(i=nrl; i<=nrh; i++) { 852 fprintf(f, "%4d", i); 853 for(j=ncl; j<=nch; j++) { 854 fprintf(f, format, m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x); 855 } 856 fputc('\n', f); 857 } 858 fclose(f); 859 } 127 128 #undef write_type_matrix_number 129 #define write_type_matrix_number(t) \ 130 void short_name(t,write_,matrix_number)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * filename) \ 131 { \ 132 FILE * f = fopen(filename, "wt"); \ 133 if (f == NULL) { \ 134 nrerror("Can't open file %s in %s", filename, __func__); \ 135 } \ 136 fprintf(f, "%5c", '#'); \ 137 for (int32_t j = ncl; j <= nch; j++) { \ 138 fprintf(f, format, j); \ 139 } \ 140 fprintf(f, "\n"); \ 141 for (int32_t i = nrl; i <= nrh; i++) { \ 142 fprintf(f, "[%3d]", i); \ 143 for (int32_t j = ncl; j <= nch; j++) { \ 144 fprintf(f, format, m[i][j]); \ 145 } \ 146 fprintf(f, "\n"); \ 147 } \ 148 fclose(f); \ 149 } 150 151 152 #undef elem 153 #define elem m[i][j] 154 write_type_matrix_number(int8_t); 155 write_type_matrix_number(uint8_t); 156 write_type_matrix_number(int16_t); 157 write_type_matrix_number(uint16_t); 158 write_type_matrix_number(int32_t); 159 write_type_matrix_number(uint32_t); 160 write_type_matrix_number(int64_t); 161 write_type_matrix_number(uint64_t); 162 write_type_matrix_number(float); 163 write_type_matrix_number(double); 164 #undef elem 165 #define elem m[i][j].r, m[i][j].g, m[i][j].b 166 write_type_matrix_number(rgb8); 167 #undef elem 168 #define elem m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x 169 write_type_matrix_number(rgbx8); 170 171 860 172 /* 861 173 * ----------------------------- … … 863 175 * ----------------------------- 864 176 */ 865 /* ------------------------------------------------------------------------------------------------------------------- */ 866 IMAGE_EXPORT(void) write_si8matrix_T_number(sint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 867 /* ------------------------------------------------------------------------------------------------------------------- */ 868 { 869 int i,j; 870 871 FILE *f; 872 873 f = fopen(filename, "wt"); 874 if(f == NULL) { 875 nrerror("Can't open file in write_si8matrix_T_number"); 876 } 877 878 fprintf(f, "%5c", '#'); 879 for(i=nrl; i<=nrh; i++) { 880 fprintf(f, format, i); 881 } 882 fputc('\n', f); 883 884 for(j=ncl; j<=nch; j++) { 885 fprintf(f, "[%3d]", j); 886 for(i=nrl; i<=nrh; i++) { 887 fprintf(f, format, m[i][j]); 888 } 889 fputc('\n', f); 890 } 891 fclose(f); 892 } 893 /* ------------------------------------------------------------------------------------------------------------------- */ 894 IMAGE_EXPORT(void) write_ui8matrix_T_number(uint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 895 /* ------------------------------------------------------------------------------------------------------------------- */ 896 { 897 int i,j; 898 899 FILE *f; 900 901 f = fopen(filename, "wt"); 902 if(f == NULL) { 903 nrerror("Can't open file in write_ui8matrix_T_number"); 904 } 905 906 fprintf(f, "%5c", '#'); 907 for(i=nrl; i<=nrh; i++) { 908 fprintf(f, format, i); 909 } 910 fputc('\n', f); 911 912 913 for(j=ncl; j<=nch; j++) { 914 fprintf(f, "[%3d]", j); 915 for(i=nrl; i<=nrh; i++) { 916 fprintf(f, format, m[i][j]); 917 } 918 fputc('\n', f); 919 } 920 fclose(f); 921 } 922 /* --------------------------------------------------------------------------------------------------------------------- */ 923 IMAGE_EXPORT(void) write_si16matrix_T_number(sint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 924 /* --------------------------------------------------------------------------------------------------------------------- */ 925 { 926 int i,j; 927 928 FILE *f; 929 930 f = fopen(filename, "wt"); 931 if(f == NULL) { 932 nrerror("Can't open file in write_si16matrix_T_number"); 933 } 934 935 fprintf(f, "%5c", '#'); 936 for(i=nrl; i<=nrh; i++) { 937 fprintf(f, format, (float)i); 938 } 939 fputc('\n', f); 940 941 for(j=ncl; j<=nch; j++) { 942 fprintf(f, "[%3d]", j); 943 for(i=nrl; i<=nrh; i++) { 944 fprintf(f, format, m[i][j]); 945 } 946 fputc('\n', f); 947 } 948 fclose(f); 949 } 950 /* --------------------------------------------------------------------------------------------------------------------- */ 951 IMAGE_EXPORT(void) write_ui16matrix_T_number(uint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 952 /* --------------------------------------------------------------------------------------------------------------------- */ 953 { 954 int i,j; 955 956 FILE *f; 957 958 f = fopen(filename, "wt"); 959 if(f == NULL) { 960 nrerror("Can't open file in write_ui16matrix_T_number"); 961 } 962 963 fprintf(f, "%5c", '#'); 964 for(i=nrl; i<=nrh; i++) { 965 fprintf(f, format, (float)i); 966 } 967 fputc('\n', f); 968 969 for(j=ncl; j<=nch; j++) { 970 fprintf(f, "[%3d]", j); 971 for(i=nrl; i<=nrh; i++) { 972 fprintf(f, format, m[i][j]); 973 } 974 fputc('\n', f); 975 } 976 fclose(f); 977 } 978 /* --------------------------------------------------------------------------------------------------------------------- */ 979 IMAGE_EXPORT(void) write_si32matrix_T_number(sint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 980 /* --------------------------------------------------------------------------------------------------------------------- */ 981 { 982 int i,j; 983 984 FILE *f; 985 986 f = fopen(filename, "wt"); 987 if(f == NULL) { 988 nrerror("Can't open file in write_si32matrix_T_number"); 989 } 990 991 fprintf(f, "%5c", '#'); 992 for(i=nrl; i<=nrh; i++) { 993 fprintf(f, format, (float)i); 994 } 995 fputc('\n', f); 996 997 for(j=ncl; j<=nch; j++) { 998 fprintf(f, "[%3d]", j); 999 for(i=nrl; i<=nrh; i++) { 1000 fprintf(f, format, m[i][j]); 1001 } 1002 fputc('\n', f); 1003 } 1004 fclose(f); 1005 } 1006 1007 /* --------------------------------------------------------------------------------------------------------------------- */ 1008 IMAGE_EXPORT(void) write_ui32matrix_T_number(uint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 1009 /* --------------------------------------------------------------------------------------------------------------------- */ 1010 { 1011 int i,j; 1012 1013 FILE *f; 1014 1015 f = fopen(filename, "wt"); 1016 if(f == NULL) { 1017 nrerror("Can't open file in write_ui32matrix_T_number"); 1018 } 1019 1020 fprintf(f, "%5c", '#'); 1021 for(i=nrl; i<=nrh; i++) { 1022 fprintf(f, format, (float)i); 1023 } 1024 fputc('\n', f); 1025 1026 1027 for(j=ncl; j<=nch; j++) { 1028 fprintf(f, "[%3d]", j); 1029 for(i=nrl; i<=nrh; i++) { 1030 fprintf(f, format, m[i][j]); 1031 } 1032 fputc('\n', f); 1033 } 1034 fclose(f); 1035 } 1036 /* --------------------------------------------------------------------------------------------------------------------- */ 1037 IMAGE_EXPORT(void) write_si64matrix_T_number(sint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 1038 /* --------------------------------------------------------------------------------------------------------------------- */ 1039 { 1040 int i,j; 1041 1042 FILE *f; 1043 1044 f = fopen(filename, "wt"); 1045 if(f == NULL) { 1046 nrerror("Can't open file in write_si64matrix_T_number"); 1047 } 1048 1049 fprintf(f, "%5c", '#'); 1050 for(i=nrl; i<=nrh; i++) { 1051 fprintf(f, format, (float)i); 1052 } 1053 fputc('\n', f); 1054 1055 for(j=ncl; j<=nch; j++) { 1056 fprintf(f, "[%3d]", j); 1057 for(i=nrl; i<=nrh; i++) { 1058 fprintf(f, format, m[i][j]); 1059 } 1060 fputc('\n', f); 1061 } 1062 fclose(f); 1063 } 1064 /* --------------------------------------------------------------------------------------------------------------------- */ 1065 IMAGE_EXPORT(void) write_ui64matrix_T_number(uint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 1066 /* --------------------------------------------------------------------------------------------------------------------- */ 1067 { 1068 int i,j; 1069 1070 FILE *f; 1071 1072 f = fopen(filename, "wt"); 1073 if(f == NULL) { 1074 nrerror("Can't open file in write_ui64matrix_T_number"); 1075 } 1076 1077 fprintf(f, "%5c", '#'); 1078 for(i=nrl; i<=nrh; i++) { 1079 fprintf(f, format, (float)i); 1080 } 1081 fputc('\n', f); 1082 1083 for(j=ncl; j<=nch; j++) { 1084 fprintf(f, "[%3d]", j); 1085 for(i=nrl; i<=nrh; i++) { 1086 fprintf(f, format, m[i][j]); 1087 } 1088 fputc('\n', f); 1089 } 1090 fclose(f); 1091 } 1092 1093 /* --------------------------------------------------------------------------------------------------------------------- */ 1094 IMAGE_EXPORT(void) write_f32matrix_T_number(float32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 1095 /* --------------------------------------------------------------------------------------------------------------------- */ 1096 { 1097 int i,j; 1098 1099 FILE *f; 1100 1101 f = fopen(filename, "wt"); 1102 if(f == NULL) { 1103 nrerror("Can't open file in write_f32matrix_T_number"); 1104 } 1105 1106 fprintf(f, "%5c", '#'); 1107 for(i=nrl; i<=nrh; i++) { 1108 fprintf(f, format, (float)i); 1109 } 1110 fputc('\n', f); 1111 1112 for(j=ncl; j<=nch; j++) { 1113 fprintf(f, "[%3d]", j); 1114 for(i=nrl; i<=nrh; i++) { 1115 fprintf(f, format, m[i][j]); 1116 } 1117 fputc('\n', f); 1118 } 1119 fclose(f); 1120 } 1121 /* --------------------------------------------------------------------------------------------------------------------- */ 1122 IMAGE_EXPORT(void) write_f64matrix_T_number(float64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 1123 /* --------------------------------------------------------------------------------------------------------------------- */ 1124 { 1125 int i,j; 1126 1127 FILE *f; 1128 1129 f = fopen(filename, "wt"); 1130 if(f == NULL) { 1131 nrerror("Can't open file in write_f64matrix_T_number"); 1132 } 1133 1134 fprintf(f, "%5c", '#'); 1135 for(i=nrl; i<=nrh; i++) { 1136 fprintf(f, format, (float)i); 1137 } 1138 fputc('\n', f); 1139 1140 for(j=ncl; j<=nch; j++) { 1141 fprintf(f, "[%3d]", j); 1142 for(i=nrl; i<=nrh; i++) { 1143 fprintf(f, format, m[i][j]); 1144 } 1145 fputc('\n', f); 1146 } 1147 fclose(f); 1148 } 1149 /* ------------------------------------------------------------------------------------------------------------------- */ 1150 IMAGE_EXPORT(void) write_rgb8matrix_T_number(rgb8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 1151 /* ------------------------------------------------------------------------------------------------------------------- */ 1152 { 1153 int i,j; 1154 1155 FILE *f; 1156 1157 f = fopen(filename, "wt"); 1158 if(f == NULL) { 1159 nrerror("Can't open file in write_rgb8matrix_T_number"); 1160 } 1161 1162 fprintf(f, "%5c", '#'); 1163 for(i=nrl; i<=nrh; i++) { 1164 fprintf(f, format, (float)i); 1165 } 1166 fputc('\n', f); 1167 1168 for(j=ncl; j<=nch; j++) { 1169 fprintf(f, "[%3d]", j); 1170 for(i=nrl; i<=nrh; i++) { 1171 fprintf(f, format, m[i][j]); 1172 } 1173 fputc('\n', f); 1174 } 1175 fclose(f); 1176 } 1177 /* --------------------------------------------------------------------------------------------------------------------- */ 1178 IMAGE_EXPORT(void) write_rgbx8matrix_T_number(rgbx8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *filename) 1179 /* --------------------------------------------------------------------------------------------------------------------- */ 1180 { 1181 int i,j; 1182 1183 FILE *f; 1184 1185 f = fopen(filename, "wt"); 1186 if(f == NULL) { 1187 nrerror("Can't open file in write_rgbx8matrix_T_number"); 1188 } 1189 1190 fprintf(f, "%5c", '#'); 1191 for(i=nrl; i<=nrh; i++) { 1192 fprintf(f, format, (float)i); 1193 } 1194 fputc('\n', f); 1195 1196 for(j=ncl; j<=nch; j++) { 1197 fprintf(f, "[%3d]", j); 1198 for(i=nrl; i<=nrh; i++) { 1199 fprintf(f, format, m[i][j]); 1200 } 1201 fputc('\n', f); 1202 } 1203 fclose(f); 1204 } 177 178 179 #undef write_type_matrix_T_number 180 #define write_type_matrix_T_number(t) \ 181 void short_name(t,write_,matrix_T_number)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * filename) \ 182 { \ 183 FILE * f = fopen(filename, "wt"); \ 184 if (f == NULL) { \ 185 nrerror("Can't open file %s in %s", filename, __func__); \ 186 } \ 187 fprintf(f, "%5c", '#'); \ 188 for (int32_t i = nrl; i <= nrh; i++) { \ 189 fprintf(f, format, i); \ 190 } \ 191 fprintf(f, "\n"); \ 192 for (int32_t j = ncl; j <= nch; j++) { \ 193 fprintf(f, "[%3d]", j); \ 194 for (int32_t i = nrl; i <= nrh; i++) { \ 195 fprintf(f, format, m[i][j]); \ 196 } \ 197 fprintf(f, "\n"); \ 198 } \ 199 fclose(f); \ 200 } 201 202 203 #undef elem 204 #define elem m[i][j] 205 write_type_matrix_T_number(int8_t); 206 write_type_matrix_T_number(uint8_t); 207 write_type_matrix_T_number(int16_t); 208 write_type_matrix_T_number(uint16_t); 209 write_type_matrix_T_number(int32_t); 210 write_type_matrix_T_number(uint32_t); 211 write_type_matrix_T_number(int64_t); 212 write_type_matrix_T_number(uint64_t); 213 write_type_matrix_T_number(float); 214 write_type_matrix_T_number(double); 215 #undef elem 216 #define elem m[i][j].r, m[i][j].g, m[i][j].b 217 write_type_matrix_T_number(rgb8); 218 #undef elem 219 #define elem m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x 220 write_type_matrix_T_number(rgbx8); 221 222 1205 223 /* 1206 224 * --------------------- … … 1209 227 */ 1210 228 1211 /* ----------------------------------------------------------------------------------------------- */ 1212 IMAGE_EXPORT(void) fwrite_si8matrix(sint8 **m,long nrl, long nrh, long ncl, long nch, char *filename) 1213 /* ----------------------------------------------------------------------------------------------- */ 1214 { 1215 long i, ncol = nch-ncl+1; 1216 FILE *f; 1217 1218 f = fopen(filename, "wb"); 1219 if(f == NULL) nrerror("Can't open file in fwrite_si8matrix"); 1220 1221 for(i=nrl; i<=nrh; i++) { 1222 fwrite(m[i]+nrl, sizeof(sint8), ncol, f); 1223 } 1224 fclose(f); 1225 } 1226 /* ------------------------------------------------------------------------------------------------ */ 1227 IMAGE_EXPORT(void) fwrite_ui8matrix(uint8 **m, long nrl, long nrh, long ncl, long nch, char *filename) 1228 /* ------------------------------------------------------------------------------------------------ */ 1229 { 1230 long i, ncol = nch-ncl+1; 1231 FILE *f; 1232 1233 f = fopen(filename, "wb"); 1234 if(f == NULL) nrerror("Can't open file in fwrite_ui8matrix"); 1235 1236 for(i=nrl; i<=nrh; i++) { 1237 fwrite(m[i]+nrl, sizeof(int8), ncol, f); 1238 } 1239 fclose(f); 1240 } 1241 /* ------------------------------------------------------------------------------------------------- */ 1242 IMAGE_EXPORT(void) fwrite_si16matrix(sint16 **m,long nrl, long nrh, long ncl, long nch, char *filename) 1243 /* ------------------------------------------------------------------------------------------------- */ 1244 { 1245 long i, ncol = nch-ncl+1; 1246 FILE *f; 1247 1248 f = fopen(filename, "wb"); 1249 if(f == NULL) 1250 nrerror("Can't open file in fwrite_i16matrix"); 1251 1252 for(i=nrl; i<=nrh; i++) { 1253 fwrite(m[i]+nrl, sizeof(sint16), ncol, f); 1254 } 1255 fclose(f); 1256 } 1257 /* ------------------------------------------------------------------------------------------------- */ 1258 IMAGE_EXPORT(void) fwrite_ui16matrix(uint16 **m,long nrl, long nrh, long ncl, long nch, char *filename) 1259 /* ------------------------------------------------------------------------------------------------- */ 1260 { 1261 long i, ncol = nch-ncl+1; 1262 FILE *f; 1263 1264 f = fopen(filename, "wb"); 1265 if(f == NULL) 1266 nrerror("Can't open file in fwrite_ui16matrix"); 1267 1268 for(i=nrl; i<=nrh; i++) { 1269 fwrite(m[i]+nrl, sizeof(uint16), ncol, f); 1270 } 1271 fclose(f); 1272 } 1273 /* ------------------------------------------------------------------------------------------------- */ 1274 IMAGE_EXPORT(void) fwrite_si32matrix(sint32 **m,long nrl, long nrh, long ncl, long nch, char *filename) 1275 /* ------------------------------------------------------------------------------------------------- */ 1276 { 1277 long i, ncol = nch-ncl+1; 1278 FILE *f; 1279 1280 f = fopen(filename, "wb"); 1281 if(f == NULL) 1282 nrerror("Can't open file in fwrite_si32matrix"); 1283 1284 for(i=nrl; i<=nrh; i++) { 1285 fwrite(m[i]+nrl, sizeof(sint32), ncol, f); 1286 } 1287 fclose(f); 1288 } 1289 /* ------------------------------------------------------------------------------------------------- */ 1290 IMAGE_EXPORT(void) fwrite_ui32matrix(uint32 **m,long nrl, long nrh, long ncl, long nch, char *filename) 1291 /* ------------------------------------------------------------------------------------------------- */ 1292 { 1293 long i, ncol = nch-ncl+1; 1294 FILE *f; 1295 1296 f = fopen(filename, "wb"); 1297 if(f == NULL) 1298 nrerror("Can't open file in fwrite_ui32matrix"); 1299 1300 for(i=nrl; i<=nrh; i++) { 1301 fwrite(m[i]+nrl, sizeof(uint32), ncol, f); 1302 } 1303 fclose(f); 1304 } 1305 /* ----------------------------------------------------------------------------------------------- */ 1306 IMAGE_EXPORT(void) fwrite_si64matrix(sint64 **m,long nrl,long nrh,long ncl, long nch, char *filename) 1307 /* ----------------------------------------------------------------------------------------------- */ 1308 { 1309 long i, ncol = nch-ncl+1; 1310 FILE *f; 1311 1312 f = fopen(filename, "wb"); 1313 if(f == NULL) 1314 nrerror("Can't open file in fwrite_si64matrix"); 1315 1316 for(i=nrl; i<=nrh; i++) { 1317 fwrite(m[i]+nrl, sizeof(sint64), ncol, f); 1318 } 1319 fclose(f); 1320 } 1321 /* ----------------------------------------------------------------------------------------------- */ 1322 IMAGE_EXPORT(void) fwrite_ui64matrix(uint64 **m,long nrl,long nrh,long ncl, long nch, char *filename) 1323 /* ----------------------------------------------------------------------------------------------- */ 1324 { 1325 long i, ncol = nch-ncl+1; 1326 FILE *f; 1327 1328 f = fopen(filename, "wb"); 1329 if(f == NULL) 1330 nrerror("Can't open file in fwrite_ui64matrix"); 1331 1332 for(i=nrl; i<=nrh; i++) { 1333 fwrite(m[i]+nrl, sizeof(uint64), ncol, f); 1334 } 1335 fclose(f); 1336 } 1337 /* ----------------------------------------------------------------------------------------------- */ 1338 IMAGE_EXPORT(void) fwrite_f32matrix(float32 **m,long nrl,long nrh,long ncl, long nch, char *filename) 1339 /* ----------------------------------------------------------------------------------------------- */ 1340 { 1341 long i, ncol = nch-ncl+1; 1342 FILE *f; 1343 1344 f = fopen(filename, "wb"); 1345 if(f == NULL) nrerror("Can't open file in fwrite_f32matrix"); 1346 1347 for(i=nrl; i<=nrh; i++) { 1348 fwrite(m[i]+nrl, sizeof(float32), ncol, f); 1349 } 1350 fclose(f); 1351 } 1352 /* ----------------------------------------------------------------------------------------------- */ 1353 IMAGE_EXPORT(void) fwrite_f64matrix(float64 **m,long nrl,long nrh,long ncl, long nch, char *filename) 1354 /* ----------------------------------------------------------------------------------------------- */ 1355 { 1356 long i, ncol = nch-ncl+1; 1357 FILE *f; 1358 1359 f = fopen(filename, "wb"); 1360 if(f == NULL) nrerror("Can't open file in f64write_matrix"); 1361 1362 for(i=nrl; i<=nrh; i++) { 1363 fwrite(m[i]+nrl, sizeof(float64), ncol, f); 1364 } 1365 fclose(f); 1366 } 1367 /* --------------------------------------------------------------------------------------------- */ 1368 IMAGE_EXPORT(void) fwrite_rgb8matrix(rgb8 **m,long nrl,long nrh,long ncl, long nch, char *filename) 1369 /* --------------------------------------------------------------------------------------------- */ 1370 { 1371 long i, ncol = nch-ncl+1; 1372 FILE *f; 1373 1374 f = fopen(filename, "wb"); 1375 if(f == NULL) nrerror("Can't open file in fwrite_rgb8matrix"); 1376 1377 for(i=nrl; i<=nrh; i++) { 1378 fwrite(m[i]+nrl, sizeof(rgb8), ncol, f); 1379 } 1380 fclose(f); 1381 } 1382 /* ----------------------------------------------------------------------------------------------- */ 1383 IMAGE_EXPORT(void) fwrite_rgbx8matrix(rgbx8 **m,long nrl,long nrh,long ncl, long nch, char *filename) 1384 /* ----------------------------------------------------------------------------------------------- */ 1385 { 1386 long i, ncol = nch-ncl+1; 1387 FILE *f; 1388 1389 f = fopen(filename, "wb"); 1390 if(f == NULL) nrerror("Can't open file in fwrite_rgbx8matrix"); 1391 1392 for(i=nrl; i<=nrh; i++) { 1393 fwrite(m[i]+nrl, sizeof(rgbx8), ncol, f); 1394 } 1395 fclose(f); 1396 } 229 #undef fwrite_type_matrix 230 #define fwrite_type_matrix(t) \ 231 void short_name(t,fwrite_,matrix)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * filename) \ 232 { \ 233 int32_t ncol = nch - ncl + 1; \ 234 FILE * f = fopen(filename, "wb"); \ 235 if (f == NULL) { \ 236 nrerror("Can't open file %s in %s", filename, __func__); \ 237 } \ 238 for (int32_t i = nrl; i <= nrh; i++) { \ 239 fwrite(m[i] + nrl, sizeof(t), ncol, f); \ 240 } \ 241 fclose(f); \ 242 } 243 244 fwrite_type_matrix(int8_t); 245 fwrite_type_matrix(uint8_t); 246 fwrite_type_matrix(int16_t); 247 fwrite_type_matrix(uint16_t); 248 fwrite_type_matrix(int32_t); 249 fwrite_type_matrix(uint32_t); 250 fwrite_type_matrix(int64_t); 251 fwrite_type_matrix(uint64_t); 252 fwrite_type_matrix(float); 253 fwrite_type_matrix(double); 254 fwrite_type_matrix(rgb8); 255 fwrite_type_matrix(rgbx8); 256 257 1397 258 1398 259 /* … … 1402 263 */ 1403 264 1404 /* -------------------------------------------------------------------------------------------- */ 1405 IMAGE_EXPORT(void) fread_si8matrix(char *filename, sint8 **m,long nrl,long nrh,long ncl, long nch) 1406 /* -------------------------------------------------------------------------------------------- */ 1407 { 1408 long i, ncol = nch-ncl+1, nread; 1409 FILE *f; 1410 1411 f = fopen(filename, "rb"); 1412 if(f == NULL) nrerror("Can't open file in fread_i8matrix"); 1413 1414 for(i=nrl; i<=nrh; i++) { 1415 nread = fread(m[i]+ncl, sizeof(sint8), ncol, f); 1416 if(nread != ncol) nrerror("fread_si8matrix : can't read data"); 1417 } 1418 fclose(f); 1419 } 1420 /* -------------------------------------------------------------------------------------------- */ 1421 IMAGE_EXPORT(void) fread_ui8matrix(char *filename, uint8 **m,long nrl,long nrh,long ncl, long nch) 1422 /* -------------------------------------------------------------------------------------------- */ 1423 { 1424 long i, ncol = nch-ncl+1, nread; 1425 FILE *f; 1426 1427 f = fopen(filename, "rb"); 1428 if(f == NULL) nrerror("Can't open file in fread_ui8matrix"); 1429 1430 for(i=nrl; i<=nrh; i++) { 1431 nread = fread(m[i]+ncl, sizeof(uint8), ncol, f); 1432 if(nread != ncol) nrerror("fread_ui8matrix : can't read data"); 1433 } 1434 fclose(f); 1435 } 1436 /* ---------------------------------------------------------------------------------------------- */ 1437 IMAGE_EXPORT(void) fread_si32matrix(char *filename, sint32 **m,long nrl,long nrh,long ncl, long nch) 1438 /* ---------------------------------------------------------------------------------------------- */ 1439 { 1440 long i, ncol = nch-ncl+1, nread; 1441 FILE *f; 1442 1443 f = fopen(filename, "rb"); 1444 if(f == NULL) 1445 nrerror("Can't open file in fread_si32matrix"); 1446 1447 for(i=nrl; i<=nrh; i++) { 1448 nread = fread(m[i]+ncl, sizeof(sint32), ncol, f); 1449 if(nread != ncol) nrerror("fread_si32matrix : can't read data"); 1450 } 1451 fclose(f); 1452 } 1453 /* ---------------------------------------------------------------------------------------------- */ 1454 IMAGE_EXPORT(void) fread_ui32matrix(char *filename, uint32 **m,long nrl,long nrh,long ncl, long nch) 1455 /* ---------------------------------------------------------------------------------------------- */ 1456 { 1457 long i, ncol = nch-ncl+1, nread; 1458 FILE *f; 1459 1460 f = fopen(filename, "rb"); 1461 if(f == NULL) 1462 nrerror("Can't open file in fread_ui32matrix"); 1463 1464 for(i=nrl; i<=nrh; i++) { 1465 nread = fread(m[i]+ncl, sizeof(uint32), ncol, f); 1466 if(nread != ncol) nrerror("fread_ui32matrix : can't read data"); 1467 } 1468 fclose(f); 1469 } 1470 /* ---------------------------------------------------------------------------------------------- */ 1471 IMAGE_EXPORT(void) fread_si64matrix(char *filename, sint64 **m,long nrl,long nrh,long ncl, long nch) 1472 /* ---------------------------------------------------------------------------------------------- */ 1473 { 1474 long i, ncol = nch-ncl+1, nread; 1475 FILE *f; 1476 1477 f = fopen(filename, "rb"); 1478 if(f == NULL) 1479 nrerror("Can't open file in fread_si64matrix"); 1480 1481 for(i=nrl; i<=nrh; i++) { 1482 nread = fread(m[i]+ncl, sizeof(sint32), ncol, f); 1483 if(nread != ncol) nrerror("fread_si64matrix : can't read data"); 1484 } 1485 fclose(f); 1486 } 1487 /* ---------------------------------------------------------------------------------------------- */ 1488 IMAGE_EXPORT(void) fread_ui64matrix(char *filename, uint64 **m,long nrl,long nrh,long ncl, long nch) 1489 /* ---------------------------------------------------------------------------------------------- */ 1490 { 1491 long i, ncol = nch-ncl+1, nread; 1492 FILE *f; 1493 1494 f = fopen(filename, "rb"); 1495 if(f == NULL) 1496 nrerror("Can't open file in fread_ui64matrix"); 1497 1498 for(i=nrl; i<=nrh; i++) { 1499 nread = fread(m[i]+ncl, sizeof(uint64), ncol, f); 1500 if(nread != ncol) nrerror("fread_ui64matrix : can't read data"); 1501 } 1502 fclose(f); 1503 } 1504 /* ---------------------------------------------------------------------------------------------- */ 1505 IMAGE_EXPORT(void) fread_f32matrix(char *filename, float32 **m,long nrl,long nrh,long ncl, long nch) 1506 /* ---------------------------------------------------------------------------------------------- */ 1507 { 1508 long i, ncol = nch-ncl+1, nread; 1509 FILE *f; 1510 1511 f = fopen(filename, "rb"); 1512 if(f == NULL) nrerror("Can't open file in fread_f32matrix"); 1513 1514 for(i=nrl; i<=nrh; i++) { 1515 nread = fread(m[i]+ncl, sizeof(float32), ncol, f); 1516 if(nread != ncol) nrerror("fread_f32matrix : can't read data"); 1517 } 1518 fclose(f); 1519 } 1520 /* ---------------------------------------------------------------------------------------------- */ 1521 IMAGE_EXPORT(void) fread_f64matrix(char *filename, float64 **m,long nrl,long nrh,long ncl, long nch) 1522 /* ---------------------------------------------------------------------------------------------- */ 1523 { 1524 long i, ncol = nch-ncl+1, nread; 1525 FILE *f; 1526 1527 f = fopen(filename, "rb"); 1528 if(f == NULL) nrerror("Can't open file in fread_f64matrix"); 1529 1530 for(i=nrl; i<=nrh; i++) { 1531 nread = fread(m[i]+ncl, sizeof(float64), ncol, f); 1532 if(nread != ncol) nrerror("fread_f64matrix : can't read data"); 1533 } 1534 fclose(f); 1535 } 1536 /* -------------------------------------------------------------------------------------------- */ 1537 IMAGE_EXPORT(void) fread_rgb8matrix(char *filename, rgb8 **m,long nrl,long nrh,long ncl, long nch) 1538 /* -------------------------------------------------------------------------------------------- */ 1539 { 1540 long i, ncol = nch-ncl+1, nread; 1541 FILE *f; 1542 1543 f = fopen(filename, "rb"); 1544 if(f == NULL) nrerror("Can't open file in fread_rgb8matrix"); 1545 1546 for(i=nrl; i<=nrh; i++) { 1547 nread = fread(m[i]+ncl, sizeof(rgb8), ncol, f); 1548 if(nread != ncol) nrerror("fread_rgb8matrix : can't read data"); 1549 } 1550 fclose(f); 1551 } 1552 /* ---------------------------------------------------------------------------------------------- */ 1553 IMAGE_EXPORT(void) fread_rgbx8matrix(char *filename, rgbx8 **m,long nrl,long nrh,long ncl, long nch) 1554 /* ---------------------------------------------------------------------------------------------- */ 1555 { 1556 long i, ncol = nch-ncl+1, nread; 1557 FILE *f; 1558 1559 f = fopen(filename, "rb"); 1560 if(f == NULL) nrerror("Can't open file in fread_rgbx8matrix"); 1561 1562 for(i=nrl; i<=nrh; i++) { 1563 nread = fread(m[i]+ncl, sizeof(rgbx8), ncol, f); 1564 if(nread != ncol) nrerror("fread_rgbx8matrix : can't read data"); 1565 } 1566 fclose(f); 1567 } 1568 // -------------------------------------------------------------------------------------------------- 1569 void write_ui8matrix_positive(uint8 **m, int i0, int i1, int j0, int j1, int iformat, char *filename) 1570 // -------------------------------------------------------------------------------------------------- 1571 { 1572 int i, j; 1573 char *format, *str; 1574 FILE *f; 1575 1576 select_display_positive_parameters(iformat, &format, &str); 1577 1578 f = fopen(filename, "wt"); 1579 if(f == NULL) { 1580 nrerror("Can't open file in write_ui8matrix_positive"); 1581 } 1582 1583 for(i=i0; i<=i1; i++) { 1584 for(j=j0; j<=j1; j++) { 1585 if(m[i][j]) { 1586 fprintf(f, format, m[i][j]); 1587 } else { 1588 fprintf(f, "%s", str); 1589 } 1590 } 1591 fputc('\n', f); 1592 } 1593 fclose(f); 1594 } 1595 1596 // ---------------------------------------------------------------------------------------------------- 1597 void write_ui16matrix_positive(uint16 **m, int i0, int i1, int j0, int j1, int iformat, char *filename) 1598 // ---------------------------------------------------------------------------------------------------- 1599 { 1600 int i, j; 1601 char *format, *str; 1602 FILE *f; 1603 1604 select_display_positive_parameters(iformat, &format, &str); 1605 1606 f = fopen(filename, "wt"); 1607 if(f == NULL) { 1608 nrerror("Can't open file in write_ui16matrix_positive"); 1609 } 1610 1611 for(i=i0; i<=i1; i++) { 1612 for(j=j0; j<=j1; j++) { 1613 if(m[i][j]) { 1614 fprintf(f, format, m[i][j]); 1615 } else { 1616 fprintf(f, "%s", str); 1617 } 1618 } 1619 fputc('\n', f); 1620 } 1621 fclose(f); 1622 } 1623 // --------------------------------------------------------------------------------------------------- 1624 void write_ui32matrix_positive(uint32 **m,int i0, int i1, int j0, int j1, int iformat, char *filename) 1625 // --------------------------------------------------------------------------------------------------- 1626 { 1627 int i, j; 1628 char *format, *str; 1629 FILE *f; 1630 1631 select_display_positive_parameters(iformat, &format, &str); 1632 1633 f = fopen(filename, "wt"); 1634 if(f == NULL) { 1635 nrerror("Can't open file in write_ui32matrix_positive"); 1636 } 1637 1638 for(i=i0; i<=i1; i++) { 1639 for(j=j0; j<=j1; j++) { 1640 if(m[i][j]) { 1641 fprintf(f, format, m[i][j]); 1642 } else { 1643 fprintf(f, "%s", str); 1644 } 1645 } 1646 fputc('\n', f); 1647 } 1648 fclose(f); 1649 } 265 #undef fread_type_matrix 266 #define fread_type_matrix(t) \ 267 void short_name(t,fread_,matrix)(char * filename, t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch) \ 268 { \ 269 int32_t ncol = nch - ncl + 1; \ 270 int32_t nread; \ 271 FILE * f = fopen(filename, "rb"); \ 272 if (f == NULL) { \ 273 nrerror("Can't open file %s in %s", filename, __func__); \ 274 } \ 275 for (int32_t i = nrl; i <= nrh; i++) { \ 276 nread = fread(m[i] + ncl, sizeof(t), ncol, f); \ 277 if (nread != ncol) { \ 278 nrerror("%s : can't read data", __func__); \ 279 } \ 280 } \ 281 fclose(f); \ 282 } 283 284 fread_type_matrix(int8_t); 285 fread_type_matrix(uint8_t); 286 fread_type_matrix(int16_t); 287 fread_type_matrix(uint16_t); 288 fread_type_matrix(int32_t); 289 fread_type_matrix(uint32_t); 290 fread_type_matrix(int64_t); 291 fread_type_matrix(uint64_t); 292 fread_type_matrix(float); 293 fread_type_matrix(double); 294 fread_type_matrix(rgb8); 295 fread_type_matrix(rgbx8); 296 297 298 299 #undef write_type_matrix_positive 300 #define write_type_matrix_positive(t) \ 301 void short_name(t,write_,matrix_positive)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, int32_t iformat, char * filename) \ 302 { \ 303 char * format; \ 304 char * str; \ 305 FILE * f; \ 306 select_display_positive_parameters(iformat, &format, &str); \ 307 f = fopen(filename, "wt"); \ 308 if (f == NULL) { \ 309 nrerror("Can't open file %s in %s", filename, __func__); \ 310 } \ 311 for (int32_t i = i0; i <= i1; i++) { \ 312 for (int32_t j = j0; j <= j1; j++) { \ 313 if (m[i][j] != 0) { \ 314 fprintf(f, format, m[i][j]); \ 315 } \ 316 else { \ 317 fprintf(f, "%s", str); \ 318 } \ 319 } \ 320 fprintf(f, "\n"); \ 321 } \ 322 fclose(f); \ 323 } 324 325 write_type_matrix_positive(uint8_t); 326 write_type_matrix_positive(uint16_t); 327 write_type_matrix_positive(uint32_t); 328 1650 329 1651 330 /* ------------------------ */ … … 1653 332 /* ------------------------ */ 1654 333 1655 PRIVATE char *readitem (FILE *file, char *buffer);1656 PRIVATE void ReadPGMrow (FILE *file, int width, uint8 *line);1657 PRIVATE void WritePGMrow(uint8 *line, int width, FILE *file);1658 334 1659 335 /* ------------------------------------------ */ 1660 PRIVATE char *readitem(FILE *file, char *buffer)336 static char * readitem(FILE * file, char * buffer) 1661 337 /* ------------------------------------------ */ 1662 // read a word 1663 // public domain function: author is unknown 1664 { 1665 char *aux; 1666 int k; 1667 1668 k=0; 1669 aux=buffer; 1670 while (!feof(file)) 1671 { 1672 *aux=fgetc(file); 1673 switch(k) 1674 { 1675 case 0: 1676 if (*aux=='#') k=1; 1677 if (isalnum(*aux)) k=2, aux++; 1678 break; 1679 case 1: 1680 if (*aux==0xA) k=0; 1681 break; 1682 case 2: 1683 if (!isalnum(*aux)) 1684 { 1685 *aux=0; 1686 return buffer; 1687 } 1688 aux++; 1689 break; 338 // read a word 339 // public domain function: author is unknown 340 { 341 char * aux; 342 int32_t k; 343 344 k = 0; 345 aux = buffer; 346 while (!feof(file)) { 347 *aux = fgetc(file); 348 switch (k) { 349 case 0: 350 if (*aux == '#') { 351 k = 1; 352 } 353 if (isalnum(*aux)) { 354 k = 2; 355 aux++; 356 } 357 break; 358 case 1: 359 if (*aux == 0xA) { 360 k = 0; 361 } 362 break; 363 case 2: 364 if (!isalnum(*aux)) { 365 *aux = 0; 366 return buffer; 367 } 368 aux++; 369 break; 1690 370 } 1691 371 } 1692 *aux =0;372 *aux = 0; 1693 373 return buffer; 1694 374 } 375 1695 376 /* ----------------------------------------------------- */ 1696 PRIVATE void ReadPGMrow(FILE *file, int width, uint8 *line)377 static void ReadPGMrow(FILE * file, int32_t width, uint8_t * line) 1697 378 /* ----------------------------------------------------- */ 1698 379 { 1699 380 // file is already open (in read) and will not be closed at the end 1700 fread(&(line[0]), sizeof(uint8), width, file); 1701 } 381 fread(&(line[0]), sizeof(uint8_t), width, file); 382 } 383 384 1702 385 /* ------------------------------------------------------- */ 1703 PRIVATE void WritePGMrow(uint8 *line, int width, FILE *file)386 static void WritePGMrow(uint8_t * line, int32_t width, FILE * file) 1704 387 /* ------------------------------------------------------- */ 1705 388 { 1706 389 // file is already open (in read) and will not be closed at the end 1707 fwrite(&(line[0]), sizeof(uint8), width, file); 1708 } 390 fwrite(&(line[0]), sizeof(uint8_t), width, file); 391 } 392 393 1709 394 /* --------------------------------------------------------------------------------------- */ 1710 IMAGE_EXPORT(uint8 **) LoadPGM_ui8matrix(char *filename, int *nrl, int *nrh, int *ncl, int *nch)395 uint8_t ** LoadPGM_ui8matrix(char * filename, int32_t * nrl, int32_t * nrh, int32_t * ncl, int32_t * nch) 1711 396 /* --------------------------------------------------------------------------------------- */ 1712 397 { 1713 398 // only for P5 binary type, not for text type 1714 1715 int height, width, gris;1716 uint8 **m;1717 FILE * file;1718 1719 char * buffer;399 400 int32_t height, width, gris; 401 uint8_t ** m; 402 FILE * file; 403 404 char * buffer; 1720 405 char msg[1024]; 1721 //char c; 1722 int i; 1723 1724 buffer = (char*) calloc(80, sizeof(char)); 406 (void) gris; 407 408 buffer = (char *) calloc(80, sizeof(char)); 1725 409 1726 410 // open file 1727 file = fopen(filename,"rb"); 1728 if (file==NULL) { 1729 //nrerror("ouverture du fichier impossible\n"); 1730 sprintf(msg, "ouverture du fichier %s impossible dans LoadPGM_ui8matrix\n", filename); 411 file = fopen(filename, "rb"); 412 if (file == NULL) { 413 snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); 1731 414 nrerror(msg); 1732 415 } 1733 416 1734 417 // read PGM header 1735 418 readitem(file, buffer); 1736 /*fscanf(fichier, "%s", buffer);*/1737 if(strcmp(buffer, "P5") != 0) nrerror("entete du fichier %s invalide\n");1738 //nrerror("entete du fichier %s invalide\n", filename);1739 419 if (strcmp(buffer, "P5") != 0) { 420 nrerror("Entete du fichier %s invalide\n", filename); 421 } 422 1740 423 width = atoi(readitem(file, buffer)); 1741 424 height = atoi(readitem(file, buffer)); 1742 425 gris = atoi(readitem(file, buffer)); 1743 426 1744 427 *nrl = 0; 1745 428 *nrh = height - 1; … … 1747 430 *nch = width - 1; 1748 431 m = ui8matrix(*nrl, *nrh, *ncl, *nch); 1749 1750 for(i =0; i<height; i++) {432 433 for(int32_t i = 0; i < height; i++) { 1751 434 ReadPGMrow(file, width, m[i]); 1752 435 } 1753 436 1754 437 fclose(file); 1755 438 free(buffer); 1756 439 1757 440 return m; 1758 441 } 1759 /* ---------------------------------------------------------------------------------------- */ 1760 IMAGE_EXPORT(uint8 **) LoadPGM_bmatrix(char *filename, int *nrl, int *nrh, int *ncl, int *nch) 1761 /* ---------------------------------------------------------------------------------------- */ 442 443 444 /* ----------------------------------------------------------------------------------------------- */ 445 void LoadPGM_ui8matrix2(char * filename, int32_t * nrl, int32_t * nrh, int32_t * ncl, int32_t * nch, uint8_t ** m) 446 /* ----------------------------------------------------------------------------------------------- */ 1762 447 { 1763 448 // only for P5 binary type, not for text type 1764 1765 int height, width, gris; 1766 uint8 **m; 1767 FILE *file; 1768 1769 char *buffer; 449 int32_t height, width, gris; 450 FILE * file; 451 452 char * buffer; 1770 453 char msg[1024]; 1771 //char c; 1772 int i; 1773 1774 buffer = (char*) calloc(80, sizeof(char)); 1775 1776 // open file 1777 file = fopen(filename,"rb"); 1778 if (file==NULL) { 1779 //nrerror("ouverture du fichier impossible\n"); 1780 sprintf(msg, "ouverture du fichier %s impossible dans SavePGM_bmatrix\n", filename); 454 (void) gris; 455 456 buffer = (char *) calloc(80, sizeof(char)); 457 /* ouverture du fichier */ 458 file = fopen(filename, "rb"); 459 if (file == NULL) { 460 snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); 1781 461 nrerror(msg); 1782 462 } 1783 1784 / / read PGM header463 464 /* lecture de l'entete du fichier pgm */ 1785 465 readitem(file, buffer); 1786 /*fscanf(fichier, "%s", buffer);*/ 1787 if(strcmp(buffer, "P5") != 0) 1788 nrerror("entete du fichier %s invalide\n"); 1789 //nrerror("entete du fichier %s invalide\n", filename); 1790 466 if (strcmp(buffer, "P5") != 0) { 467 nrerror("Entete du fichier %s invalide\n", filename); 468 } 469 1791 470 width = atoi(readitem(file, buffer)); 1792 471 height = atoi(readitem(file, buffer)); 1793 472 gris = atoi(readitem(file, buffer)); 1794 473 1795 474 *nrl = 0; 1796 475 *nrh = height - 1; 1797 476 *ncl = 0; 1798 477 *nch = width - 1; 1799 m = ui8matrix(*nrl, *nrh, *ncl, *nch); 1800 1801 for(i=0; i<height; i++) { 478 479 for (int32_t i = 0; i < height; i++) { 1802 480 ReadPGMrow(file, width, m[i]); 1803 481 } 1804 482 1805 483 fclose(file); 1806 484 free(buffer); 1807 1808 return m; 1809 } 1810 /* -------------------------------------------------------------------------------------------- ---*/1811 IMAGE_EXPORT(void) LoadPGM_bmatrix2(char *filename, int *nrl, int *nrh, int *ncl, int *nch, uint8 **m)1812 /* -------------------------------------------------------------------------------------------- ---*/485 } 486 487 488 /* -------------------------------------------------------------------------------------------- */ 489 void MLoadPGM_ui8matrix(char * filename, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, uint8_t ** m) 490 /* -------------------------------------------------------------------------------------------- */ 1813 491 { 1814 492 // only for P5 binary type, not for text type 1815 1816 int height, width, gris; 1817 FILE *file; 1818 1819 char *buffer; 493 int32_t height, width, gris; 494 FILE * file; 495 496 char * buffer; 1820 497 char msg[1024]; 1821 int i;1822 1823 buffer = (char *) calloc(80, sizeof(char));498 (void) gris; 499 500 buffer = (char *) calloc(80, sizeof(char)); 1824 501 /* ouverture du fichier */ 1825 file = fopen(filename,"rb"); 1826 if (file==NULL) { 1827 //nrerror("ouverture du fichier impossible\n"); 1828 sprintf(msg, "ouverture du fichier %s impossible dans SavePGM_bmatrix\n", filename); 502 file = fopen(filename, "rb"); 503 if (file == NULL) { 504 snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); 1829 505 nrerror(msg); 1830 506 } 1831 507 1832 508 /* lecture de l'entete du fichier pgm */ 1833 509 readitem(file, buffer); 1834 /*fscanf(fichier, "%s", buffer);*/ 1835 if(strcmp(buffer, "P5") != 0) 1836 nrerror("entete du fichier %s invalide\n"); 1837 //nrerror("entete du fichier %s invalide\n", filename); 1838 510 if (strcmp(buffer, "P5") != 0) { 511 nrerror("Entete du fichier %s invalide\n", filename); 512 } 513 1839 514 width = atoi(readitem(file, buffer)); 1840 515 height = atoi(readitem(file, buffer)); 1841 516 gris = atoi(readitem(file, buffer)); 1842 1843 *nrl = 0; 1844 *nrh = height - 1; 1845 *ncl = 0; 1846 *nch = width - 1; 1847 1848 for(i=0; i<height; i++) { 517 518 for (int32_t i = 0; i < height; i++) { 1849 519 ReadPGMrow(file, width, m[i]); 1850 520 } 1851 521 1852 522 fclose(file); 1853 523 free(buffer); 1854 524 } 1855 /* -------------------------------------------------------------------------------------------- */ 1856 IMAGE_EXPORT(void) MLoadPGM_bmatrix(char *filename, int nrl, int nrh, int ncl, int nch, uint8 **m) 1857 /* ------------------------------------------------------------------------------------------ --*/1858 { 1859 // only for P5 binary type, not for text type 1860 1861 int height, width, gris;1862 FILE *file;1863 1864 char *buffer;525 526 527 /* ------------------------------------------------------------------------------------------ */ 528 void SavePGM_ui8matrix(uint8_t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * filename) 529 /* ------------------------------------------------------------------------------------------ */ 530 { 531 int nrow = nrh - nrl + 1; 532 int ncol = nch - ncl + 1; 533 534 char buffer[80]; 1865 535 char msg[1024]; 1866 1867 int i; 1868 1869 buffer = (char*) calloc(80, sizeof(char)); 536 537 FILE * file; 538 539 file = fopen(filename, "wb"); 540 if (file == NULL) { 541 snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); 542 nrerror(msg); 543 } 544 545 /* enregistrement de l'image au format rpgm */ 546 snprintf(buffer, 80, "P5\n%d %d\n255\n", ncol, nrow); 547 fwrite(buffer, strlen(buffer), 1, file); 548 for (int32_t i = nrl; i <= nrh; i++) { 549 WritePGMrow(m[i], ncol, file); 550 } 551 552 /* fermeture du fichier */ 553 fclose(file); 554 } 555 556 557 /* --------------------------- */ 558 /* -- PNM IO for rgb8matrix -- */ 559 /* --------------------------- */ 560 561 /* ------------------------------------------------------ */ 562 static void ReadPNMrow(FILE * file, int32_t width, uint8_t * line) 563 /* ------------------------------------------------------ */ 564 { 565 /* Le fichier est ouvert (en lecture) et ne sera pas ferme a la fin */ 566 fread(&(line[0]), sizeof(uint8_t), 3 * sizeof(uint8_t) * width, file); 567 } 568 569 570 /* ------------------------------------------------------- */ 571 static void WritePNMrow(uint8_t * line, int32_t width, FILE * file) 572 /* ------------------------------------------------------- */ 573 { 574 /* Le fichier est deja ouvert et ne sera pas ferme a la fin */ 575 fwrite(&(line[0]), sizeof(uint8_t), 3 * sizeof(uint8_t) * width, file); 576 } 577 578 579 /* ------------------------------------------------------------------------------------------ */ 580 rgb8 ** LoadPPM_rgb8matrix(char * filename, int32_t * nrl, int32_t * nrh, int32_t * ncl, int32_t * nch) 581 /* ------------------------------------------------------------------------------------------ */ 582 { 583 /* cette version ne lit plus que le type P6 */ 584 int32_t height, width, gris; 585 rgb8 ** m; 586 FILE * file; 587 588 char * buffer; 589 char msg[1024]; 590 (void) gris; 591 592 buffer = (char *) calloc(80, sizeof(char)); 1870 593 /* ouverture du fichier */ 1871 594 file = fopen(filename,"rb"); 1872 if (file==NULL) { 1873 //nrerror("ouverture du fichier impossible\n"); 1874 sprintf(msg, "ouverture du fichier %s impossible dans SavePGM_bmatrix\n", filename); 595 if (file == NULL) { 596 snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); 1875 597 nrerror(msg); 1876 598 } 1877 599 1878 600 /* lecture de l'entete du fichier pgm */ 1879 601 readitem(file, buffer); 1880 /*fscanf(fichier, "%s", buffer);*/ 1881 if(strcmp(buffer, "P5") != 0) 1882 nrerror("entete du fichier %s invalide\n"); 1883 //nrerror("entete du fichier %s invalide\n", filename); 1884 602 if (strcmp(buffer, "P6") != 0) { 603 nrerror("Entete du fichier %s invalide\n", filename); 604 } 605 1885 606 width = atoi(readitem(file, buffer)); 1886 607 height = atoi(readitem(file, buffer)); 1887 608 gris = atoi(readitem(file, buffer)); 1888 1889 for(i=0; i<height; i++) { 1890 ReadPGMrow(file, width, m[i]); 1891 } 1892 1893 fclose(file); 1894 free(buffer); 1895 } 1896 /* ------------------------------------------------------------------------------------------ */ 1897 IMAGE_EXPORT(void) SavePGM_bmatrix(uint8 **m, int nrl, int nrh, int ncl, int nch, char *filename) 1898 /* ------------------------------------------------------------------------------------------ */ 1899 { 1900 int nrow = nrh-nrl+1; 1901 int ncol = nch-ncl+1; 1902 1903 char buffer[80]; 1904 char msg[1024]; 1905 1906 FILE *file; 1907 int i; 1908 1909 file = fopen(filename, "wb"); 1910 if (file == NULL) { 1911 //nrerror("ouverture du fichier %s impossible dans SavePGM_bmatrix\n", filename); 1912 sprintf(msg, "ouverture du fichier %s impossible dans SavePGM_bmatrix\n", filename); 1913 nrerror(msg); 1914 } 1915 1916 /* enregistrement de l'image au format rpgm */ 1917 sprintf(buffer,"P5\n%d %d\n255\n",ncol, nrow); 1918 fwrite(buffer,strlen(buffer),1,file); 1919 for(i=nrl; i<=nrh; i++) 1920 WritePGMrow(m[i], ncol, file); 1921 1922 /* fermeture du fichier */ 1923 fclose(file); 1924 } 1925 /* --------------------------------------------------------------------------------------------- */ 1926 IMAGE_EXPORT(void) SavePGM_ui8matrix(uint8 **m, int nrl, int nrh, int ncl, int nch, char *filename) 1927 /* --------------------------------------------------------------------------------------------- */ 1928 { 1929 int nrow = nrh-nrl+1; 1930 int ncol = nch-ncl+1; 1931 1932 char buffer[80]; 1933 char msg[1024]; 1934 1935 FILE *file; 1936 int i; 1937 1938 file = fopen(filename, "wb"); 1939 if (file == NULL) { 1940 //nrerror("ouverture du fichier %s impossible dans SavePGM_bmatrix\n", filename); 1941 sprintf(msg, "ouverture du fichier %s impossible dans SavePGM_bmatrix\n", filename); 1942 nrerror(msg); 1943 } 1944 1945 /* enregistrement de l'image au format rpgm */ 1946 sprintf(buffer,"P5\n%d %d\n255\n",ncol, nrow); 1947 fwrite(buffer,strlen(buffer),1,file); 1948 for(i=nrl; i<=nrh; i++) 1949 WritePGMrow(m[i], ncol, file); 1950 1951 /* fermeture du fichier */ 1952 fclose(file); 1953 } 1954 /* --------------------------- */ 1955 /* -- PNM IO for rgb8matrix -- */ 1956 /* --------------------------- */ 1957 1958 /* ------------------------------------------------------ */ 1959 PRIVATE void ReadPNMrow(FILE *file, int width, byte *line) 1960 /* ------------------------------------------------------ */ 1961 { 1962 /* Le fichier est ouvert (en lecture) et ne sera pas ferme a la fin */ 1963 fread(&(line[0]), sizeof(byte), 3*sizeof(byte)*width, file); 1964 } 1965 /* ------------------------------------------------------- */ 1966 PRIVATE void WritePNMrow(byte *line, int width, FILE *file) 1967 /* ------------------------------------------------------- */ 1968 { 1969 /* Le fichier est deja ouvert et ne sera pas ferme a la fin */ 1970 1971 fwrite(&(line[0]), sizeof(byte), 3*sizeof(byte)*width, file); 1972 } 1973 /* ------------------------------------------------------------------------------------------ */ 1974 IMAGE_EXPORT(rgb8 **) LoadPPM_rgb8matrix(char *filename, int *nrl, int *nrh, int *ncl, int *nch) 1975 /* ------------------------------------------------------------------------------------------ */ 1976 { 1977 /* cette version ne lit plus que le type P6 */ 1978 1979 int height, width, gris; 1980 rgb8 **m; 1981 FILE *file; 1982 //int format; 1983 1984 char *buffer; 1985 char msg[1024]; 1986 //char c; 1987 int i; 1988 1989 buffer = (char*) calloc(80, sizeof(char)); 1990 /* ouverture du fichier */ 1991 file = fopen(filename,"rb"); 1992 if (file==NULL) { 1993 //nrerror("ouverture du fichier impossible\n"); 1994 sprintf(msg, "ouverture du fichier %s impossible dans SavePGM_bmatrix\n", filename); 1995 nrerror(msg); 1996 } 1997 1998 /* lecture de l'entete du fichier pgm */ 1999 readitem(file, buffer); 2000 /*fscanf(fichier, "%s", buffer);*/ 2001 if(strcmp(buffer, "P6") != 0) 2002 nrerror("entete du fichier %s invalide\n"); 2003 //nrerror("entete du fichier %s invalide\n", filename); 2004 2005 width = atoi(readitem(file, buffer)); 2006 height = atoi(readitem(file, buffer)); 2007 gris = atoi(readitem(file, buffer)); 2008 609 2009 610 *nrl = 0; 2010 611 *nrh = height - 1; … … 2012 613 *nch = width - 1; 2013 614 m = rgb8matrix(*nrl, *nrh, *ncl, *nch); 2014 2015 for (i=0; i<height; i++) {2016 ReadPNMrow(file, width, ( byte*)m[i]);2017 } 2018 615 616 for (int32_t i = 0; i < height; i++) { 617 ReadPNMrow(file, width, (uint8_t *) m[i]); 618 } 619 2019 620 fclose(file); 2020 621 free(buffer); 2021 622 2022 623 return m; 2023 624 } 625 626 2024 627 /* -------------------------------------------------------------------------------------------------- */ 2025 IMAGE_EXPORT(void) LoadPPM_rgb8matrix2(char *filename, int *nrl, int *nrh, int *ncl, int *nch, rgb8 **m)2026 /* -------------------------------------------------------------------------------------------------- */628 void LoadPPM_rgb8matrix2(char * filename, int * nrl, int * nrh, int * ncl, int * nch, rgb8 ** m) 629 /* -------------------------------------------------------------------------------------------------- */ 2027 630 { 2028 631 /* cette version ne lit plus que le type P6 */ 2029 2030 632 int height, width, gris; 2031 FILE *file; 2032 //int format; 2033 2034 char *buffer; 633 FILE * file; 634 635 char * buffer; 2035 636 char msg[1024]; 2036 //char c; 2037 int i; 2038 2039 buffer = (char*) calloc(80, sizeof(char)); 637 (void) gris; 638 639 buffer = (char *) calloc(80, sizeof(char)); 2040 640 /* ouverture du fichier */ 2041 file = fopen(filename,"rb"); 2042 if (file==NULL) { 2043 //nrerror("ouverture du fichier impossible\n"); 2044 sprintf(msg, "ouverture du fichier %s impossible dans LoadPPM_rgb8matrix2\n", filename); 641 file = fopen(filename, "rb"); 642 if (file == NULL) { 643 snprintf(msg, 1024, "ouverture du fichier %s impossible dans LoadPPM_rgb8matrix2\n", filename); 2045 644 nrerror(msg); 2046 645 } 2047 646 2048 647 /* lecture de l'entete du fichier pgm */ 2049 648 readitem(file, buffer); 2050 /*fscanf(fichier, "%s", buffer);*/2051 if(strcmp(buffer, "P6") != 0)2052 nrerror("entete du fichier %s invalide\n");649 if (strcmp(buffer, "P6") != 0) { 650 nrerror("Entete du fichier %s invalide\n", filename); 651 } 2053 652 //nrerror("entete du fichier %s invalide\n", filename); 2054 653 2055 654 width = atoi(readitem(file, buffer)); 2056 655 height = atoi(readitem(file, buffer)); 2057 656 gris = atoi(readitem(file, buffer)); 2058 657 2059 658 *nrl = 0; 2060 659 *nrh = height - 1; 2061 660 *ncl = 0; 2062 661 *nch = width - 1; 2063 2064 for (i=0; i<height; i++) {2065 ReadPNMrow(file, width, ( byte*)m[i]);662 663 for (int32_t i = 0; i < height; i++) { 664 ReadPNMrow(file, width, (uint8_t *) m[i]); 2066 665 } 2067 666 fclose(file); 2068 667 free(buffer); 2069 668 } 669 670 2070 671 /* ------------------------------------------------------------------------------------------------- */ 2071 IMAGE_EXPORT(void) SavePPM_rgb8matrix(rgb8 **m, int nrl, int nrh, int ncl, int nch, char *filename)672 void SavePPM_rgb8matrix(rgb8 ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * filename) 2072 673 /* ------------------------------------------------------------------------------------------------- */ 2073 674 { 2074 int nrow = nrh-nrl+1;2075 int ncol = nch-ncl+1;2076 675 int32_t nrow = nrh - nrl + 1; 676 int32_t ncol = nch - ncl + 1; 677 2077 678 char buffer[80]; 2078 679 char msg[1024]; 2079 2080 FILE *file; 2081 int i; 2082 680 681 FILE * file; 682 2083 683 file = fopen(filename, "wb"); 2084 684 if (file == NULL) { 2085 //nrerror("ouverture du fichier %s impossible dans SavePGM_bmatrix\n", filename); 2086 //nrerror("ouverture du fichier %s impossible dans SavePPM_bmatrix\n"); 2087 sprintf(msg, "ouverture du fichier %s impossible dans SavePPM_rgb8matrix\n", filename); 2088 685 snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); 2089 686 nrerror(msg); 2090 687 } 2091 688 2092 689 /* enregistrement de l'image au format rpgm */ 2093 2094 sprintf(buffer,"P6\n%d %d\n255\n",ncol, nrow);2095 f write(buffer,strlen(buffer),1,file);2096 for(i=nrl; i<=nrh; i++)2097 WritePNMrow((byte*)m[i], ncol, file);2098 690 snprintf(buffer, 80, "P6\n%d %d\n255\n", ncol, nrow); 691 fwrite(buffer, strlen(buffer), 1, file); 692 for (int32_t i = nrl; i <= nrh; i++) { 693 WritePNMrow((uint8_t *) m[i], ncol, file); 694 } 695 2099 696 /* fermeture du fichier */ 2100 697 fclose(file); 2101 698 } 699 700 701 // Local Variables: 702 // tab-width: 4 703 // c-basic-offset: 4 704 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 705 // indent-tabs-mode: nil 706 // End: 707 708 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 709
Note: See TracChangeset
for help on using the changeset viewer.