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

#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>

#include "pm_c_util.h"
#include "nstring.h"
#include "mallocvar.h"
#include "ppm.h"

#include "mitsu.h"


#define HASHSIZE 2048
#define myhash(x) ((PPM_GETR(x)*3 + PPM_GETG(x)*5 + PPM_GETB(x)*7) % HASHSIZE)

typedef struct Hashinfo {
        pixel     color;
        long      flag;
        struct Hashinfo * next;
} Hashinfo;



static struct Mediasize
mediaSize(const char * const media,
          bool         const dpi300) {

    struct Mediasize medias;

    if (TOUPPER(media[0]) == 'A') {
        switch (TOUPPER(media[1])) {
        case 'S':
            medias = MSize_AS;
            break;
        case '4':
            if (TOUPPER(media[2]) == 'S')
                medias = MSize_A4S;
            else
                medias = MSize_A4;
            break;
        default:
            medias = MSize_A;
        }
    } else
        medias = MSize_User;

    if (dpi300) {
        medias.maxcols *= 2;
        medias.maxrows *= 2;
    }

    return medias;
}



static void
cmd(char const arg) {

    fputc(arg, stdout);
}



static void
datum(unsigned char const arg) {

    fputc(arg, stdout);
}



static void
data(unsigned char * const arg,
     size_t          const len) {

    fwrite((unsigned char *)arg, 1, len, stdout);
}



static void
checkAndRotate(int              const cols,
               int              const rows,
               int              const enlarge,
               struct Mediasize const medias) {

    if (cols > rows) {
        ROTATEIMG(DOROTATE);                        /* rotate image */
        if (enlarge * rows > medias.maxcols || enlarge * cols > medias.maxrows)
            pm_error("Image too large (%u x %u).  "
                     "Size of output media: %u x %u",
                     enlarge*rows, enlarge*cols,
                     medias.maxrows, medias.maxcols);
        HPIXELS(cols);
        VPIXELS(rows);
        HPIXELSOFF((medias.maxcols/enlarge - rows)/2);
        VPIXELSOFF((medias.maxrows/enlarge - cols)/2);
        pm_message("rotating image for output");
    } else {
        ROTATEIMG(DONTROTATE);
        if (enlarge * rows > medias.maxrows || enlarge * cols > medias.maxcols)
            pm_error("Image too large (%u x %u).  "
                     "Size of output media: %u x %u",
                     enlarge*rows, enlarge*cols,
                     medias.maxrows, medias.maxcols);
        HPIXELS(cols);
        VPIXELS(rows);
        HPIXELSOFF((medias.maxcols/enlarge - cols)/2);
        VPIXELSOFF((medias.maxrows/enlarge - rows)/2);
    }
}



static void
lineputinit(int              const cols,
            int              const rows,
            int              const sharpness,
            int              const enlarge,
            int              const copy,
            struct Mediasize const medias) {
    ONLINE;
    CLRMEM;
    MEDIASIZE(medias);

    switch (enlarge) {
    case 2:
        HENLARGE(ENLARGEx2); /* enlarge horizontal */
        VENLARGE(ENLARGEx2); /* enlarge vertical */
        break;
    case 3:
        HENLARGE(ENLARGEx3); /* enlarge horizontal */
        VENLARGE(ENLARGEx3); /* enlarge vertical */
        break;
    default:
        HENLARGE(NOENLARGE); /* enlarge horizontal */
        VENLARGE(NOENLARGE); /* enlarge vertical */
    }

    COLREVERSION(DONTREVERTCOLOR);
    NUMCOPY(copy);

    HOFFINCH('\000');
    VOFFINCH('\000');
    CENTERING(DONTCENTER);

    TRANSFERFORMAT(LINEORDER);
    COLORSYSTEM(RGB);
    GRAYSCALELVL(BIT_8);

    switch (sharpness) {          /* sharpness :-) */
    case 0:
        SHARPNESS(SP_NONE);
        break;
    case 1:
        SHARPNESS(SP_LOW);
        break;
    case 2:
        SHARPNESS(SP_MIDLOW);
        break;
    case 3:
        SHARPNESS(SP_MIDHIGH);
        break;
    case 4:
        SHARPNESS(SP_HIGH);
        break;
    default:
        SHARPNESS(SP_USER);
    }
    checkAndRotate(cols, rows, enlarge, medias);
    DATASTART;
}



