summaryrefslogtreecommitdiff
blob: 5b27b623633519afca2d33c73540b74dda122ff9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
/* Copyright (C) 2001-2020 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


/* setscreen operator for Ghostscript library */
#include "memory_.h"
#include "string_.h"
#include <stdlib.h>             /* for qsort */
#include "gx.h"
#include "gserrors.h"
#include "gsstruct.h"
#include "gsutil.h"             /* for gs_next_ids */
#include "gxarith.h"            /* for igcd */
#include "gzstate.h"
#include "gxdevice.h"           /* for gzht.h */
#include "gzht.h"
#include "gxfmap.h"             /* For effective transfer usage in threshold */

/* Forward declarations */
void gx_set_effective_transfer(gs_gstate *);

/* Structure types */
public_st_ht_order();
private_st_ht_order_component();
public_st_ht_order_comp_element();
public_st_halftone();
public_st_device_halftone();

/* GC procedures */

static
ENUM_PTRS_WITH(ht_order_enum_ptrs, gx_ht_order *porder) return 0;
case 0: ENUM_RETURN((porder->data_memory ? porder->levels : 0));
case 1: ENUM_RETURN((porder->data_memory ? porder->bit_data : 0));
case 2: ENUM_RETURN(porder->cache);
case 3: ENUM_RETURN(porder->transfer);
ENUM_PTRS_END
static
RELOC_PTRS_WITH(ht_order_reloc_ptrs, gx_ht_order *porder)
{
    if (porder->data_memory) {
        RELOC_VAR(porder->levels);
        RELOC_VAR(porder->bit_data);
    }
    RELOC_VAR(porder->cache);
    RELOC_VAR(porder->transfer);
}
RELOC_PTRS_END

static
ENUM_PTRS_WITH(halftone_enum_ptrs, gs_halftone *hptr) return 0;
case 0:
switch (hptr->type)
{
    case ht_type_spot:
        ENUM_RETURN((hptr->params.spot.transfer == 0 ?
                     hptr->params.spot.transfer_closure.data :
                     0));
    case ht_type_threshold:
        ENUM_RETURN_CONST_STRING_PTR(gs_halftone, params.threshold.thresholds);
    case ht_type_threshold2:
        return ENUM_CONST_BYTESTRING(&hptr->params.threshold2.thresholds);
    case ht_type_client_order:
        ENUM_RETURN(hptr->params.client_order.client_data);
    case ht_type_multiple:
    case ht_type_multiple_colorscreen:
        ENUM_RETURN(hptr->params.multiple.components);
    case ht_type_none:
    case ht_type_screen:
    case ht_type_colorscreen:
        return 0;
}
/* fall through */
case 1:
switch (hptr->type) {
    case ht_type_threshold:
        ENUM_RETURN((hptr->params.threshold.transfer == 0 ?
                     hptr->params.threshold.transfer_closure.data :
                     0));
    case ht_type_threshold2:
        ENUM_RETURN(hptr->params.threshold2.transfer_closure.data);
    case ht_type_client_order:
        ENUM_RETURN(hptr->params.client_order.transfer_closure.data);
    default:
        return 0;
}
ENUM_PTRS_END

static RELOC_PTRS_WITH(halftone_reloc_ptrs, gs_halftone *hptr)
{
    switch (hptr->type) {
        case ht_type_spot:
            if (hptr->params.spot.transfer == 0)
                RELOC_PTR(gs_halftone, params.spot.transfer_closure.data);
            break;
        case ht_type_threshold:
            RELOC_CONST_STRING_PTR(gs_halftone, params.threshold.thresholds);
            if (hptr->params.threshold.transfer == 0)
                RELOC_PTR(gs_halftone, params.threshold.transfer_closure.data);
            break;
        case ht_type_threshold2:
            RELOC_CONST_BYTESTRING_VAR(hptr->params.threshold2.thresholds);
            RELOC_OBJ_VAR(hptr->params.threshold2.transfer_closure.data);
            break;
        case ht_type_client_order:
            RELOC_PTR(gs_halftone, params.client_order.client_data);
            RELOC_PTR(gs_halftone, params.client_order.transfer_closure.data);
            break;
        case ht_type_multiple:
        case ht_type_multiple_colorscreen:
            RELOC_PTR(gs_halftone, params.multiple.components);
            break;
        case ht_type_none:
        case ht_type_screen:
        case ht_type_colorscreen:
            break;
    }
}
RELOC_PTRS_END

/* setscreen */
int
gs_setscreen(gs_gstate * pgs, gs_screen_halftone * phsp)
{
    gs_screen_enum senum;
    int code = gx_ht_process_screen(&senum, pgs, phsp,
                                    gs_currentaccuratescreens(pgs->memory));

    if (code < 0)
        return code;
    return gs_screen_install(&senum);
}

/* currentscreen */
int
gs_currentscreen(const gs_gstate * pgs, gs_screen_halftone * phsp)
{
    switch (pgs->halftone->type) {
        case ht_type_screen:
            *phsp = pgs->halftone->params.screen;
            return 0;
        case ht_type_colorscreen:
            *phsp = pgs->halftone->params.colorscreen.screens.colored.gray;
            return 0;
        default:
            return_error(gs_error_undefined);
    }
}

/* .currentscreenlevels */
int
gs_currentscreenlevels(const gs_gstate * pgs)
{
    int gi = 0;

    if (pgs->device != 0)
        gi = pgs->device->color_info.gray_index;
    if (gi != GX_CINFO_COMP_NO_INDEX)
        return pgs->dev_ht->components[gi].corder.num_levels;
    else
        return pgs->dev_ht->components[0].corder.num_levels;
}

/* .setscreenphase */
int
gx_gstate_setscreenphase(gs_gstate * pgs, int x, int y,
                         gs_color_select_t select)
{
    if (select == gs_color_select_all) {
        int i;

        for (i = 0; i < gs_color_select_count; ++i)
            gx_gstate_setscreenphase(pgs, x, y, (gs_color_select_t) i);
        return 0;
    } else if ((int)select < 0 || (int)select >= gs_color_select_count)
        return_error(gs_error_rangecheck);
    pgs->screen_phase[select].x = x;
    pgs->screen_phase[select].y = y;
    return 0;
}
int
gs_setscreenphase(gs_gstate * pgs, int x, int y, gs_color_select_t select)
{
    int code = gx_gstate_setscreenphase(pgs, x, y,
                                        select);

    /*
     * If we're only setting the source phase, we don't need to do
     * unset_dev_color, because the source phase doesn't affect painting
     * with the current color.
     */
    if (code >= 0 && (select == gs_color_select_texture ||
                      select == gs_color_select_all)
        )
        gx_unset_dev_color(pgs);
    return code;
}

