Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtHelp / il / ilcodec.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: ilcodec.c /main/6 1996/06/19 12:24:18 ageorge $ */
24 /**---------------------------------------------------------------------
25 ***     
26 ***    (c)Copyright 1991 Hewlett-Packard Co.
27 ***    
28 ***                             RESTRICTED RIGHTS LEGEND
29 ***    Use, duplication, or disclosure by the U.S. Government is subject to
30 ***    restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
31 ***    Technical Data and Computer Software clause in DFARS 252.227-7013.
32 ***                             Hewlett-Packard Company
33 ***                             3000 Hanover Street
34 ***                             Palo Alto, CA 94304 U.S.A.
35 ***    Rights for non-DOD U.S. Government Departments and Agencies are as set
36 ***    forth in FAR 52.227-19(c)(1,2).
37 ***
38 ***-------------------------------------------------------------------*/
39
40 #include "ilint.h"
41 #include "ilpipelem.h"
42 #include "ilpipeint.h"
43 #include "ilcodec.h"
44 #include "ilerrors.h"
45
46
47     /*  ------------------------ ilReallocCompressedBuffer ------------------------ */
48     /*  Realloc (or alloc the first time) the pixel buffer for plane "plane" of the 
49         compressed image "*pImage", so that its "bufferSize" is a minimum of 
50        "minNewSize" bytes in size.
51     */
52 IL_PRIVATE ilBool _ilReallocCompressedBuffer (
53     ilImageInfo        *pImage,
54     unsigned int        plane,
55     unsigned long       minNewSize
56     )
57 {
58 register ilImagePlaneInfo *pPlane;
59
60     pPlane = &pImage->plane[plane];
61     pPlane->bufferSize = minNewSize + 10000;  /* A GUESS - DO SOMETHING SMARTER !!!!! */
62
63     if (!pPlane->pPixels) pPlane->pPixels = (ilPtr)IL_MALLOC (pPlane->bufferSize);
64     else pPlane->pPixels = (ilPtr)IL_REALLOC (pPlane->pPixels, pPlane->bufferSize);
65     if (!pPlane->pPixels) {
66         pPlane->bufferSize = 0;
67         return FALSE;
68         }
69     return TRUE;
70 }
71
72
73     /*  ------------------------ ilCopyCompressedExecute ------------------------ */
74     /*  Execute() function for ilInsertCompressedCopyFilter() to copy compressed images.
75         Copies one strip of compressed data.
76     */
77 static ilError ilCopyCompressedExecute (
78     register ilExecuteData  *pData,
79     long                    dstLine,
80     long                   *pNLines
81     )
82 {
83     register ilImagePlaneInfo *pSrcPlane, *pDstPlane;
84     long                    nBytes, dstOffset, requiredBufferSize;
85
86     nBytes = pData->compressed.nBytesToRead;        /* # of bytes to write */
87     pSrcPlane = &pData->pSrcImage->plane[0];
88     if (!pSrcPlane->pPixels || (nBytes <= 0))       /* nothing to copy; exit */
89         return IL_OK;
90
91     dstOffset = *pData->compressed.pDstOffset;      /* byte offset into dst buffer */
92     pDstPlane = &pData->pDstImage->plane[0];
93     requiredBufferSize = nBytes + dstOffset;        /* # bytes needed in dst buffer */
94
95         /*  Check for space in output buffer; realloc/malloc if not enough */
96     if (requiredBufferSize > pDstPlane->bufferSize) {
97         pDstPlane->pPixels = (pDstPlane->pPixels) ? 
98             (ilPtr)IL_REALLOC (pDstPlane->pPixels, requiredBufferSize) :
99             (ilPtr)IL_MALLOC (requiredBufferSize);
100         if (!pDstPlane->pPixels) {
101             pDstPlane->bufferSize = 0;
102             return IL_ERROR_MALLOC;
103             }
104         pDstPlane->bufferSize = requiredBufferSize;
105         }
106
107         /*  Copy nBytes from src to dst buffer, using offsets from *pData */
108     bcopy ((char *)(pSrcPlane->pPixels + pData->compressed.srcOffset), 
109            (char *)(pDstPlane->pPixels + dstOffset), nBytes);
110     *pData->compressed.pNBytesWritten = nBytes;
111
112     return IL_OK;
113 }
114
115
116         /*  ------------------- ilInsertCompressedCopyFilter ------------------------- */
117         /*  Insert a "filter" which copies the compressed pipe image to the dest image.
118             This is the equivalent to ilInsertCopyFilter(), except for compressed images.
119         */
120 IL_PRIVATE ilBool _ilInsertCompressedCopyFilter (
121     ilPipe              pipe
122     )
123 {
124         /*  Add a filter which copies src to dst.  No private needed; the Execute()
125             function does all necessary setup.
126         */
127     return (ilAddPipeElement (pipe, IL_FILTER, 0, 0, 
128         (ilSrcElementData *)NULL, (ilDstElementData *)NULL, 
129         IL_NPF, IL_NPF, IL_NPF, ilCopyCompressedExecute, NULL, 0)) ? TRUE : FALSE;
130
131 }
132