static void
lookuptableinit(int              const sharpness,
                int              const enlarge,
                int              const copy,
                struct Mediasize const medias) {

    ONLINE;
    CLRMEM;
    MEDIASIZE(medias);

    switch (enlarge) {
    case 2:
        HENLARGE(ENLARGEx2); /* enlarge horizontal */
        VENLARGE(ENLARGEx2); /* enlarge vertical */
        break;
    case 3:
        HENLARGE(ENLARGEx3); /* enlarge horizontal */
        VENLARGE(ENLARGEx3); /* enlarge vertical */
        break;
    default:
        HENLARGE(NOENLARGE); /* enlarge horizontal */
        VENLARGE(NOENLARGE); /* enlarge vertical */
    }

    COLREVERSION(DONTREVERTCOLOR);
    NUMCOPY(copy);

    HOFFINCH('\000');
    VOFFINCH('\000');
    CENTERING(DONTCENTER);

    TRANSFERFORMAT(LOOKUPTABLE);

    switch (sharpness) {          /* sharpness :-) */
    case 0:
        SHARPNESS(SP_NONE);
        break;
    case 1:
        SHARPNESS(SP_LOW);
        break;
    case 2:
        SHARPNESS(SP_MIDLOW);
        break;
    case 3:
        SHARPNESS(SP_MIDHIGH);
        break;
    case 4:
        SHARPNESS(SP_HIGH);
        break;
    default:
        SHARPNESS(SP_USER);
    }

    LOADLOOKUPTABLE;
}



static void
lookuptabledata(int              const cols,
                int              const rows,
                int              const enlarge,
                struct Mediasize const medias) {

    DONELOOKUPTABLE;
    checkAndRotate(cols, rows, enlarge, medias);
    DATASTART;
}



static void
frametransferinit(int              const cols,
                  int              const rows,
                  int              const sharpness,
                  int              const enlarge,
                  int              const copy,
                  struct Mediasize const medias) {

    ONLINE;
    CLRMEM;
    MEDIASIZE(medias);

    switch (enlarge) {
    case 2:
        HENLARGE(ENLARGEx2); /* enlarge horizontal */
        VENLARGE(ENLARGEx2); /* enlarge vertical */
        break;
    case 3:
        HENLARGE(ENLARGEx3); /* enlarge horizontal */
        VENLARGE(ENLARGEx3); /* enlarge vertical */
        break;
    default:
        HENLARGE(NOENLARGE); /* enlarge horizontal */
        VENLARGE(NOENLARGE); /* enlarge vertical */
    }

    COLREVERSION(DONTREVERTCOLOR);
    NUMCOPY(copy);

    HOFFINCH('\000');
    VOFFINCH('\000');
    CENTERING(DONTCENTER);

    TRANSFERFORMAT(FRAMEORDER);
    COLORSYSTEM(RGB);
    GRAYSCALELVL(BIT_8);

    switch (sharpness) {          /* sharpness :-) */
    case 0:
        SHARPNESS(SP_NONE);
        break;
    case 1:
        SHARPNESS(SP_LOW);
        break;
    case 2:
        SHARPNESS(SP_MIDLOW);
        break;
    case 3:
        SHARPNESS(SP_MIDHIGH);
        break;
    case 4:
        SHARPNESS(SP_HIGH);
        break;
    default:
        SHARPNESS(SP_USER);
    }
    checkAndRotate(cols, rows, enlarge, medias);
}



