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
use super::*;
use foreign_types::ForeignTypeRef;

unsafe fn cast<T>(ptr: *const u8) -> &'static T {
    assert!(0 == ptr.align_offset(std::mem::align_of::<T>()), "Tag data pointer must be aligned");
    &*(ptr as *const T)
}

/// from_ptr() methods require mut for no good reason
unsafe fn aligned_mut<T>(ptr: *const u8) -> *mut T {
    assert!(0 == ptr.align_offset(std::mem::align_of::<T>()), "Tag data pointer must be aligned");
    ptr as *mut T
}

impl<'a> Tag<'a> {
    pub fn is_none(&self) -> bool {
        match *self {
            Tag::None => true,
            _ => false,
        }
    }

    pub unsafe fn data_for_signature(&self, sig: TagSignature) -> *const u8 {
        use crate::TagSignature::*;
        match (sig, self) {
            (RedColorantTag, &Tag::CIEXYZ(data)) |
            (BlueColorantTag, &Tag::CIEXYZ(data)) |
            (GreenColorantTag, &Tag::CIEXYZ(data)) |
            (LuminanceTag, &Tag::CIEXYZ(data)) |
            (MediaBlackPointTag, &Tag::CIEXYZ(data)) |
            (MediaWhitePointTag, &Tag::CIEXYZ(data)) => {
                data as *const _ as *const u8
            },
            (ViewingCondDescTag, &Tag::MLU(data)) |
            (CharTargetTag, &Tag::MLU(data)) |
            (CopyrightTag, &Tag::MLU(data)) |
            (DeviceMfgDescTag, &Tag::MLU(data)) |
            (DeviceModelDescTag, &Tag::MLU(data)) |
            (ProfileDescriptionTag, &Tag::MLU(data)) |
            (ProfileDescriptionMLTag, &Tag::MLU(data)) |
            (ScreeningDescTag, &Tag::MLU(data)) => {
                data.as_ptr() as *const _
            },
            (ChromaticityTag, &Tag::CIExyYTRIPLE(data)) |
            (ChromaticAdaptationTag, &Tag::CIExyYTRIPLE(data)) => {
                data as *const _ as *const u8
            },
            (ColorantTableTag, &Tag::NAMEDCOLORLIST(data)) |
            (ColorantTableOutTag, &Tag::NAMEDCOLORLIST(data)) |
            (CrdInfoTag, &Tag::NAMEDCOLORLIST(data)) |
            (NamedColor2Tag, &Tag::NAMEDCOLORLIST(data)) => data as *const _ as *const u8,
            (DataTag, &Tag::ICCData(data)) |
            (Ps2CRD0Tag, &Tag::ICCData(data)) |
            (Ps2CRD1Tag, &Tag::ICCData(data)) |
            (Ps2CRD2Tag, &Tag::ICCData(data)) |
            (Ps2CRD3Tag, &Tag::ICCData(data)) |
            (Ps2CSATag, &Tag::ICCData(data)) => data as *const _ as *const u8,
            (Ps2RenderingIntentTag, &Tag::ICCData(data)) => data as *const _ as *const u8,
            (AToB0Tag, &Tag::Pipeline(data)) |
            (AToB1Tag, &Tag::Pipeline(data)) |
            (AToB2Tag, &Tag::Pipeline(data)) |
            (BToA0Tag, &Tag::Pipeline(data)) |
            (BToA1Tag, &Tag::Pipeline(data)) |
            (BToA2Tag, &Tag::Pipeline(data)) |
            (DToB0Tag, &Tag::Pipeline(data)) |
            (DToB1Tag, &Tag::Pipeline(data)) |
            (DToB2Tag, &Tag::Pipeline(data)) |
            (DToB3Tag, &Tag::Pipeline(data)) |
            (BToD0Tag, &Tag::Pipeline(data)) |
            (BToD1Tag, &Tag::Pipeline(data)) |
            (BToD2Tag, &Tag::Pipeline(data)) |
            (BToD3Tag, &Tag::Pipeline(data)) |
            (GamutTag, &Tag::Pipeline(data)) |
            (Preview0Tag, &Tag::Pipeline(data)) |
            (Preview1Tag, &Tag::Pipeline(data)) |
            (Preview2Tag, &Tag::Pipeline(data)) => {
                data.as_ptr() as *const _
            },
            (BlueTRCTag, &Tag::ToneCurve(data)) |
            (GrayTRCTag, &Tag::ToneCurve(data)) |
            (GreenTRCTag, &Tag::ToneCurve(data)) |
            (RedTRCTag, &Tag::ToneCurve(data)) => {
                data.as_ptr() as *const _
            },
            (ColorimetricIntentImageStateTag, &Tag::ColorimetricIntentImageState(ref data)) => {
                data as *const ffi::ColorimetricIntentImageState as *const u8
            },
            (PerceptualRenderingIntentGamutTag, &Tag::Intent(ref data)) |
            (SaturationRenderingIntentGamutTag, &Tag::Intent(ref data)) => {
                data as *const ffi::Intent as *const u8
            },
            (TechnologyTag, &Tag::Technology(ref data)) => data as *const _ as *const u8,
            (MeasurementTag, &Tag::ICCMeasurementConditions(data)) => {
                data as *const _ as *const u8
            },
            (ProfileSequenceDescTag, &Tag::SEQ(data)) |
            (ProfileSequenceIdTag, &Tag::SEQ(data)) => {
                data as *const _ as *const u8
            },
            (ScreeningTag, &Tag::Screening(data)) => data as *const _ as *const u8,
            (UcrBgTag, &Tag::UcrBg(data)) => data as *const _ as *const u8,
            (ViewingConditionsTag, &Tag::ICCViewingConditions(data)) => {
                data as *const _ as *const u8
            },
            (sig, _) => panic!("Signature type {:?} does not support this tag data type", sig),
        }
    }