int
gs_currentscreenphase_pgs(const gs_gstate * pgs, gs_int_point * pphase,
                      gs_color_select_t select)
{
    if ((int)select < 0 || (int)select >= gs_color_select_count)
        return_error(gs_error_rangecheck);
    *pphase = pgs->screen_phase[select];
    return 0;
}

/* .currentscreenphase */
int
gs_currentscreenphase(const gs_gstate * pgs, gs_int_point * pphase,
                      gs_color_select_t select)
{
    return gs_currentscreenphase_pgs((const gs_gstate *)pgs, pphase, select);
}

/* currenthalftone */
int
gs_currenthalftone(gs_gstate * pgs, gs_halftone * pht)
{
    *pht = *pgs->halftone;
    return 0;
}

/* ------ Internal routines ------ */

/* Process one screen plane. */
int
gx_ht_process_screen_memory(gs_screen_enum * penum, gs_gstate * pgs,
                gs_screen_halftone * phsp, bool accurate, gs_memory_t * mem)
{
    gs_point pt;
    int code = gs_screen_init_memory(penum, pgs, phsp, accurate, mem);

    if (code < 0)
        return code;
    while ((code = gs_screen_currentpoint(penum, &pt)) == 0)
        if ((code = gs_screen_next(penum, (*phsp->spot_function) (pt.x, pt.y))) < 0)
            return code;
    return 0;
}

/*
 * Internal procedure to allocate and initialize either an internally
 * generated or a client-defined halftone order.  For spot halftones,
 * the client is responsible for calling gx_compute_cell_values.
 */
int
gx_ht_alloc_ht_order(gx_ht_order * porder, uint width, uint height,
                     uint num_levels, uint num_bits, uint strip_shift,
                     const gx_ht_order_procs_t *procs, gs_memory_t * mem)
{
    porder->threshold = NULL;
    porder->width = width;
    porder->height = height;
    porder->raster = bitmap_raster(width);
    porder->shift = strip_shift;
    porder->orig_height = porder->height;
    porder->orig_shift = porder->shift;
    porder->full_height = ht_order_full_height(porder);
    porder->num_levels = num_levels;
    porder->num_bits = num_bits;
    porder->procs = procs;
    porder->data_memory = mem;

    if (num_levels > 0) {
        porder->levels =
            (uint *)gs_alloc_byte_array(mem, porder->num_levels, sizeof(uint),
                                        "alloc_ht_order_data(levels)");
        if (porder->levels == 0)
            return_error(gs_error_VMerror);
        memset(porder->levels, 0, sizeof(uint) * porder->num_levels);
    } else
        porder->levels = 0;

    if (num_bits > 0) {
        porder->bit_data =
            gs_alloc_byte_array(mem, porder->num_bits,
                                porder->procs->bit_data_elt_size,
                                "alloc_ht_order_data(bit_data)");
        if (porder->bit_data == 0) {
            gs_free_object(mem, porder->levels, "alloc_ht_order_data(levels)");
            porder->levels = 0;
            return_error(gs_error_VMerror);
        }
    } else
        porder->bit_data = 0;

    porder->cache = 0;
    porder->transfer = 0;
    return 0;
}

/*
 * Procedure to copy a halftone order.
 */
static int
gx_ht_copy_ht_order(gx_ht_order * pdest, gx_ht_order * psrc, gs_memory_t * mem)
{
    int code;

    *pdest = *psrc;

    code = gx_ht_alloc_ht_order(pdest, psrc->width, psrc->height,
                     psrc->num_levels, psrc->num_bits, psrc->shift,
                     psrc->procs, mem);
    if (code < 0)
        return code;
    if (pdest->levels != 0)
        memcpy(pdest->levels, psrc->levels, psrc->num_levels * sizeof(uint));
    if (pdest->bit_data != 0)
        memcpy(pdest->bit_data, psrc->bit_data,
               (size_t)psrc->num_bits * psrc->procs->bit_data_elt_size);
    pdest->transfer = psrc->transfer;
    rc_increment(pdest->transfer);
    return 0;
}

/*
 * Set the destination component to match the source component, and
 * "assume ownership" of all of the refrernced data structures.
 */
static void
gx_ht_move_ht_order(gx_ht_order * pdest, gx_ht_order * psrc)
{
    uint    width = psrc->width, height = psrc->height, shift = psrc->shift;

    pdest->params = psrc->params;
    pdest->width = width;
    pdest->height = height;
    pdest->raster = bitmap_raster(width);
    pdest->shift = shift;
    pdest->orig_height = height;
    pdest->orig_shift = shift;
    pdest->full_height = ht_order_full_height(pdest);
    pdest->num_levels = psrc->num_levels;
    pdest->num_bits = psrc->num_bits;
    pdest->procs = psrc->procs;
    pdest->data_memory = psrc->data_memory;
    pdest->levels = psrc->levels;
    pdest->bit_data = psrc->bit_data;
    pdest->cache = psrc->cache;    /* should be 0 */
    pdest->transfer = psrc->transfer;
}

/* Allocate and initialize the contents of a halftone order. */
/* The client must have set the defining values in porder->params. */
int
gx_ht_alloc_order(gx_ht_order * porder, uint width, uint height,
                  uint strip_shift, uint num_levels, gs_memory_t * mem)
{
    gx_ht_order order;
    int code;

    order = *porder;
    gx_compute_cell_values(&order.params);
    code = gx_ht_alloc_ht_order(&order, width, height, num_levels,
                                width * height, strip_shift,
                                &ht_order_procs_default, mem);
    if (code < 0)
        return code;
    *porder = order;
    return 0;
}

/*
 * Allocate and initialize a threshold order, which may use the short
 * representation.
 */
int
gx_ht_alloc_threshold_order(gx_ht_order * porder, uint width, uint height,
                            uint num_levels, gs_memory_t * mem)
{
    gx_ht_order order;
    uint num_bits = width * height;
    const gx_ht_order_procs_t *procs =
        (num_bits > 2000 && num_bits <= max_ushort ?
         &ht_order_procs_short : &ht_order_procs_default);
    int code;

    order = *porder;
    gx_compute_cell_values(&order.params);
    code = gx_ht_alloc_ht_order(&order, width, height, num_levels,
                                width * height, 0, procs, mem);
    if (code < 0)
        return code;
    *porder = order;
    return 0;
}

/* Allocate and initialize the contents of a client-defined halftone order. */
int
gx_ht_alloc_client_order(gx_ht_order * porder, uint width, uint height,
                         uint num_levels, uint num_bits, gs_memory_t * mem)
{
    gx_ht_order order;
    int code;

    order = *porder;
    order.params.M = width, order.params.N = 0;
    order.params.R = 1;
    order.params.M1 = height, order.params.N1 = 0;
    order.params.R1 = 1;
    gx_compute_cell_values(&order.params);
    code = gx_ht_alloc_ht_order(&order, width, height, num_levels,
                                num_bits, 0, &ht_order_procs_default, mem);
    if (code < 0)
        return code;
    *porder = order;
    return 0;
}

