summaryrefslogtreecommitdiff
path: root/filter/source/graphicfilter/ipcd/ipcd.cxx
blob: f76ba225def7edd4975e8311917fc7a46c29b57c (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include <rtl/alloc.h>
#include <vcl/graph.hxx>
#include <vcl/BitmapTools.hxx>
#include <vcl/svapp.hxx>
#include <vcl/fltcall.hxx>
#include <svl/solar.hrc>
#include <vcl/FilterConfigItem.hxx>
#include <tools/stream.hxx>

//============================ PCDReader ==================================

// these resolutions are contained in a PCD file:
enum PCDResolution {
    PCDRES_BASE16,  //  192 x  128
    PCDRES_BASE4,   //  384 x  256
    PCDRES_BASE,    //  768 x  512
    // the following ones are compressed
    // and CANNOT be read by us
    PCDRES_4BASE,   // 1536 x 1024
    PCDRES_16BASE   // 3072 x 3072
};

class PCDReader {

private:

    bool bStatus;

    SvStream &m_rPCD;
    std::unique_ptr<vcl::bitmap::RawBitmap> mpBitmap;

    sal_uInt8               nOrientation;   // orientation of the picture within the PCD file:
                                        // 0 - spire point up
                                        // 1 - spire points to the right
                                        // 2 - spire points down
                                        // 3 - spire points to the left

    PCDResolution       eResolution;    // which resolution we want

    sal_uLong               nWidth;         // width of the PCD picture
    sal_uLong               nHeight;        // height of the PCD picture
    sal_uLong               nImagePos;      // position of the picture within the PCD file

    // temporary lLue-Green-Red-Bitmap
    sal_uLong               nBMPWidth;
    sal_uLong               nBMPHeight;

    void    CheckPCDImagePacFile();
        // checks whether it's a Photo-CD file with 'Image Pac'

    void    ReadOrientation();
        // reads the orientation and sets nOrientation

    void    ReadImage();

public:

    explicit PCDReader(SvStream &rStream)
        : bStatus(false)
        , m_rPCD(rStream)
        , nOrientation(0)
        , eResolution(PCDRES_BASE16)
        , nWidth(0)
        , nHeight(0)
        , nImagePos(0)
        , nBMPWidth(0)
        , nBMPHeight(0)
    {
    }

    bool ReadPCD( Graphic & rGraphic, FilterConfigItem* pConfigItem );
};

//=================== Methods of PCDReader ==============================

bool PCDReader::ReadPCD( Graphic & rGraphic, FilterConfigItem* pConfigItem )
{
    bStatus      = true;

    // is it a PCD file with a picture? ( sets bStatus == sal_False, if that's not the case):
    CheckPCDImagePacFile();

    // read orientation of the picture:
    ReadOrientation();

    // which resolution do we want?:
    eResolution = PCDRES_BASE;
    if ( pConfigItem )
    {
        sal_Int32 nResolution = pConfigItem->ReadInt32( "Resolution", 2 );
        if ( nResolution == 1 )
            eResolution = PCDRES_BASE4;
        else if ( nResolution == 0 )
            eResolution = PCDRES_BASE16;
    }
    // determine size and position (position within the PCD file) of the picture:
    switch (eResolution)
    {
        case PCDRES_BASE16 :
            nWidth = 192;
            nHeight = 128;
            nImagePos = 8192;
            break;

        case PCDRES_BASE4 :
            nWidth = 384;
            nHeight = 256;
            nImagePos = 47104;
            break;

        case PCDRES_BASE :
            nWidth = 768;
            nHeight = 512;
            nImagePos = 196608;
            break;

        default:
            bStatus = false;
    }
    if ( bStatus )
    {
        if ( ( nOrientation & 0x01 ) == 0 )
        {
            nBMPWidth = nWidth;
            nBMPHeight = nHeight;
        }
        else
        {
            nBMPWidth = nHeight;
            nBMPHeight = nWidth;
        }
        mpBitmap.reset(new vcl::bitmap::RawBitmap( Size( nBMPWidth, nBMPHeight ), 24 ));

        ReadImage();

        rGraphic = vcl::bitmap::CreateFromData(std::move(*mpBitmap));
    }
    return bStatus;
}


void PCDReader::CheckPCDImagePacFile()
{
    char Buf[ 8 ];

    m_rPCD.Seek( 2048 );
    m_rPCD.ReadBytes(Buf, 7);
    Buf[ 7 ] = 0;
    if (OString(Buf) != "PCD_IPI")
        bStatus = false;
}


void PCDReader::ReadOrientation()
{
    if ( !bStatus )
        return;
    m_rPCD.Seek( 194635 );
    m_rPCD.ReadUChar( nOrientation );
    nOrientation &= 0x03;
}


void PCDReader::ReadImage()
{
    sal_uLong  nx,ny,nW2,nH2,nYPair,ndy,nXPair;
    long   nL,nCb,nCr,nRed,nGreen,nBlue;
    sal_uInt8 * pt;
    sal_uInt8 * pL0; // luminance for each pixel of the 1st row of the current pair of rows
    sal_uInt8 * pL1; // luminance for each pixel of the 2nd row of the current pair of rows
    sal_uInt8 * pCb; // blue chrominance for each 2x2 pixel of the current pair of rows
    sal_uInt8 * pCr; // red chrominance for each 2x2 pixel of the current pair of rows
    sal_uInt8 * pL0N, * pL1N, * pCbN, * pCrN; // like above, but for the next pair of rows

    if ( !bStatus )
        return;

    nW2=nWidth>>1;
    nH2=nHeight>>1;

    pL0 =static_cast<sal_uInt8*>(std::malloc( nWidth ));
    pL1 =static_cast<sal_uInt8*>(std::malloc( nWidth ));
    pCb =static_cast<sal_uInt8*>(std::malloc( nW2+1 ));
    pCr =static_cast<sal_uInt8*>(std::malloc( nW2+1 ));
    pL0N=static_cast<sal_uInt8*>(std::malloc( nWidth ));
    pL1N=static_cast<sal_uInt8*>(std::malloc( nWidth ));
    pCbN=static_cast<sal_uInt8*>(std::malloc( nW2+1 ));
    pCrN=static_cast<sal_uInt8*>(std::malloc( nW2+1 ));

    if ( pL0 == nullptr || pL1 == nullptr || pCb == nullptr || pCr == nullptr ||
        pL0N == nullptr || pL1N == nullptr || pCbN == nullptr || pCrN == nullptr)
    {
        std::free(static_cast<void*>(pL0) );
        std::free(static_cast<void*>(pL1) );
        std::free(static_cast<void*>(pCb) );
        std::free(static_cast<void*>(pCr) );
        std::free(static_cast<void*>(pL0N));
        std::free(static_cast<void*>(pL1N));
        std::free(static_cast<void*>(pCbN));
        std::free(static_cast<void*>(pCrN));
        bStatus = false;
        return;
    }

    m_rPCD.Seek( nImagePos );

    // next pair of rows := first pair of rows:
    m_rPCD.ReadBytes( pL0N, nWidth );
    m_rPCD.ReadBytes( pL1N, nWidth );
    m_rPCD.ReadBytes( pCbN, nW2 );
    m_rPCD.ReadBytes( pCrN, nW2 );
    pCbN[ nW2 ] = pCbN[ nW2 - 1 ];
    pCrN[ nW2 ] = pCrN[ nW2 - 1 ];

    for ( nYPair = 0; nYPair < nH2; nYPair++ )
    {
        // current pair of rows := next pair of rows:
        pt=pL0; pL0=pL0N; pL0N=pt;
        pt=pL1; pL1=pL1N; pL1N=pt;
        pt=pCb; pCb=pCbN; pCbN=pt;
        pt=pCr; pCr=pCrN; pCrN=pt;

        // get the next pair of rows:
        if ( nYPair < nH2 - 1 )
        {
            m_rPCD.ReadBytes( pL0N, nWidth );
            m_rPCD.ReadBytes( pL1N, nWidth );
            m_rPCD.ReadBytes( pCbN, nW2 );
            m_rPCD.ReadBytes( pCrN, nW2 );
            pCbN[nW2]=pCbN[ nW2 - 1 ];
            pCrN[nW2]=pCrN[ nW2 - 1 ];
        }
        else
        {
            for ( nXPair = 0; nXPair < nW2; nXPair++ )
            {
                pCbN[ nXPair ] = pCb[ nXPair ];
                pCrN[ nXPair ] = pCr[ nXPair ];
            }
        }

        // loop through both rows of the pair of rows:
        for ( ndy = 0; ndy < 2; ndy++ )
        {
            ny = ( nYPair << 1 ) + ndy;

            // loop through X:
            for ( nx = 0; nx < nWidth; nx++ )
            {
                // get/calculate nL,nCb,nCr for the pixel nx,ny:
                nXPair = nx >> 1;
                if ( ndy == 0 )
                {
                    nL = static_cast<long>(pL0[ nx ]);
                    if (( nx & 1 ) == 0 )
                    {
                        nCb = static_cast<long>(pCb[ nXPair ]);
                        nCr = static_cast<long>(pCr[ nXPair ]);
                    }
                    else
                    {
                        nCb = ( static_cast<long>(pCb[ nXPair ]) + static_cast<long>(pCb[ nXPair + 1 ]) ) >> 1;
                        nCr = ( static_cast<long>(pCr[ nXPair ]) + static_cast<long>(pCr[ nXPair + 1 ]) ) >> 1;
                    }
                }
                else {
                    nL = pL1[ nx ];
                    if ( ( nx & 1 ) == 0 )
                    {
                        nCb = ( static_cast<long>(pCb[ nXPair ]) + static_cast<long>(pCbN[ nXPair ]) ) >> 1;
                        nCr = ( static_cast<long>(pCr[ nXPair ]) + static_cast<long>(pCrN[ nXPair ]) ) >> 1;
                    }
                    else
                    {
                        nCb = ( static_cast<long>(pCb[ nXPair ]) + static_cast<long>(pCb[ nXPair + 1 ]) +
                               static_cast<long>(pCbN[ nXPair ]) + static_cast<long>(pCbN[ nXPair + 1 ]) ) >> 2;
                        nCr = ( static_cast<long>(pCr[ nXPair ]) + static_cast<long>(pCr[ nXPair + 1]) +
                               static_cast<long>(pCrN[ nXPair ]) + static_cast<long>(pCrN[ nXPair + 1 ]) ) >> 2;
                    }
                }
                // conversion of nL,nCb,nCr in nRed,nGreen,nBlue:
                nL *= 89024;
                nCb -= 156;
                nCr -= 137;
                nRed = ( nL + nCr * 119374 + 0x8000 ) >> 16;
                if ( nRed < 0 )
                    nRed = 0;
                if ( nRed > 255)
                    nRed = 255;
                nGreen = ( nL - nCb * 28198 - nCr * 60761 + 0x8000 ) >> 16;
                if ( nGreen < 0 )
                    nGreen = 0;
                if ( nGreen > 255 )
                    nGreen = 255;
                nBlue = ( nL + nCb * 145352 + 0x8000 ) >> 16;
                if ( nBlue < 0 )
                    nBlue = 0;
                if ( nBlue > 255 )
                    nBlue = 255;

                // register color value in pBMPMap:
                if ( nOrientation < 2 )
                {
                    if ( nOrientation == 0 )
                        mpBitmap->SetPixel( ny, nx, Color( static_cast<sal_uInt8>(nRed), static_cast<sal_uInt8>(nGreen), static_cast<sal_uInt8>(nBlue) ) );
                    else
                        mpBitmap->SetPixel( nWidth - 1 - nx, ny, Color( static_cast<sal_uInt8>(nRed), static_cast<sal_uInt8>(nGreen), static_cast<sal_uInt8>(nBlue) ) );
                }
                else
                {
                    if ( nOrientation == 2 )
                        mpBitmap->SetPixel( nHeight - 1 - ny, ( nWidth - 1 - nx ), Color( static_cast<sal_uInt8>(nRed), static_cast<sal_uInt8>(nGreen), static_cast<sal_uInt8>(nBlue) ) );
                    else
                        mpBitmap->SetPixel( nx, ( nHeight - 1 - ny ), Color( static_cast<sal_uInt8>(nRed), static_cast<sal_uInt8>(nGreen), static_cast<sal_uInt8>(nBlue) ) );
                }
            }
        }

        if ( m_rPCD.GetError() )
            bStatus = false;
        if ( !bStatus )
            break;
    }
    std::free(static_cast<void*>(pL0) );
    std::free(static_cast<void*>(pL1) );
    std::free(static_cast<void*>(pCb) );
    std::free(static_cast<void*>(pCr) );
    std::free(static_cast<void*>(pL0N));
    std::free(static_cast<void*>(pL1N));
    std::free(static_cast<void*>(pCbN));
    std::free(static_cast<void*>(pCrN));
}

//================== GraphicImport - the exported Function ================

extern "C" SAL_DLLPUBLIC_EXPORT bool
icdGraphicImport( SvStream & rStream, Graphic & rGraphic, FilterConfigItem* pConfigItem )
{
    PCDReader aPCDReader(rStream);
    return aPCDReader.ReadPCD(rGraphic, pConfigItem);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */