Tizen 2.1 base
[platform/upstream/hplip.git] / ip / xskel.c
1 /* libhpojip -- HP OfficeJet image-processing library. */
2
3 /* Copyright (C) 1995-2002 Hewlett-Packard Company
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
12  * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
13  * NON-INFRINGEMENT.  See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18  * MA 02111-1307, USA.
19  *
20  * In addition, as a special exception, Hewlett-Packard Company
21  * gives permission to link the code of this program with any
22  * version of the OpenSSL library which is distributed under a
23  * license identical to that listed in the included LICENSE.OpenSSL
24  * file, and distribute linked combinations including the two.
25  * You must obey the GNU General Public License in all respects
26  * for all of the code used other than OpenSSL.  If you modify
27  * this file, you may extend this exception to your version of the
28  * file, but you are not obligated to do so.  If you do not wish to
29  * do so, delete this exception statement from your version.
30  */
31
32 /* Original author: Mark Overton and others.
33  *
34  * Ported to Linux by David Paschal.
35  */
36
37 /******************************************************************************\
38  *
39  * xskel.c - A skeleton xform driver on which new xforms can be based
40  *
41  * You must change "skel" and "SKEL" to your actual xform name.
42  *
43  ******************************************************************************
44  *
45  * Name of Global Jump-Table:
46  *
47  *    skelTbl
48  *
49  * Items in aXformInfo array passed into setXformSpec:
50  *
51  *    aXformInfo[IP_SKEL_SPEC_1] = fully describe these options here, if any
52  *    aXformInfo[IP_SKEL_SPEC_2] =
53  *
54  * Capabilities and Limitations:
55  *
56  *    What does this xform do?  What limitations of data types, ranges, etc?
57  *
58  * Default Input Traits, and Output Traits:
59  *
60  *    Describe what you do with the default input traits, and how the
61  *    output traits are determined.
62  *
63  *          trait             default input             output
64  *    -------------------  ---------------------  ------------------------
65  *    iPixelsPerRow         * passed into output   same as default input
66  *    iBitsPerPixel         * passed into output   same as default input
67  *    iComponentsPerPixel   * passed into output   same as default input
68  *    lHorizDPI               passed into output   same as default input
69  *    lVertDPI                passed into output   same as default input
70  *    lNumRows                passed into output   same as default input
71  *    iNumPages               passed into output   same as default input
72  *    iPageNum                passed into output   same as default input
73  *
74  *    Above, a "*" by an item indicates it must be valid (not negative).
75  *
76 \******************************************************************************/
77
78 // Use the #define below if this transform will exist in a dll outside of the
79 // image pipeline.  This will allow the functions to be exported.
80 // #define EXPORT_TRANFORM 1
81
82 #include "hpip.h"
83 #include "ipdefs.h"
84 #include "string.h"    /* for memset and memcpy */
85
86
87 #if 0
88     #include "stdio.h"
89     #include <tchar.h>
90     #define PRINT(msg,arg1,arg2) \
91         _ftprintf(stderr, msg, (int)arg1, (int)arg2)
92 #else
93     #define PRINT(msg,arg1,arg2)
94 #endif
95
96 #define CHECK_VALUE 0x4ba1dace
97
98 #ifdef EXPORT_TRANSFORM
99 #define FUNC_STATUS __declspec (dllexport)
100 #else
101 #define FUNC_STATUS static
102 #endif
103
104 typedef struct {
105     IP_IMAGE_TRAITS traits;   /* traits of the input and output image */
106     DWORD    dwBytesPerRow;   /* # of bytes in each row */
107     DWORD    dwRowsDone;      /* number of rows converted so far */
108     DWORD    dwInNextPos;     /* file pos for subsequent input */
109     DWORD    dwOutNextPos;    /* file pos for subsequent output */
110     DWORD    dwValidChk;      /* struct validity check value */
111 } SKEL_INST, *PSKEL_INST;
112
113
114
115 /*****************************************************************************\
116  *
117  * skel_openXform - Creates a new instance of the transformer
118  *
119  *****************************************************************************
120  *
121  * This returns a handle for the new instance to be passed into
122  * all subsequent calls.
123  *
124  * Return value: IP_DONE=success; IP_FATAL_ERROR=misc error.
125  *
126 \*****************************************************************************/
127
128 FUNC_STATUS WORD skel_openXform (
129     IP_XFORM_HANDLE *pXform)   /* out: returned handle */
130 {
131     PSKEL_INST g;
132
133     INSURE (pXform != NULL);
134     IP_MEM_ALLOC (sizeof(SKEL_INST), g);
135     *pXform = g;
136     memset (g, 0, sizeof(SKEL_INST));
137     g->dwValidChk = CHECK_VALUE;
138     return IP_DONE;
139
140     fatal_error:
141     return IP_FATAL_ERROR;
142 }
143
144
145
146 /*****************************************************************************\
147  *
148  * skel_setDefaultInputTraits - Specifies default input image traits
149  *
150  *****************************************************************************
151  *
152  * The header of the file-type handled by the transform probably does
153  * not include *all* the image traits we'd like to know.  Those not
154  * specified in the file-header are filled in from info provided by
155  * this routine.
156  *
157  * Return value: IP_DONE=success; IP_FATAL_ERROR=misc error.
158  *
159 \*****************************************************************************/
160
161 FUNC_STATUS WORD skel_setDefaultInputTraits (
162     IP_XFORM_HANDLE  hXform,     /* in: handle for xform */
163     PIP_IMAGE_TRAITS pTraits)    /* in: default image traits */
164 {
165     PSKEL_INST g;
166
167     HANDLE_TO_PTR (hXform, g);
168
169     /* insure that traits we care about are known */
170     INSURE (pTraits->iPixelsPerRow>0 && pTraits->iBitsPerPixel>0);
171     g->traits = *pTraits;   /* a structure copy */
172     g->dwBytesPerRow = (g->traits.iPixelsPerRow*g->traits.iBitsPerPixel + 7) / 8;
173     return IP_DONE;
174
175     fatal_error:
176     return IP_FATAL_ERROR;
177 }
178
179
180
181 /*****************************************************************************\
182  *
183  * skel_setXformSpec - Provides xform-specific information
184  *
185 \*****************************************************************************/
186
187 FUNC_STATUS WORD skel_setXformSpec (
188     IP_XFORM_HANDLE hXform,         /* in: handle for xform */
189     DWORD_OR_PVOID  aXformInfo[])   /* in: xform information */
190 {
191     PSKEL_INST g;
192
193     HANDLE_TO_PTR (hXform, g);
194
195     /* Check your options in aXformInfo here, and save them.
196      * Use the INSURE macro like you'd use assert.  INSURE jumps to
197      * fatal_error below if it fails.
198      */
199
200     return IP_DONE;
201
202     fatal_error:
203     return IP_FATAL_ERROR;
204 }
205
206
207
208 /*****************************************************************************\
209  *
210  * skel_getHeaderBufSize- Returns size of input buf needed to hold header
211  *
212 \*****************************************************************************/
213
214 FUNC_STATUS WORD skel_getHeaderBufSize (
215     IP_XFORM_HANDLE  hXform,         /* in:  handle for xform */
216     DWORD           *pdwInBufLen)    /* out: buf size for parsing header */
217 {
218     /* since input is raw pixels, there is no header, so set it to zero */
219     *pdwInBufLen = 0;
220     return IP_DONE;
221 }
222
223
224
225 /*****************************************************************************\
226  *
227  * skel_getActualTraits - Parses header, and returns input & output traits
228  *
229 \*****************************************************************************/
230
231 FUNC_STATUS WORD skel_getActualTraits (
232     IP_XFORM_HANDLE  hXform,         /* in:  handle for xform */
233     DWORD            dwInputAvail,   /* in:  # avail bytes in input buf */
234     PBYTE            pbInputBuf,     /* in:  ptr to input buffer */
235     PDWORD           pdwInputUsed,   /* out: # bytes used from input buf */
236     PDWORD           pdwInputNextPos,/* out: file-pos to read from next */
237     PIP_IMAGE_TRAITS pInTraits,      /* out: input image traits */
238     PIP_IMAGE_TRAITS pOutTraits)     /* out: output image traits */
239 {
240     PSKEL_INST g;
241
242     HANDLE_TO_PTR (hXform, g);
243
244     /* Since there is no header, we'll report no usage of input */
245     *pdwInputUsed    = 0;
246     *pdwInputNextPos = 0;
247     g->dwInNextPos   = 0;
248
249     *pInTraits  = g->traits;   /* structure copies */
250     *pOutTraits = g->traits;
251
252     return IP_DONE | IP_READY_FOR_DATA;
253
254     fatal_error:
255     return IP_FATAL_ERROR;
256 }
257
258
259
260 /****************************************************************************\
261  *
262  * skel_getActualBufSizes - Returns buf sizes needed for remainder of job
263  *
264 \****************************************************************************/
265
266 FUNC_STATUS WORD skel_getActualBufSizes (
267     IP_XFORM_HANDLE hXform,          /* in:  handle for xform */
268     PDWORD          pdwMinInBufLen,  /* out: min input buf size */
269     PDWORD          pdwMinOutBufLen) /* out: min output buf size */
270 {
271     PSKEL_INST g;
272
273     HANDLE_TO_PTR (hXform, g);
274
275     *pdwMinInBufLen = *pdwMinOutBufLen = g->dwBytesPerRow;
276     return IP_DONE;
277
278     fatal_error:
279     return IP_FATAL_ERROR;
280 }
281
282
283
284 /*****************************************************************************\
285  *
286  * skel_convert - Converts one row
287  *
288 \*****************************************************************************/
289
290 FUNC_STATUS WORD skel_convert (
291     IP_XFORM_HANDLE hXform,
292     DWORD           dwInputAvail,     /* in:  # avail bytes in in-buf */
293     PBYTE           pbInputBuf,       /* in:  ptr to in-buffer */
294     PDWORD          pdwInputUsed,     /* out: # bytes used from in-buf */
295     PDWORD          pdwInputNextPos,  /* out: file-pos to read from next */
296     DWORD           dwOutputAvail,    /* in:  # avail bytes in out-buf */
297     PBYTE           pbOutputBuf,      /* in:  ptr to out-buffer */
298     PDWORD          pdwOutputUsed,    /* out: # bytes written in out-buf */
299     PDWORD          pdwOutputThisPos) /* out: file-pos to write the data */
300 {
301     PSKEL_INST g;
302     int       nBytes;
303     PBYTE     pIn, pOut;
304
305     HANDLE_TO_PTR (hXform, g);
306
307     /**** Check if we were told to flush ****/
308
309     if (pbInputBuf == NULL) {
310         PRINT (_T("skel_convert: Told to flush.\n"), 0, 0);
311         *pdwInputUsed = *pdwOutputUsed = 0;
312         *pdwInputNextPos  = g->dwInNextPos;
313         *pdwOutputThisPos = g->dwOutNextPos;
314         return IP_DONE;
315     }
316
317     /**** Output a Row ****/
318
319     nBytes = g->dwBytesPerRow;
320     INSURE (dwInputAvail  >= (DWORD)nBytes );
321     INSURE (dwOutputAvail >= (DWORD)nBytes);
322
323     pIn  = pbInputBuf;
324     pOut = pbOutputBuf;
325
326     /* At this point, pIn is your input buffer, and pOut is your output buffer.
327      * Do whatever you are going to do here.
328      */
329     memcpy(pOut,pIn,nBytes);
330
331     *pdwInputUsed     = nBytes;
332     g->dwInNextPos   += nBytes;
333     *pdwInputNextPos  = g->dwInNextPos;
334
335     *pdwOutputUsed    = nBytes;
336     *pdwOutputThisPos = g->dwOutNextPos;
337     g->dwOutNextPos  += nBytes;
338
339     g->dwRowsDone += 1;
340
341     return IP_CONSUMED_ROW | IP_PRODUCED_ROW | IP_READY_FOR_DATA;
342
343     fatal_error:
344     return IP_FATAL_ERROR;
345 }
346
347
348
349 /*****************************************************************************\
350  *
351  * skel_insertedData - client inserted into our output stream
352  *
353 \*****************************************************************************/
354
355 FUNC_STATUS WORD skel_insertedData (
356     IP_XFORM_HANDLE hXform,
357     DWORD           dwNumBytes)
358 {
359     fatalBreakPoint ();
360     return IP_FATAL_ERROR;   /* must never be called (can't insert data) */
361 }
362
363
364
365 /*****************************************************************************\
366  *
367  * skel_newPage - Tells us to flush this page, and start a new page
368  *
369 \*****************************************************************************/
370
371 FUNC_STATUS WORD skel_newPage (
372     IP_XFORM_HANDLE hXform)
373 {
374     PSKEL_INST g;
375
376     HANDLE_TO_PTR (hXform, g);
377     /* todo: return fatal error if convert is called again? */
378     return IP_DONE;   /* can't insert page-breaks, so ignore this call */
379
380     fatal_error:
381     return IP_FATAL_ERROR;
382
383 }
384
385
386
387 /*****************************************************************************\
388  *
389  * skel_closeXform - Destroys this instance
390  *
391 \*****************************************************************************/
392
393 FUNC_STATUS WORD skel_closeXform (IP_XFORM_HANDLE hXform)
394 {
395     PSKEL_INST g;
396
397     HANDLE_TO_PTR (hXform, g);
398
399     g->dwValidChk = 0;
400     IP_MEM_FREE (g);       /* free memory for the instance */
401
402     return IP_DONE;
403
404     fatal_error:
405     return IP_FATAL_ERROR;
406 }
407
408
409
410 /*****************************************************************************\
411  *
412  * skelTbl - Jump-table for transform driver
413  *
414 \*****************************************************************************/
415 #ifdef EXPORT_TRANSFORM
416 __declspec (dllexport)
417 #endif
418 IP_XFORM_TBL skelTbl = {
419     skel_openXform,
420     skel_setDefaultInputTraits,
421     skel_setXformSpec,
422     skel_getHeaderBufSize,
423     skel_getActualTraits,
424     skel_getActualBufSizes,
425     skel_convert,
426     skel_newPage,
427     skel_insertedData,
428     skel_closeXform
429 };
430
431 /* End of File */
432
433
434 /*****************************************************************************\
435  *
436  * ipGetXformTable - Returns pointer to the jump table
437  *
438 \*****************************************************************************/
439
440 #ifdef EXPORT_TRANSFORM
441 EXPORT(WORD) ipGetXformTable (LPIP_XFORM_TBL pXform)
442 {
443     WORD wRet = IP_DONE;
444
445     if (pXform)
446     {
447         *pXform = clrmapTbl;
448     }
449     else
450     {
451         wRet = IP_FATAL_ERROR;
452     }
453
454     return wRet;
455 }
456 #endif