/* Compare keys ("masks", actually sample values) for qsort. */
static int
compare_samples(const void *p1, const void *p2)
{
    ht_sample_t m1 = ((const gx_ht_bit *)p1)->mask;
    ht_sample_t m2 = ((const gx_ht_bit *)p2)->mask;

    /* force qsort() to be determinstic even if two masks are the same */
    if (m1==m2) {
      m1=((const gx_ht_bit *)p1)->offset;
      m2=((const gx_ht_bit *)p2)->offset;
    }

    return (m1 < m2 ? -1 : m1 > m2 ? 1 : 0);
}
/* Sort the halftone order by sample value. */
void
gx_sort_ht_order(gx_ht_bit * recs, uint N)
{
    int i;

    /* Tag each sample with its index, for sorting. */
    for (i = 0; i < N; i++)
        recs[i].offset = i;
    qsort((void *)recs, N, sizeof(*recs), compare_samples);
#ifndef GS_THREADSAFE
#ifdef DEBUG
    if (gs_debug_c('H')) {
        uint i;

        dlputs("[H]Sorted samples:\n");
        for (i = 0; i < N; i++)
            dlprintf3("%5u: %5u: %u\n",
                      i, recs[i].offset, recs[i].mask);
    }
#endif
#endif
}

/*
 * Construct the halftone order from a sampled spot function.  Only width x
 * strip samples have been filled in; we must replicate the resulting sorted
 * order vertically, shifting it by shift each time.  See gxdht.h regarding
 * the invariants that must be restored.
 */
void
gx_ht_construct_spot_order(gx_ht_order * porder)
{
    uint width = porder->width;
    uint num_levels = porder->num_levels;       /* = width x strip */
    uint strip = num_levels / width;
    gx_ht_bit *bits = (gx_ht_bit *)porder->bit_data;
    uint *levels = porder->levels;
    uint shift = porder->orig_shift;
    uint full_height = porder->full_height;
    uint num_bits = porder->num_bits;
    uint copies = num_bits / (width * strip);
    gx_ht_bit *bp = bits + num_bits - 1;
    uint i;

    gx_sort_ht_order(bits, num_levels);
    if_debug5('h',
              "[h]spot order: num_levels=%u w=%u h=%u strip=%u shift=%u\n",
              num_levels, width, porder->orig_height, strip, shift);
    /* Fill in the levels array, replicating the bits vertically */
    /* if needed. */
    for (i = num_levels; i > 0;) {
        uint offset = bits[--i].offset;
        uint x = offset % width;
        uint hy = offset - x;
        uint k;

        levels[i] = i * copies;
        for (k = 0; k < copies;
             k++, bp--, hy += num_levels, x = (x + width - shift) % width
            )
            bp->offset = hy + x;
    }
    /* If we have a complete halftone, restore the invariant. */
    if (num_bits == width * full_height) {
        porder->height = full_height;
        porder->shift = 0;
    }
    gx_ht_construct_bits(porder);
}

/* Construct a single offset/mask. */
void
gx_ht_construct_bit(gx_ht_bit * bit, int width, int bit_num)
{
    uint padding = bitmap_raster(width) * 8 - width;
    int pix = bit_num;
    ht_mask_t mask;
    byte *pb;

    pix += pix / width * padding;
    bit->offset = (pix >> 3) & -size_of(mask);
    mask = (ht_mask_t) 1 << (~pix & (ht_mask_bits - 1));
    /* Replicate the mask bits. */
    pix = ht_mask_bits - width;
    while ((pix -= width) >= 0)
        mask |= mask >> width;
    /* Store the mask, reversing bytes if necessary. */
    bit->mask = 0;
    for (pb = (byte *) & bit->mask + (sizeof(mask) - 1);
         mask != 0;
         mask >>= 8, pb--
        )
        *pb = (byte) mask;
}

/* Construct offset/masks from the whitening order. */
/* porder->bits[i].offset contains the index of the bit position */
/* that is i'th in the whitening order. */
void
gx_ht_construct_bits(gx_ht_order * porder)
{
    uint i;
    gx_ht_bit *phb;

    for (i = 0, phb = (gx_ht_bit *)porder->bit_data;
         i < porder->num_bits;
         i++, phb++)
        gx_ht_construct_bit(phb, porder->width, phb->offset);
#ifdef DEBUG
    if (gs_debug_c('H')) {
        dmlprintf1(porder->data_memory, "[H]Halftone order bits "PRI_INTPTR":\n", (intptr_t)porder->bit_data);
        for (i = 0, phb = (gx_ht_bit *)porder->bit_data;
             i < porder->num_bits;
             i++, phb++)
            dmlprintf3(porder->data_memory, "%4d: %u:0x%lx\n", i, phb->offset,
                      (ulong) phb->mask);
    }
#endif
}

/* Release a gx_device_halftone by freeing its components. */
/* (Don't free the gx_device_halftone itself.) */
void
gx_ht_order_release(gx_ht_order * porder, gs_memory_t * mem, bool free_cache)
{
    /* "free cache" is a proxy for "differs from default" */
    if (free_cache) {
        if (porder->cache != 0)
            gx_ht_free_cache(mem, porder->cache);
    }
    porder->cache = 0;
    rc_decrement(porder->transfer, "gx_ht_order_release(transfer)");
    porder->transfer = 0;
    if (porder->data_memory != 0) {
        gs_free_object(porder->data_memory, porder->bit_data,
                       "gx_ht_order_release(bit_data)");
        gs_free_object(porder->data_memory, porder->levels,
                       "gx_ht_order_release(levels)");
        if (porder->threshold != NULL) {
            gs_free_object(porder->data_memory->non_gc_memory, porder->threshold,
                       "gx_ht_order_release(threshold)");
        }
    }
    porder->threshold = 0;
    porder->levels = 0;
    porder->bit_data = 0;
}

void
gx_device_halftone_release(gx_device_halftone * pdht, gs_memory_t * mem)
{
    if (pdht->components) {
        int i;

        /* One of the components might be the same as the default */
        /* order, so check that we don't free it twice. */
        for (i = 0; i < pdht->num_comp; ++i)
            if (pdht->components[i].corder.bit_data !=
                pdht->order.bit_data
                ) {             /* Currently, all orders except the default one */
                /* own their caches. */
                gx_ht_order_release(&pdht->components[i].corder, mem, true);
            }
        gs_free_object(mem, pdht->components,
                       "gx_dev_ht_release(components)");
        pdht->components = 0;
        pdht->num_comp = 0;
    }
    gx_ht_order_release(&pdht->order, mem, false);
}