static void
doLookupTableColors(colorhist_vector const table,
                    unsigned int     const nColor,
                    Hashinfo *       const colorhashtable) {

    unsigned int colval;
    for (colval = 0; colval < nColor; ++colval) {
        struct Hashinfo * const hashchain =
            &colorhashtable[myhash((table[colval]).color)];

        struct Hashinfo * hashrun;

        cmd('$');
        datum(colval);
        datum(PPM_GETR((table[colval]).color));
        datum(PPM_GETG((table[colval]).color));
        datum(PPM_GETB((table[colval]).color));

        hashrun = hashchain;  /* start at beginning of chain */

        if (hashrun->flag == -1) {
            hashrun->color = (table[colval]).color;
            hashrun->flag  = colval;
        } else {
            while (hashrun->next != NULL)
                hashrun = hashrun->next;
            MALLOCVAR_NOFAIL(hashrun->next);
            hashrun = hashrun->next;
            hashrun->color = (table[colval]).color;
            hashrun->flag  = colval;
            hashrun->next  = NULL;
        }
    }
}



static void
doLookupTableGrays(colorhist_vector const table,
                   unsigned int     const nColor,
                   Hashinfo *       const colorhashtable) {

    unsigned int colval;
    for (colval = 0; colval < nColor; ++colval) {
        struct Hashinfo * const hashchain =
            &colorhashtable[myhash((table[colval]).color)];
        struct Hashinfo * hashrun;

        cmd('$');
        datum(colval);
        datum(PPM_GETB((table[colval]).color));
        datum(PPM_GETB((table[colval]).color));
        datum(PPM_GETB((table[colval]).color));

        hashrun = hashchain;  /* start at beginning of chain */

        if (hashrun->flag == -1) {
            hashrun->color = (table[colval]).color;
            hashrun->flag  = colval;
        } else {
            while (hashrun->next != NULL)
                hashrun = hashrun->next;
            MALLOCVAR_NOFAIL(hashrun->next);
            hashrun = hashrun->next;
            hashrun->color = (table[colval]).color;
            hashrun->flag  = colval;
            hashrun->next  = NULL;
        }
    }
}



static void
generateLookupTable(colorhist_vector const table,
                    unsigned int     const nColor,
                    unsigned int     const cols,
                    unsigned int     const rows,
                    int              const format,
                    int              const sharpness,
                    int              const enlarge,
                    int              const copy,
                    struct Mediasize const medias,
                    Hashinfo **      const colorhashtableP) {
/*----------------------------------------------------------------------------
   Write to the output file the palette (color lookup table) indicated by
   'table' and generate a hash table to use with it: *colorhashtableP.

   Also write the various properties 'sharpness', 'enlarge', 'copy', and
   'medias' to the output file.
-----------------------------------------------------------------------------*/
    Hashinfo * colorhashtable;

    lookuptableinit(sharpness, enlarge, copy, medias);

    /* Initialize the hash table to empty */

    MALLOCARRAY_NOFAIL(colorhashtable, HASHSIZE);
    {
        unsigned int i;
        for (i = 0; i < HASHSIZE; ++i) {
            colorhashtable[i].flag = -1;
                    colorhashtable[i].next = NULL;
        }
    }

    switch(PPM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        doLookupTableColors(table, nColor, colorhashtable);
        break;
    default:
        doLookupTableGrays(table, nColor, colorhashtable);
    }
    lookuptabledata(cols, rows, enlarge, medias);

    *colorhashtableP = colorhashtable;
}



static void
writeColormapRaster(pixel **         const pixels,
                    unsigned int     const cols,
                    unsigned int     const rows,
                    Hashinfo *       const colorhashtable) {
/*----------------------------------------------------------------------------
   Write a colormapped raster: write the pixels pixels[][] (dimensions cols x
   rows) as indices into the colormap (palette; lookup table) indicated by
   'colorhashtable'.
-----------------------------------------------------------------------------*/
    unsigned int row;

    for (row = 0; row < rows; ++row) {
        unsigned int col;

        for (col = 0; col < cols; ++col) {
            pixel * const pixrow = pixels[row];
            struct Hashinfo * const hashchain =
                &colorhashtable[myhash(pixrow[col])];
            struct Hashinfo * p;

            p = hashchain;
            while (!PPM_EQUAL((p->color), pixrow[col])) {
                assert(p->next);
                p = p->next;
            }
            datum(p->flag);
        }
    }
}