    pub unsafe fn new(sig: TagSignature, data: *const u8) -> Self {
        if data.is_null() {
            return Tag::None;
        }
        use crate::TagSignature::*;
        match sig {
            BlueColorantTag |
            GreenColorantTag |
            LuminanceTag |
            MediaBlackPointTag |
            MediaWhitePointTag |
            RedColorantTag => Tag::CIEXYZ(cast(data)),
            CharTargetTag |
            CopyrightTag |
            DeviceMfgDescTag |
            DeviceModelDescTag |
            ProfileDescriptionTag |
            ProfileDescriptionMLTag |
            ScreeningDescTag |
            ViewingCondDescTag => Tag::MLU(MLURef::from_ptr(aligned_mut(data))),
            ChromaticityTag |
            ChromaticAdaptationTag => Tag::CIExyYTRIPLE(cast(data)),
            ColorantTableTag |
            ColorantTableOutTag |
            CrdInfoTag |
            NamedColor2Tag => Tag::NAMEDCOLORLIST(NamedColorListRef::from_ptr(aligned_mut(data))),
            DataTag |
            Ps2CRD0Tag |
            Ps2CRD1Tag |
            Ps2CRD2Tag |
            Ps2CRD3Tag |
            Ps2CSATag |
            Ps2RenderingIntentTag => Tag::ICCData(cast(data)),
            AToB0Tag |
            AToB1Tag |
            AToB2Tag |
            BToA0Tag |
            BToA1Tag |
            BToA2Tag |
            DToB0Tag |
            DToB1Tag |
            DToB2Tag |
            DToB3Tag |
            BToD0Tag |
            BToD1Tag |
            BToD2Tag |
            BToD3Tag |
            GamutTag |
            Preview0Tag |
            Preview1Tag |
            Preview2Tag => Tag::Pipeline(PipelineRef::from_ptr(aligned_mut(data))),
            BlueTRCTag |
            GrayTRCTag |
            GreenTRCTag |
            RedTRCTag => Tag::ToneCurve(ToneCurveRef::from_ptr(aligned_mut(data))),
            ColorimetricIntentImageStateTag  => {
                Tag::ColorimetricIntentImageState((data as *const ffi::ColorimetricIntentImageState).read_unaligned())
            },
            PerceptualRenderingIntentGamutTag |
            SaturationRenderingIntentGamutTag => {
                Tag::Intent((data as *const ffi::Intent).read_unaligned())
            },
            TechnologyTag => Tag::Technology((data as *const ffi::TechnologySignature).read_unaligned()),
            MeasurementTag => Tag::ICCMeasurementConditions(cast(data)),
            ProfileSequenceDescTag |
            ProfileSequenceIdTag => Tag::SEQ(cast(data)),
            ScreeningTag => Tag::Screening(cast(data)),
            UcrBgTag => Tag::UcrBg(cast(data)),
            ViewingConditionsTag => Tag::ICCViewingConditions(cast(data)),
            _ => Tag::None,
        }
    }
}