/*
 * This routine will take a color name (defined by a ptr and size) and
 * check if this is a valid colorant name for the current device.  If
 * so then the device's colorant number is returned.
 *
 * Two other checks are also made.  If the name is "Default" then a value
 * of GX_DEVICE_COLOR_MAX_COMPONENTS is returned.  This is done to
 * simplify the handling of default halftones.  Note:  The device also
 * uses GX_DEVICE_COLOR_MAX_COMPONENTS to indicate colorants which are
 * known but not being used due to the SeparationOrder parameter.  In this
 * case we return -1 since the colorant is not currently being used by the
 * device.
 *
 * If the halftone type is colorscreen or multiple colorscreen, then we
 * also check for Red/Cyan, Green/Magenta, Blue/Yellow, and Gray/Black
 * component name pairs.  This is done since the setcolorscreen and
 * sethalftone types 2 and 4 imply the dual name sets.
 *
 * A negative value is returned if the color name is not found.
 */
int
gs_color_name_component_number(gx_device * dev, const char * pname,
                                int name_size, int halftonetype)
{
    int num_colorant;

#define check_colorant_name(dev, name) \
    ((*dev_proc(dev, get_color_comp_index)) (dev, name, strlen(name), NO_COMP_NAME_TYPE))

#define check_colorant_name_length(dev, name, length) \
    ((*dev_proc(dev, get_color_comp_index)) (dev, name, length, NO_COMP_NAME_TYPE))

#define check_name(str, pname, length) \
    ((strlen(str) == length) && (strncmp(pname, str, length) == 0))

    /*
     * Check if this is a device colorant.
     */
    num_colorant = check_colorant_name_length(dev, pname, name_size);
    if (num_colorant >= 0) {
        /*
         * The device will return GX_DEVICE_COLOR_MAX_COMPONENTS if the
         * colorant is logically present in the device but not being used
         * because a SeparationOrder parameter is specified.  Since we are
         * using this value to indicate 'Default', we use -1 to indicate
         * that the colorant is not really being used.
         */
        if (num_colorant == GX_DEVICE_COLOR_MAX_COMPONENTS)
            num_colorant = -1;
        return num_colorant;
    }

    /*
     * Check if this is the default component
     */
    if (check_name("Default", pname, name_size))
        return GX_DEVICE_COLOR_MAX_COMPONENTS;

    /* Halftones set by setcolorscreen, and (we think) */
    /* Type 2 and Type 4 halftones, are supposed to work */
    /* for both RGB and CMYK, so we need a special check here. */
    if (halftonetype == ht_type_colorscreen ||
        halftonetype == ht_type_multiple_colorscreen) {
        if (check_name("Red", pname, name_size))
            num_colorant = check_colorant_name(dev, "Cyan");
        else if (check_name("Green", pname, name_size))
            num_colorant = check_colorant_name(dev, "Magenta");
        else if (check_name("Blue", pname, name_size))
            num_colorant = check_colorant_name(dev, "Yellow");
        else if (check_name("Gray", pname, name_size))
            num_colorant = check_colorant_name(dev, "Black");
        /*
         * The device will return GX_DEVICE_COLOR_MAX_COMPONENTS if the
         * colorant is logically present in the device but not being used
         * because a SeparationOrder parameter is specified.  Since we are
         * using this value to indicate 'Default', we use -1 to indicate
         * that the colorant is not really being used.
         */
        if (num_colorant == GX_DEVICE_COLOR_MAX_COMPONENTS)
            num_colorant = -1;

#undef check_colorant_name
#undef check_colorant_name_length
#undef check_name

    }
    return num_colorant;
}

/*
 * See gs_color_name_component_number for main description.
 *
 * This version converts a name index value into a string and size and
 * then call gs_color_name_component_number.
 */
int
gs_cname_to_colorant_number(gs_gstate * pgs, byte * pname, uint name_size,
                int halftonetype)
{
    gx_device * dev = pgs->device;

    return gs_color_name_component_number(dev, (char *)pname, name_size,
                    halftonetype);
}

