| 1 | /* ---------------- */ |
|---|
| 2 | /* --- nrio2f.c --- */ |
|---|
| 3 | /* ---------------- */ |
|---|
| 4 | |
|---|
| 5 | /* |
|---|
| 6 | * Copyright (c) 2000-2014, Lionel Lacassagne, All rights reserved |
|---|
| 7 | * Univ Paris Sud XI, CNRS |
|---|
| 8 | * |
|---|
| 9 | * Distributed under the Boost Software License, Version 1.0 |
|---|
| 10 | * see accompanying file LICENSE.txt or copy it at |
|---|
| 11 | * http://www.boost.org/LICENSE_1_0.txt |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #include <stdio.h> |
|---|
| 15 | #include <stddef.h> |
|---|
| 16 | #include <stdlib.h> |
|---|
| 17 | #include <string.h> |
|---|
| 18 | #include <ctype.h> |
|---|
| 19 | |
|---|
| 20 | #include "nrc_os_config.h" |
|---|
| 21 | #include "mypredef.h" |
|---|
| 22 | #include "nrtype.h" |
|---|
| 23 | #include "nrdef.h" |
|---|
| 24 | #include "nrmacro.h" |
|---|
| 25 | #include "nrkernel.h" |
|---|
| 26 | |
|---|
| 27 | #include "nralloc1.h" |
|---|
| 28 | #include "nralloc2.h" |
|---|
| 29 | #include "nrio0.h" |
|---|
| 30 | #include "nrio1.h" |
|---|
| 31 | #include "nrio2.h" |
|---|
| 32 | |
|---|
| 33 | /* |
|---|
| 34 | * -------------------- |
|---|
| 35 | * --- write_matrix --- |
|---|
| 36 | * -------------------- |
|---|
| 37 | */ |
|---|
| 38 | |
|---|
| 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 | |
|---|
| 75 | |
|---|
| 76 | /* |
|---|
| 77 | * ---------------------- |
|---|
| 78 | * --- write_matrix_T --- |
|---|
| 79 | * ---------------------- |
|---|
| 80 | */ |
|---|
| 81 | |
|---|
| 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 | |
|---|
| 120 | |
|---|
| 121 | /* |
|---|
| 122 | * --------------------------- |
|---|
| 123 | * --- write_matrix_number --- |
|---|
| 124 | * --------------------------- |
|---|
| 125 | */ |
|---|
| 126 | |
|---|
| 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 | |
|---|
| 172 | /* |
|---|
| 173 | * ----------------------------- |
|---|
| 174 | * --- write_matrix_T_number --- |
|---|
| 175 | * ----------------------------- |
|---|
| 176 | */ |
|---|
| 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 | |
|---|
| 223 | /* |
|---|
| 224 | * --------------------- |
|---|
| 225 | * --- fwrite_matrix --- |
|---|
| 226 | * --------------------- |
|---|
| 227 | */ |
|---|
| 228 | |
|---|
| 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 | |
|---|
| 258 | |
|---|
| 259 | /* |
|---|
| 260 | * -------------------- |
|---|
| 261 | * --- fread_matrix --- |
|---|
| 262 | * -------------------- |
|---|
| 263 | */ |
|---|
| 264 | |
|---|
| 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 | |
|---|
| 329 | |
|---|
| 330 | /* ------------------------ */ |
|---|
| 331 | /* -- PGM IO for bmatrix -- */ |
|---|
| 332 | /* ------------------------ */ |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | /* ------------------------------------------ */ |
|---|
| 336 | static char * readitem(FILE * file, char * buffer) |
|---|
| 337 | /* ------------------------------------------ */ |
|---|
| 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; |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | *aux = 0; |
|---|
| 373 | return buffer; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | /* ----------------------------------------------------- */ |
|---|
| 377 | static void ReadPGMrow(FILE * file, int32_t width, uint8_t * line) |
|---|
| 378 | /* ----------------------------------------------------- */ |
|---|
| 379 | { |
|---|
| 380 | // file is already open (in read) and will not be closed at the end |
|---|
| 381 | fread(&(line[0]), sizeof(uint8_t), width, file); |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | /* ------------------------------------------------------- */ |
|---|
| 386 | static void WritePGMrow(uint8_t * line, int32_t width, FILE * file) |
|---|
| 387 | /* ------------------------------------------------------- */ |
|---|
| 388 | { |
|---|
| 389 | // file is already open (in read) and will not be closed at the end |
|---|
| 390 | fwrite(&(line[0]), sizeof(uint8_t), width, file); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | /* --------------------------------------------------------------------------------------- */ |
|---|
| 395 | uint8_t ** LoadPGM_ui8matrix(char * filename, int32_t * nrl, int32_t * nrh, int32_t * ncl, int32_t * nch) |
|---|
| 396 | /* --------------------------------------------------------------------------------------- */ |
|---|
| 397 | { |
|---|
| 398 | // only for P5 binary type, not for text type |
|---|
| 399 | |
|---|
| 400 | int32_t height, width, gris; |
|---|
| 401 | uint8_t ** m; |
|---|
| 402 | FILE * file; |
|---|
| 403 | |
|---|
| 404 | char * buffer; |
|---|
| 405 | char msg[1024]; |
|---|
| 406 | (void) gris; |
|---|
| 407 | |
|---|
| 408 | buffer = (char *) calloc(80, sizeof(char)); |
|---|
| 409 | |
|---|
| 410 | // open file |
|---|
| 411 | file = fopen(filename, "rb"); |
|---|
| 412 | if (file == NULL) { |
|---|
| 413 | snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); |
|---|
| 414 | nrerror(msg); |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | // read PGM header |
|---|
| 418 | readitem(file, buffer); |
|---|
| 419 | if (strcmp(buffer, "P5") != 0) { |
|---|
| 420 | nrerror("Entete du fichier %s invalide\n", filename); |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | width = atoi(readitem(file, buffer)); |
|---|
| 424 | height = atoi(readitem(file, buffer)); |
|---|
| 425 | gris = atoi(readitem(file, buffer)); |
|---|
| 426 | |
|---|
| 427 | *nrl = 0; |
|---|
| 428 | *nrh = height - 1; |
|---|
| 429 | *ncl = 0; |
|---|
| 430 | *nch = width - 1; |
|---|
| 431 | m = ui8matrix(*nrl, *nrh, *ncl, *nch); |
|---|
| 432 | |
|---|
| 433 | for(int32_t i = 0; i < height; i++) { |
|---|
| 434 | ReadPGMrow(file, width, m[i]); |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | fclose(file); |
|---|
| 438 | free(buffer); |
|---|
| 439 | |
|---|
| 440 | return m; |
|---|
| 441 | } |
|---|
| 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 | /* ----------------------------------------------------------------------------------------------- */ |
|---|
| 447 | { |
|---|
| 448 | // only for P5 binary type, not for text type |
|---|
| 449 | int32_t height, width, gris; |
|---|
| 450 | FILE * file; |
|---|
| 451 | |
|---|
| 452 | char * buffer; |
|---|
| 453 | char msg[1024]; |
|---|
| 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__); |
|---|
| 461 | nrerror(msg); |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | /* lecture de l'entete du fichier pgm */ |
|---|
| 465 | readitem(file, buffer); |
|---|
| 466 | if (strcmp(buffer, "P5") != 0) { |
|---|
| 467 | nrerror("Entete du fichier %s invalide\n", filename); |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | width = atoi(readitem(file, buffer)); |
|---|
| 471 | height = atoi(readitem(file, buffer)); |
|---|
| 472 | gris = atoi(readitem(file, buffer)); |
|---|
| 473 | |
|---|
| 474 | *nrl = 0; |
|---|
| 475 | *nrh = height - 1; |
|---|
| 476 | *ncl = 0; |
|---|
| 477 | *nch = width - 1; |
|---|
| 478 | |
|---|
| 479 | for (int32_t i = 0; i < height; i++) { |
|---|
| 480 | ReadPGMrow(file, width, m[i]); |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | fclose(file); |
|---|
| 484 | free(buffer); |
|---|
| 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 | /* -------------------------------------------------------------------------------------------- */ |
|---|
| 491 | { |
|---|
| 492 | // only for P5 binary type, not for text type |
|---|
| 493 | int32_t height, width, gris; |
|---|
| 494 | FILE * file; |
|---|
| 495 | |
|---|
| 496 | char * buffer; |
|---|
| 497 | char msg[1024]; |
|---|
| 498 | (void) gris; |
|---|
| 499 | |
|---|
| 500 | buffer = (char *) calloc(80, sizeof(char)); |
|---|
| 501 | /* ouverture du fichier */ |
|---|
| 502 | file = fopen(filename, "rb"); |
|---|
| 503 | if (file == NULL) { |
|---|
| 504 | snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); |
|---|
| 505 | nrerror(msg); |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | /* lecture de l'entete du fichier pgm */ |
|---|
| 509 | readitem(file, buffer); |
|---|
| 510 | if (strcmp(buffer, "P5") != 0) { |
|---|
| 511 | nrerror("Entete du fichier %s invalide\n", filename); |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | width = atoi(readitem(file, buffer)); |
|---|
| 515 | height = atoi(readitem(file, buffer)); |
|---|
| 516 | gris = atoi(readitem(file, buffer)); |
|---|
| 517 | |
|---|
| 518 | for (int32_t i = 0; i < height; i++) { |
|---|
| 519 | ReadPGMrow(file, width, m[i]); |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | fclose(file); |
|---|
| 523 | free(buffer); |
|---|
| 524 | } |
|---|
| 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]; |
|---|
| 535 | char msg[1024]; |
|---|
| 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)); |
|---|
| 593 | /* ouverture du fichier */ |
|---|
| 594 | file = fopen(filename,"rb"); |
|---|
| 595 | if (file == NULL) { |
|---|
| 596 | snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); |
|---|
| 597 | nrerror(msg); |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | /* lecture de l'entete du fichier pgm */ |
|---|
| 601 | readitem(file, buffer); |
|---|
| 602 | if (strcmp(buffer, "P6") != 0) { |
|---|
| 603 | nrerror("Entete du fichier %s invalide\n", filename); |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | width = atoi(readitem(file, buffer)); |
|---|
| 607 | height = atoi(readitem(file, buffer)); |
|---|
| 608 | gris = atoi(readitem(file, buffer)); |
|---|
| 609 | |
|---|
| 610 | *nrl = 0; |
|---|
| 611 | *nrh = height - 1; |
|---|
| 612 | *ncl = 0; |
|---|
| 613 | *nch = width - 1; |
|---|
| 614 | m = rgb8matrix(*nrl, *nrh, *ncl, *nch); |
|---|
| 615 | |
|---|
| 616 | for (int32_t i = 0; i < height; i++) { |
|---|
| 617 | ReadPNMrow(file, width, (uint8_t *) m[i]); |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | fclose(file); |
|---|
| 621 | free(buffer); |
|---|
| 622 | |
|---|
| 623 | return m; |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | /* -------------------------------------------------------------------------------------------------- */ |
|---|
| 628 | void LoadPPM_rgb8matrix2(char * filename, int * nrl, int * nrh, int * ncl, int * nch, rgb8 ** m) |
|---|
| 629 | /* -------------------------------------------------------------------------------------------------- */ |
|---|
| 630 | { |
|---|
| 631 | /* cette version ne lit plus que le type P6 */ |
|---|
| 632 | int height, width, gris; |
|---|
| 633 | FILE * file; |
|---|
| 634 | |
|---|
| 635 | char * buffer; |
|---|
| 636 | char msg[1024]; |
|---|
| 637 | (void) gris; |
|---|
| 638 | |
|---|
| 639 | buffer = (char *) calloc(80, sizeof(char)); |
|---|
| 640 | /* ouverture du fichier */ |
|---|
| 641 | file = fopen(filename, "rb"); |
|---|
| 642 | if (file == NULL) { |
|---|
| 643 | snprintf(msg, 1024, "ouverture du fichier %s impossible dans LoadPPM_rgb8matrix2\n", filename); |
|---|
| 644 | nrerror(msg); |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | /* lecture de l'entete du fichier pgm */ |
|---|
| 648 | readitem(file, buffer); |
|---|
| 649 | if (strcmp(buffer, "P6") != 0) { |
|---|
| 650 | nrerror("Entete du fichier %s invalide\n", filename); |
|---|
| 651 | } |
|---|
| 652 | //nrerror("entete du fichier %s invalide\n", filename); |
|---|
| 653 | |
|---|
| 654 | width = atoi(readitem(file, buffer)); |
|---|
| 655 | height = atoi(readitem(file, buffer)); |
|---|
| 656 | gris = atoi(readitem(file, buffer)); |
|---|
| 657 | |
|---|
| 658 | *nrl = 0; |
|---|
| 659 | *nrh = height - 1; |
|---|
| 660 | *ncl = 0; |
|---|
| 661 | *nch = width - 1; |
|---|
| 662 | |
|---|
| 663 | for (int32_t i = 0; i < height; i++) { |
|---|
| 664 | ReadPNMrow(file, width, (uint8_t *) m[i]); |
|---|
| 665 | } |
|---|
| 666 | fclose(file); |
|---|
| 667 | free(buffer); |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | /* ------------------------------------------------------------------------------------------------- */ |
|---|
| 672 | void SavePPM_rgb8matrix(rgb8 ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * filename) |
|---|
| 673 | /* ------------------------------------------------------------------------------------------------- */ |
|---|
| 674 | { |
|---|
| 675 | int32_t nrow = nrh - nrl + 1; |
|---|
| 676 | int32_t ncol = nch - ncl + 1; |
|---|
| 677 | |
|---|
| 678 | char buffer[80]; |
|---|
| 679 | char msg[1024]; |
|---|
| 680 | |
|---|
| 681 | FILE * file; |
|---|
| 682 | |
|---|
| 683 | file = fopen(filename, "wb"); |
|---|
| 684 | if (file == NULL) { |
|---|
| 685 | snprintf(msg, 1024, "ouverture du fichier %s impossible dans %s\n", filename, __func__); |
|---|
| 686 | nrerror(msg); |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | /* enregistrement de l'image au format rpgm */ |
|---|
| 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 | |
|---|
| 696 | /* fermeture du fichier */ |
|---|
| 697 | fclose(file); |
|---|
| 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 | |
|---|