static void
useLookupTable(pixel **         const pixels,
               colorhist_vector const table,
               int              const sharpness,
               int              const enlarge,
               int              const copy,
               struct Mediasize const medias,
               unsigned int     const cols,
               unsigned int     const rows,
               int              const format,
               unsigned int     const nColor) {

    Hashinfo * colorhashtable;

    pm_message("found %u colors - using the lookuptable-method", nColor);

    generateLookupTable(table, nColor, cols, rows, format,
                        sharpness, enlarge, copy, medias,
                        &colorhashtable);

    writeColormapRaster(pixels, cols, rows, colorhashtable);

    free(colorhashtable);
}



static void
noLookupColor(pixel **     const pixels,
              unsigned int const cols,
              unsigned int const rows) {

    unsigned int row;
    COLORDES(RED);
    DATASTART;                    /* red coming */
    for (row = 0; row < rows; ++row) {
        pixel * const pixrow = pixels[row];
        unsigned int col;
        for (col = 0; col < cols; ++col)
            datum(PPM_GETR(pixrow[col]));
    }
    COLORDES(GREEN);
    DATASTART;                    /* green coming */
    for (row = 0; row < rows; ++row) {
        pixel * const pixrow = pixels[row];
        unsigned int col;
        for (col = 0; col < cols; ++col)
            datum(PPM_GETG(pixrow[col]));
    }
    COLORDES(BLUE);
    DATASTART;                    /* blue coming */
    for (row = 0; row < rows; ++row) {
        pixel * const pixrow = pixels[row];
        unsigned int col;
        for (col = 0; col < cols; ++col)
            datum(PPM_GETB(pixrow[col]));
    }
}



static void
noLookupGray(pixel **     const pixels,
             unsigned int const cols,
             unsigned int const rows) {

    unsigned int row;
    COLORDES(RED);
    DATASTART;                    /* red coming */
    for (row = 0; row < rows; ++row) {
        pixel * const pixrow = pixels[row];
        unsigned int col;
        for (col = 0; col < cols; ++col)
            datum(PPM_GETB(pixrow[col]));
    }
    COLORDES(GREEN);
    DATASTART;                    /* green coming */
    for (row = 0; row < rows; ++row) {
        pixel * const pixrow = pixels[row];
        unsigned int col;
        for (col = 0; col < cols; ++col)
            datum(PPM_GETB(pixrow[col]));
    }
    COLORDES(BLUE);
    DATASTART;                    /* blue coming */
    for (row = 0; row < rows; ++row) {
        pixel * const pixrow = pixels[row];
        unsigned int col;
        for (col = 0; col < cols; ++col)
            datum(PPM_GETB(pixrow[col]));
    }
}



static void
useNoLookupTable(pixel **         const pixels,
                 int              const sharpness,
                 int              const enlarge,
                 int              const copy,
                 struct Mediasize const medias,
                 unsigned int     const cols,
                 unsigned int     const rows,
                 int              const format) {

    /* $#%@^!& no lut possible, so send the pic as 24bit */

    pm_message("found too many colors for fast lookuptable mode");

    frametransferinit(cols, rows, sharpness, enlarge, copy, medias);
    switch(PPM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        noLookupColor(pixels, cols, rows);
        break;
    default:
        noLookupGray(pixels, cols, rows);
    }
}



static void
doTiny(FILE *           const ifP,
       unsigned int     const cols,
       unsigned int     const rows,
       pixval           const maxval,
       int              const format,
       int              const sharpness,
       int              const enlarge,
       int              const copy,
       struct Mediasize const medias) {

    pixel * pixelrow;
    unsigned char * redrow;
    unsigned char * grnrow;
    unsigned char * blurow;
    unsigned int row;

    pixelrow = ppm_allocrow(cols);
    MALLOCARRAY_NOFAIL(redrow, cols);
    MALLOCARRAY_NOFAIL(grnrow, cols);
    MALLOCARRAY_NOFAIL(blurow, cols);
    lineputinit(cols, rows, sharpness, enlarge, copy, medias);

    for (row = 0; row < rows; ++row) {
        ppm_readppmrow(ifP, pixelrow, cols, maxval, format);
        switch(PPM_FORMAT_TYPE(format)) {
        case PPM_TYPE: {            /* color */
            unsigned int col;
            for (col = 0; col < cols; ++col) {
                redrow[col] = PPM_GETR(pixelrow[col]);
                grnrow[col] = PPM_GETG(pixelrow[col]);
                blurow[col] = PPM_GETB(pixelrow[col]);
            }
            data(redrow, cols);
            data(grnrow, cols);
            data(blurow, cols);
        } break;
        default: {           /* grayscale */
            unsigned int col;
            for (col = 0; col < cols; ++col)
                blurow[col] = PPM_GETB(pixelrow[col]);
            data(blurow, cols);
            data(blurow, cols);
            data(blurow, cols);
        }
        }
    }
}