/*
 * Install a device halftone into the gs_gstate.
 *
 * To allow halftones to be shared between graphic states, the
 * gs_gstate contains a pointer to a device halftone structure. Thus, when
 * we say a halftone is "in" the gs_gstate, we are only claiming
 * that the halftone pointer in the gs_gstate points to that halftone.
 *
 * Though the operand halftone uses the same structure as the halftone
 * "in" the gs_gstate, not all of its fields are filled in, and the
 * organization of components differs. Specifically, the following fields
 * are not filled in:
 *
 *  rc          The operand device halftone has only a transient existence,
 *              its reference count information is not initialized. In many
 *              cases, the operand device halftone structure is allocated
 *              on the stack by clients.
 *
 *  id          A halftone is not considered to have an identity until it
 *              is installed in the gs_gstate. This is a design error
 *              which reflects the PostScript origins of this code. In
 *              PostScript, it is impossible to check if two halftone
 *              specifications (sets of operands to setscreen/setcolorscreen
 *              or halftone dictionaries) are the same. Hence, the only way
 *              a halftone could be identified was by the graphic state in
 *              which it was included. In PCL it is possible to directly
 *              identify a halftone specification, but currently there is
 *              no way to use this knowledge in the graphic library.
 *
 *              (An altogether more reasonable approach would be to apply
 *              id's to halftone orders.)
 *
 *  type        This is filled in by the type operand. It is used by
 *              PostScript's currentscreen/currentcolorscreen operators to
 *              determine if a sampling procedure or a halftone dictionary
 *              should be pushed onto the stack. More importantly, it is
 *              also used to determine if specific halftone components can
 *              be used for either the additive or subtractive version of
 *              that component in the process color model. For example, a
 *              RedThreshold in a HalftoneType 4 dictionary can be applied
 *              to either the component "Red" or the component "Cyan", but
 *              the value of the key "Red" in a HalftoneType 5 dictionary
 *              can only be used for a "Red" component (not a "Cyan"
 *              component).
 *
 *  num_comp    For the operand halftone, this is the number of halftone
 *              components included in the specification. For the device
 *              halftone in the gs_gstate, this is always the same as
 *              the number of color model components (see num_dev_comp).
 *
 *  num_dev_comp The number of components in the device process color model
 *              when the operand halftone was created.  With some compositor
 *              devices (for example PDF 1.4) we can have differences in the
 *              process color model of the compositor versus the output device.
 *              These compositor devices do not halftone.
 *
 *  components  For the operand halftone, this field is non-null only if
 *              multiple halftones are provided. In that case, the size
 *              of the array pointed is the same as the number of
 *              components provided. One of these components will usually
 *              be the same as that identified by the "order" field.
 *
 *              For the device halftone in the gs_gstate, this field is
 *              always non-null, and the size of the array pointed to will
 *              be the same as the number of components in the process
 *              color model.
 *
 *  lcm_width,  These fields provide the least common multiple of the
 *  lcm_height  halftone dimensions of the individual component orders.
 *              They represent the dimensions of the smallest tile that
 *              repeats for all color components (this is of interest
 *              because Ghostscript uses a "chunky" raster format for all
 *              drawing procedures). These fields cannot be set in the
 *              operand device halftone as we do not yet know which of
 *              the halftone components will actually be used.
 *
 * Conversely, the "order" field is significant only in the operand device
 * halftone. There it represents the default halftone component, which will
 * be used for all device color components for which a named halftone is
 * not available. It is ignored (filled with 0's) in the device halftone
 * in the gs_gstate.
 *
 * The ordering of entries and the set of fields initialized in the
 * components array also vary between the operand device halftone and
 * the device halftone in the gs_gstate.
 *
 * If the components array is present in the operand device halftone, the
 * cname field in each entry of the array will contain a name index
 * identifying the colorant name, and the comp_number field will provide the
 * index of the corresponding component in the process color model. The
 * order of entries in the components array is essentially arbitrary,
 * but in some common cases will reflect the order in which the halftone
 * specification is provided. By convention, if no explicit default order
 * is provided (i.e.: via a HalftoneType 5 dictionary), the first
 * entry of the array will be the same as the "order" (default) field.
 *
 * For the device halftone in the gs_gstate, the components array is
 * always present, but the cname and comp_number fields of individual
 * entries are ignored. The order of the entries in the array always
 * matches the order of components in the device color model.
 *
 * The distinction between the operand device halftone and the one in
 * the graphic state extends even to the individual fields of the
 * gx_ht_order structure incorporated in the order field of the halftone
 * and the corder field of the elements of the components array. The
 * fields of this structure that are handled differently in the operand
 * and gs_gstate device halftones are:
 *
 *  params          Provides a set of parameters that are required for
 *                  converting a halftone specification to a single
 *                  component order. This field is used only in the
 *                  operand device halftone; it is not set in the device
 *                  halftone in the gs_gstate.
 *
 *  orig_height,   The height and shift values of the halftone cell,
 *  orig_shift     prior to any replication. These fields are currently
 *                 unused, and will always be the same as the height
 *                 and width fields in the device halftone in the
 *                 gs_gstate.
 *
 *  full_height    The height of the smallest replicated tile whose shift
 *                 value is 0. This is calculated as part of the
 *                 installation process; it may be set in the operand
 *                 device halftone, but its value is ignored.
 *
 *
 *  data_memory    Points to the memory structure used to allocate the
 *                 levels and bit_data arrays. The handling of this field
 *                 is a bit complicated. For orders that are "taken over"
 *                 by the installation process, this field will have the
 *                 same value in the operand device halftone and the
 *                 device halftone in the gs_gstate. For halftones
 *                 that are copied by the installation process, this
 *                 field will have the same value as the memory field in
 *                 the gs_gstate (the two are usually the same).
 *
 *  cache          Pointer to a cache of tiles representing various
 *                 levels of the halftone. This may or may not be
 *                 provided in the operand device halftone (in principle
 *                 this should always be a null pointer in the operand
 *                 device halftone, but this is not the manner in which
 *                 the cache was handled historically).
 *
 *  screen_params  This structure contains transformation information
 *                 that is required when reading the sample data for a
 *                 screen. It is no longer required once the halftone
 *                 order has been constructed.
 *
 * In addition to what is noted above, this procedure is made somewhat
 * more complex than expected due to memory management considerations. To
 * clarify this, it is necessary to consider the properties of the pieces
 * that constitute a device halftone.
 *
 *  The gx_device_halftone structure itself is shareable and uses
 *  reference counts.
 *
 *  The gx_ht_order_component array (components array entry) is in
 *  principle shareable, though it does not provide any reference
 *  counting mechanism. Hence any sharing needs to be done with
 *  caution.
 *
 *  Individual component orders are not shareable, as they are part of
 *  the gx_ht_order_commponent structure (a major design error).
 *
 *  The levels, bit_data, and cache structures referenced by the
 *  gx_ht_order structure are in principle shareable, but they also do
 *  not provide any reference counting mechanism. Traditionally, one set
 *  of two component orders could share these structures, using the
 *  halftone's "order" field and various scattered bits of special case
 *  code. This practice has been ended because it did not extend to
 *  sharing amongst more than two components.
 *
 *  The gx_transfer_map structure referenced by the gx_ht_order structure
 *  is shareable, and uses reference counts. Traditionally this structure
 *  was not shared, but this is no longer the case.
 *
 * As noted, the rc field of the operand halftone is not initialized, so
 * this procedure cannot simply take ownership of the operand device
 * halftone structure (i.e.: an ostensibly shareable structure is not
 * shareable). Hence, this procedure will always create a new copy of the
 * gx_device_halftone structure, either by allocating a new structure or
 * re-using the structure already referenced by the gs_gstate. This
 * feature must be retained, as in several cases the calling code will
 * allocate the operand device halftone structure on the stack.
 *
 * Traditionally, this procedure took ownership of all of the structures
 * referenced by the operand device halftone structure. This implied
 * that all structures referenced by the gx_device_halftone structure
 * needed to be allocated on the heap, and should not be released once
 * the call to gx_gstate_dev_ht_install completes.
 *
 * There were two problems with this approach:
 *
 *  1. In the event of an error, the calling code most likely would have
 *     to release referenced components, as the gs_gstate had not yet
 *     take ownership of them. In many cases, the code did not do this.
 *
 *  2. When the structures referenced by a single order needed to be
 *     shared amongst more than one component, there was no easy way to
 *     discover this sharing when the gs_gstate's device halftone
 *     subsequently needed to be released. Hence, objects would be
 *     released multiple times.
 *
 * Subsequently, the code in this routine was changed to copy most of
 * the referenced structures (everything except the transfer functions).
 * Unfortunately, the calling code was not changed, which caused memory
 * leaks.
 *
 * The approach now taken uses a mixture of the two approaches.
 * Ownership to structures referenced by the operand device halftone is
 * assumed by the device halftone in the gs_gstate where this is
 * possible. In these cases, the corresponding references are removed in
 * the operand device halftone (hence, this operand is no longer
 * qualified as const). When a structure is required but ownership cannot
 * be assumed, a copy is made and the reference in the operand device
 * halftone is left undisturbed. The calling code has also been modified
 * to release any remaining referenced structures when this routine
 * returns, whether or not an error is indicated.
 */
int
gx_gstate_dev_ht_install(
    gs_gstate *       pgs,
    gx_device_halftone *    pdht,
    gs_halftone_type        type,
    const gx_device *       dev )
{
    gx_device_halftone      dht;
    int                     num_comps = pdht->num_dev_comp;
    int                     i, code = 0;
    bool                    used_default = false;
    int                     lcm_width = 1, lcm_height = 1;
    bool                    mem_diff = pdht->rc.memory != pgs->memory;
    uint w, h;
    int dw, dh;

    /* construct the new device halftone structure */
    memset(&dht.order, 0, sizeof(dht.order));
    /* the rc field is filled in later */
    dht.id = gs_next_ids(pgs->memory, 1);
    dht.type = type;
    dht.components =  gs_alloc_struct_array(
                          pgs->memory,
                          num_comps,
                          gx_ht_order_component,
                          &st_ht_order_component_element,
                          "gx_gstate_dev_ht_install(components)" );
    if (dht.components == NULL)
        return_error(gs_error_VMerror);
    dht.num_comp = dht.num_dev_comp = num_comps;
    /* lcm_width, lcm_height are filled in later */

    /* initialize the components array */
    memset(dht.components, 0, num_comps * sizeof(dht.components[0]));
    for (i = 0; i < num_comps; i++)
        dht.components[i].comp_number = -1;

    /*
     * Duplicate any of the non-default components, but do not create copies
     * of the levels or bit_data arrays. If all goes according to plan, the
     * gs_gstate's device halftone will assume ownership of these arrays
     * by clearing the corresponding pointers in the operand halftone's
     * orders.
     */
    if (pdht->components != 0) {
        int     input_ncomps = pdht->num_comp;

        for (i = 0; i < input_ncomps && code >= 0; i++) {
            gx_ht_order_component * p_s_comp = &pdht->components[i];
            gx_ht_order *           p_s_order = &p_s_comp->corder;
            int                     comp_num = p_s_comp->comp_number;

            if (comp_num >= 0 && comp_num < GX_DEVICE_COLOR_MAX_COMPONENTS &&
                comp_num < dht.num_comp) {
                gx_ht_order *   p_d_order = &dht.components[comp_num].corder;

                /* indicate that this order has been filled in */
                dht.components[comp_num].comp_number = comp_num;

                /*
                 * The component can be used only if it is from the
                 * proper memory
                 */
                if (mem_diff)
                    code = gx_ht_copy_ht_order( p_d_order,
                                                p_s_order,
                                                pgs->memory );
                else {
                    /* check if this is also the default component */
                    used_default = used_default ||
                                   p_s_order->bit_data == pdht->order.bit_data;

                    gx_ht_move_ht_order(p_d_order, p_s_order);
                }
            }
        }
    }

    /*
     * Copy the default order to any remaining components.
     */

    for (i = 0; i < num_comps && code >= 0; i++) {
        gx_ht_order *porder = &dht.components[i].corder;

        if (dht.components[i].comp_number != i) {
            if (used_default || mem_diff)
                code = gx_ht_copy_ht_order(porder, &pdht->order, pgs->memory);
            else {
                gx_ht_move_ht_order(porder, &pdht->order);
                used_default = true;
            }
            dht.components[i].comp_number = i;
        }

        w = porder->width;
        h = porder->full_height;
        dw = igcd(lcm_width, w);
        dh = igcd(lcm_height, h);

        lcm_width /= dw;
        lcm_height /= dh;
        lcm_width = (w > max_int / lcm_width ? max_int : lcm_width * w);
        lcm_height = (h > max_int / lcm_height ? max_int : lcm_height * h);

        if (porder->cache == 0) {
            uint            tile_bytes, num_tiles, slots_wanted, rep_raster, rep_count;
            gx_ht_cache *   pcache;

            tile_bytes = porder->raster
                          * (porder->num_bits / porder->width);
            num_tiles = 1 + gx_ht_cache_default_bits_size() / tile_bytes;
            /*
             * Limit num_tiles to a reasonable number allowing for width repition.
             * The most we need is one cache slot per bit.
             * This prevents allocations of large cache bits that will never
             * be used. See rep_count limit in gxht.c
             */
            slots_wanted = 1 + ( porder->width * porder->height );
            rep_raster = ((num_tiles*tile_bytes) / porder->height /
                            slots_wanted) & ~(align_bitmap_mod - 1);
            rep_count = rep_raster * 8 / porder->width;
            if (rep_count > sizeof(ulong) * 8 && (num_tiles >
                    1 + ((num_tiles * 8 * sizeof(ulong)) / rep_count) ))
                num_tiles = 1 + ((num_tiles * 8 * sizeof(ulong)) / rep_count);
            pcache = gx_ht_alloc_cache( pgs->memory, num_tiles,
                                        tile_bytes * num_tiles );
            if (pcache == NULL)
                code = gs_error_VMerror;
            else {
                porder->cache = pcache;
                gx_ht_init_cache(pgs->memory, pcache, porder);
            }
        }
    }
    dht.lcm_width = lcm_width;
    dht.lcm_height = lcm_height;

    /*
     * If everything is OK so far, allocate a unique copy of the device
     * halftone reference by the gs_gstate.
     *
     * This code requires a special check for the case in which the
     * deivce halftone referenced by the gs_gstate is already unique.
     * In this case, we must explicitly release just the components array
     * (and any structures it refers to) of the existing halftone. This
     * cannot be done automatically, as the rc_unshare_struct macro only
     * ensures that a unique instance of the top-level structure is
     * created, not that any substructure references are updated.
     *
     * Though this is scheduled to be changed, for the time being the
     * command list renderer may invoke this code with pdht == psi->dev_ht
     * (in which case we know pgs->dev_ht.rc.ref_count == 1). Special
     * handling is required in that case, to avoid releasing structures
     * we still need.
     */
    if (code >= 0) {
        gx_device_halftone *    pgsdht = pgs->dev_ht;
        rc_header               tmp_rc;

        if (pgsdht != 0 && pgsdht->rc.ref_count == 1) {
            if (pdht != pgsdht)
                gx_device_halftone_release(pgsdht, pgsdht->rc.memory);
        } else {
            rc_unshare_struct( pgs->dev_ht,
                               gx_device_halftone,
                               &st_device_halftone,
                               pgs->memory,
                               BEGIN code = gs_error_VMerror; goto err; END,
                               "gx_gstate_dev_ht_install" );
            pgsdht = pgs->dev_ht;
        }

        /*
         * Everything worked. "Assume ownership" of the appropriate
         * portions of the source device halftone by clearing the
         * associated references.  Since we might have
         * pdht == pgs->dev_ht, this must done before updating pgs->dev_ht.
         *
         * If the default order has been used for a device component, and
         * any of the source component orders share their levels or bit_data
         * arrays with the default order, clear the pointers in those orders
         * now. This is necessary because the default order's pointers will
         * be cleared immediately below, so subsequently it will not be
         * possible to tell if that this information is being shared.
         */
        if (pdht->components != 0) {
            int     input_ncomps = pdht->num_comp;

            for (i = 0; i < input_ncomps; i++) {
                gx_ht_order_component * p_s_comp = &pdht->components[i];
                gx_ht_order *           p_s_order = &p_s_comp->corder;
                int                     comp_num = p_s_comp->comp_number;

                if ( comp_num >= 0                            &&
                     comp_num < GX_DEVICE_COLOR_MAX_COMPONENTS  ) {
                    memset(p_s_order, 0, sizeof(*p_s_order));
                } else if ( comp_num == GX_DEVICE_COLOR_MAX_COMPONENTS &&
                            used_default                                 )
                    memset(p_s_order, 0, sizeof(*p_s_order));
            }
        }
        if (used_default) {
            memset(&pdht->order, 0, sizeof(pdht->order));
        }

        tmp_rc = pgsdht->rc;
        *pgsdht = dht;
        pgsdht->rc = tmp_rc;

        /* update the effective transfer function array */
        gx_gstate_set_effective_xfer(pgs);

        return 0;
    }

    /* something went amiss; release all copied components */
  err:
    for (i = 0; i < num_comps; i++) {
        gx_ht_order_component * pcomp = &dht.components[i];
        gx_ht_order *           porder = &pcomp->corder;

        if (pcomp->comp_number == -1) {
            gx_ht_order_release(porder, pgs->memory, true);
        }
        else if (porder->cache != NULL) {
            gx_ht_free_cache(pgs->memory, porder->cache);
            porder->cache = NULL;
        }
    }
    gs_free_object(pgs->memory, dht.components, "gx_gstate_dev_ht_install");

    return code;
}