int
main(int argc, const char ** argv) {

    FILE *           ifP;
    int              argn;
    bool             dpi300;
    bool             tiny;
    int              cols, rows, format;
    pixval           maxval;
    int              sharpness, enlarge, copy;
    struct Mediasize medias;
    char             media[16];
    const char * const usage = "[-sharpness <1-4>] [-enlarge <1-3>] [-media <a,a4,as,a4s>] [-copy <1-9>] [-tiny] [-dpi300] [ppmfile]";

    pm_proginit(&argc, argv);

    dpi300 = false;
    argn = 1;
    sharpness = 32;
    enlarge   = 1;
    copy      = 1;
    memset(media, '\0', 16);
    tiny      = false;

    /* check for flags */
    while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
    if (pm_keymatch(argv[argn], "-sharpness", 2)) {
        ++argn;
        if (argn == argc || sscanf(argv[argn], "%d", &sharpness) != 1)
            pm_usage(usage);
        else if (sharpness < 1 || sharpness > 4)
            pm_usage(usage);
        }
    else if (pm_keymatch(argv[argn], "-enlarge", 2)) {
        ++argn;
        if (argn == argc || sscanf(argv[argn], "%d", &enlarge) != 1)
            pm_usage(usage);
        else if (enlarge < 1 || enlarge > 3)
            pm_usage(usage);
        }
    else if (pm_keymatch(argv[argn], "-media", 2)) {
        ++argn;
        if (argn == argc || sscanf(argv[argn], "%15s", media) < 1)
            pm_usage(usage);
        else if (TOUPPER(media[0]) != 'A')
            pm_usage(usage);
    }
    else if (pm_keymatch(argv[argn], "-copy", 2)) {
        ++argn;
        if (argn == argc || sscanf(argv[argn], "%d", &copy) != 1)
            pm_usage(usage);
        else if (copy < 1 || copy > 9)
            pm_usage(usage);
        }
    else if (pm_keymatch(argv[argn], "-dpi300", 2))
        dpi300 = true;
    else if (pm_keymatch(argv[argn], "-tiny", 2))
        tiny = true;
    else
        pm_usage(usage);
    ++argn;
    }

    if (argn < argc) {
        ifP = pm_openr(argv[argn]);
        ++argn;
    }
    else
        ifP = stdin;

    if (argn != argc)
        pm_usage(usage);

    medias = mediaSize(media, dpi300);

    ppm_readppminit(ifP, &cols, &rows, &maxval, &format);

    if (tiny) {
        doTiny(ifP, cols, rows, maxval, format,
               sharpness, enlarge, copy, medias);

    } else {
        pixel ** pixels;
        int nColor;
        colorhist_vector table;
        unsigned int row;

        pixels = ppm_allocarray(cols, rows);
        for (row = 0; row < rows; ++row)
            ppm_readppmrow(ifP, pixels[row], cols, maxval, format);

        /* first check whether we can use the lut transfer */

        table = ppm_computecolorhist(pixels, cols, rows, MAXLUTCOL+1,
                                     &nColor);
        if (table)
            useLookupTable(pixels, table, sharpness, enlarge, copy, medias,
                           cols, rows, format, nColor);
        else
            useNoLookupTable(pixels, sharpness, enlarge, copy, medias,
                             cols, rows, format);
        ppm_freearray(pixels, rows);
    }
    PRINTIT;
    pm_close(ifP);
    return 0;
}