/*
 * Install a new halftone in the graphics state.  Note that we copy the top
 * level of the gs_halftone and the gx_device_halftone, and take ownership
 * of any substructures.
 */
int
gx_ht_install(gs_gstate * pgs, const gs_halftone * pht,
              gx_device_halftone * pdht)
{
    gs_memory_t *mem = pht->rc.memory;
    gs_halftone *old_ht = pgs->halftone;
    gs_halftone *new_ht;
    int code;

    pdht->num_dev_comp = pgs->device->color_info.num_components;
    if (old_ht != 0 && old_ht->rc.memory == mem &&
        old_ht->rc.ref_count == 1
        )
        new_ht = old_ht;
    else
        rc_alloc_struct_1(new_ht, gs_halftone, &st_halftone,
                          mem, return_error(gs_error_VMerror),
                          "gx_ht_install(new halftone)");
    code = gx_gstate_dev_ht_install(pgs,
                             pdht, pht->type, gs_currentdevice_inline(pgs));
    if (code < 0) {
        if (new_ht != old_ht)
            gs_free_object(mem, new_ht, "gx_ht_install(new halftone)");
        return code;
    }

    /*
     * Discard any unused components and the components array of the
     * operand device halftone
     */
    gx_device_halftone_release(pdht, pdht->rc.memory);

    if (new_ht != old_ht)
        rc_decrement(old_ht, "gx_ht_install(old halftone)");
    {
        rc_header rc;

        rc = new_ht->rc;
        *new_ht = *pht;
        new_ht->rc = rc;
    }
    pgs->halftone = new_ht;
    gx_unset_both_dev_colors(pgs);
    return 0;
}

/*
 * This macro will determine the colorant number of a given color name.
 * A value of -1 indicates that the name is not valid.
 */
#define check_colorant_name(name, dev) \
   ((*dev_proc(dev, get_color_comp_index)) (dev, name, strlen(name), NO_NAME_TYPE))

/* Reestablish the effective transfer functions, taking into account */
/* any overrides from halftone dictionaries. */
void
gx_gstate_set_effective_xfer(gs_gstate * pgs)
{
    gx_device_halftone *pdht = pgs->dev_ht;
    gx_transfer_map *pmap;
    gx_ht_order *porder;
    int i, component_num, non_id_count;

    non_id_count = (pgs->set_transfer.gray->proc == &gs_identity_transfer) ? 0 : GX_DEVICE_COLOR_MAX_COMPONENTS;
    for (i = 0; i < GX_DEVICE_COLOR_MAX_COMPONENTS; i++)
        pgs->effective_transfer[i] = pgs->set_transfer.gray;    /* default */

    /* Check if we have a transfer functions from setcolortransfer */
    if (pgs->set_transfer.red) {
        component_num = pgs->set_transfer.red_component_num;
        if (component_num >= 0) {
            if (pgs->effective_transfer[component_num]->proc != &gs_identity_transfer)
               non_id_count--;
            pgs->effective_transfer[component_num] = pgs->set_transfer.red;
            if (pgs->effective_transfer[component_num]->proc != &gs_identity_transfer)
               non_id_count++;
        }
    }
    if (pgs->set_transfer.green) {
        component_num = pgs->set_transfer.green_component_num;
        if (component_num >= 0) {
            if (pgs->effective_transfer[component_num]->proc != &gs_identity_transfer)
               non_id_count--;
            pgs->effective_transfer[component_num] = pgs->set_transfer.green;
            if (pgs->effective_transfer[component_num]->proc != &gs_identity_transfer)
               non_id_count++;
        }
    }
    if (pgs->set_transfer.blue) {
        component_num = pgs->set_transfer.blue_component_num;
        if (component_num >= 0) {
            if (pgs->effective_transfer[component_num]->proc != &gs_identity_transfer)
               non_id_count--;
            pgs->effective_transfer[component_num] = pgs->set_transfer.blue;
            if (pgs->effective_transfer[component_num]->proc != &gs_identity_transfer)
               non_id_count++;
        }
    }

    if (pdht) { /* might not be initialized yet */

        /* Since the transfer function is pickled into the threshold array (if any)*/
        /*  we need to free it so it can be reconstructed with the current transfer */
        porder = &(pdht->order);
        if (porder->threshold != NULL) {
            gs_free_object(porder->data_memory->non_gc_memory, porder->threshold,
                           "set_effective_transfer(threshold)");
            porder->threshold = 0;
        }
        for (i = 0; i < pdht->num_comp; i++) {
            pmap = pdht->components[i].corder.transfer;
            if (pmap != NULL) {
                if (pgs->effective_transfer[i]->proc != &gs_identity_transfer)
                    non_id_count--;
                pgs->effective_transfer[i] = pmap;
                if (pgs->effective_transfer[i]->proc != &gs_identity_transfer)
                   non_id_count++;
            }
            porder = &(pdht->components[i].corder);
            if (porder->threshold != NULL) {
                gs_free_object(porder->data_memory->non_gc_memory, porder->threshold,
                               "set_effective_transfer(threshold)");
                porder->threshold = 0;
            }
        }
    }

    pgs->effective_transfer_non_identity_count = non_id_count;
}

void
gx_set_effective_transfer(gs_gstate * pgs)
{
    gx_gstate_set_effective_xfer(pgs);
}

/* Check if the transfer function for a component is monotonic.	*/
/* Used to determine if we can do fast halftoning		*/
bool
gx_transfer_is_monotonic(const gs_gstate *pgs, int plane_index)
{
    if (pgs->effective_transfer[plane_index]->proc != gs_identity_transfer) {
        bool threshold_inverted;
        int t_level;
        frac mapped, prev;

        prev = gx_map_color_frac(pgs, frac_0, effective_transfer[plane_index]);
        threshold_inverted = prev >
                             gx_map_color_frac(pgs, frac_1, effective_transfer[plane_index]);
        for (t_level = 1; t_level < 255; t_level++) {
            mapped = gx_map_color_frac(pgs, byte2frac(t_level), effective_transfer[plane_index]);
            if ((threshold_inverted && mapped > prev) ||
                (!threshold_inverted && mapped < prev))
                return false;
            prev = mapped;
        }
    }
    return true;
}

/* This creates a threshold array from the tiles.  Threshold is allocated in
   non-gc memory and is not known to the GC. The algorithm cycles through the
   threshold values, computing the shade the same way as gx_render_device_DeviceN
   so that the threshold matches the non-threshold halftoning.
*/
int
gx_ht_construct_threshold( gx_ht_order *d_order, gx_device *dev,
                           const gs_gstate * pgs, int plane_index)
{
    int i, j;
    unsigned char *thresh;
    gs_memory_t *memory = d_order ? d_order->data_memory->non_gc_memory : NULL;
    uint max_value;
    unsigned long hsize, nshades;
    int t_level;
    int row, col;
    int code;
    int num_repeat, shift, num_levels = d_order ? d_order->num_levels : 0;
    int row_kk, col_kk, kk;
    frac t_level_frac_color;
    int shade, base_shade = 0;
    bool have_transfer = false, threshold_inverted = false;

    if (d_order == NULL) return -1;
    /* We can have simple or complete orders.  Simple ones tile the threshold
       with shifts.   To handle those we simply loop over the number of
       repeats making sure to shift columns when we set our threshold values */
    num_repeat = d_order->full_height / d_order->height;
    shift = d_order->shift;

    if (d_order->threshold != NULL) return 0;
    thresh = (byte *)gs_malloc(memory, (size_t)d_order->width * d_order->full_height, 1,
                              "gx_ht_construct_threshold");
    if (thresh == NULL) {
        return -1 ;         /* error if allocation failed   */
    }
    /* Check if we need to apply a transfer function to the values */
    if (pgs->effective_transfer[plane_index]->proc != gs_identity_transfer) {
        have_transfer = true;
        threshold_inverted = gx_map_color_frac(pgs, frac_0, effective_transfer[plane_index]) >
                                gx_map_color_frac(pgs, frac_1, effective_transfer[plane_index]);
    }
    /* Adjustments to ensure that we properly map our 256 levels into
      the number of shades that we have in our halftone screen.  For example
      if we have a 16x16 screen, we have 257 shadings that we can represent
      if we have a  2x2  screen, we have 5 shadings that we can represent.
      Calculations are performed to match what happens in the tile filling
      code */
    max_value = (dev->color_info.gray_index == plane_index) ?
         dev->color_info.dither_grays - 1 :
         dev->color_info.dither_colors - 1;
    hsize = num_levels;
    nshades = hsize * max_value + 1;

    /* search upwards to find the correct value for the last threshold value */
    /* Use this to initialize the threshold array (transition to all white) */
    t_level = 0;
    do {
        t_level++;
        t_level_frac_color = byte2frac(threshold_inverted ? 255 - t_level : t_level);
        if (have_transfer)
            t_level_frac_color = gx_map_color_frac(pgs, t_level_frac_color, effective_transfer[plane_index]);
        shade = t_level_frac_color * nshades / (frac_1_long + 1);
    } while (shade < num_levels && t_level < 255);
    /* Initialize the thresholds to the lowest level that will be all white */
    for( i = 0; i < d_order->width * d_order->full_height; i++ ) {
        thresh[i] = t_level;
    }
    for (t_level = 1; t_level < 256; t_level++) {
        t_level_frac_color = byte2frac(threshold_inverted ? 255 - t_level : t_level);
        if (have_transfer)
            t_level_frac_color = gx_map_color_frac(pgs, t_level_frac_color, effective_transfer[plane_index]);
        shade = t_level_frac_color * nshades / (frac_1_long + 1);
        if (shade < num_levels && shade > base_shade) {
            if (d_order->levels[shade] > d_order->levels[base_shade]) {
                /* Loop over the number of dots that we have to set in going
                   to this new shade from the old shade */
                for (j = d_order->levels[base_shade]; j < d_order->levels[shade]; j++) {
                    gs_int_point ppt;
                    code = d_order->procs->bit_index(d_order, j, &ppt);
                    if (code < 0)
                        return code;
                    row = ppt.y;
                    col = ppt.x;
                    if( col < (int)d_order->width ) {
                        for (kk = 0; kk < num_repeat; kk++) {
                            row_kk = row + kk * d_order->height;
                            col_kk = col + kk * shift;
                            col_kk = col_kk % d_order->width;
                            *(thresh + col_kk + (row_kk * d_order->width)) = t_level;
                        }
                    }
                }
            }
            base_shade = shade;
        }
    }
    d_order->threshold = thresh;
    d_order->threshold_inverted = threshold_inverted;
    if (dev->color_info.polarity == GX_CINFO_POLARITY_SUBTRACTIVE) {
        for(i = 0; i < (int)d_order->height; i++ ) {
            for( j=(int)d_order->width-1; j>=0; j-- )
                *(thresh+j+(i*d_order->width)) = 255 - *(thresh+j+(i*d_order->width));
        }
    }
#ifdef DEBUG
    if ( gs_debug_c('h') ) {
         for( i=0; i<(int)d_order->height; i++ ) {
            dmprintf1(memory, "threshold array row %3d= ", i);
            for( j=0; j<(int)(d_order->width); j++ )
                dmprintf1(memory, "%3d ", *(thresh+j+(i*d_order->width)) );
            dmprintf(memory, "\n");
        }
   }
#endif
    return 0;
}