Code sync
[external/hplip.git] / prnt / hpijs / scaler.cpp
1 /*****************************************************************************\
2   scaler.cpp : Implimentation for the Scaler class
3
4   Copyright (c) 1996 - 2001, Hewlett-Packard Co.
5   All rights reserved.
6
7   Redistribution and use in source and binary forms, with or without
8   modification, are permitted provided that the following conditions
9   are met:
10   1. Redistributions of source code must retain the above copyright
11      notice, this list of conditions and the following disclaimer.
12   2. Redistributions in binary form must reproduce the above copyright
13      notice, this list of conditions and the following disclaimer in the
14      documentation and/or other materials provided with the distribution.
15   3. Neither the name of Hewlett-Packard nor the names of its
16      contributors may be used to endorse or promote products derived
17      from this software without specific prior written permission.
18
19   THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
22   NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24   TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25   OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 \*****************************************************************************/
30
31 #include "header.h"
32
33 APDK_BEGIN_NAMESPACE
34
35 #define MAX_OUTPUT_RASTERS 32
36
37
38 Scaler::Scaler(SystemServices* pSys,unsigned int inputwidth,
39                unsigned int numerator,unsigned int denominator, BOOL vip, unsigned int BytesPerPixel)
40         : pSS(pSys), iInputWidth(inputwidth)
41 {
42
43     ASSERT(denominator > 0);
44
45     constructor_error=NO_ERROR;
46     for (int i = COLORTYPE_COLOR; i < MAX_COLORTYPE; i++)\r
47     {\r
48         pOutputBuffer[i] = NULL;\r
49     }\r
50
51     ScaleFactor= (float)numerator / (float)denominator;
52     if (ScaleFactor > (float)MAX_OUTPUT_RASTERS)      //
53     {
54         constructor_error = INDEX_OUT_OF_RANGE;
55         return;
56     }
57     int factor = (int)ScaleFactor;
58     float rem = ScaleFactor - (float)factor;
59     rem *= 1000;
60     remainder = (int)rem;
61
62     iOutputWidth = (int) (((float) iInputWidth / (float) denominator) *
63                           (float) numerator);
64     iOutputWidth++;         // safety measure to protect against roundoff error
65
66     if (numerator == denominator)
67         scaling=FALSE;
68     else scaling=TRUE;
69
70
71     // ScaleBound=max number of output rows per input row;
72     // i.e., if scale=4.28, then sometimes 5 rows will come out
73
74     int ScaleBound = int(ScaleFactor);
75     if  (ScaleFactor > (float) ScaleBound)
76         ScaleBound++;
77
78     // allocate a buffer for one output row
79     int RSBuffSize= (int)(((float)(BytesPerPixel*iOutputWidth)) * ScaleBound );
80     pOutputBuffer[COLORTYPE_COLOR]=(BYTE*)pSS->AllocMem(RSBuffSize);
81     if (pOutputBuffer[COLORTYPE_COLOR] == NULL)
82     {
83         constructor_error=ALLOCMEM_ERROR;
84         return;
85     }\r
86 \r
87 //  Initialize RGB buffer to white\r
88     if (vip)\r
89     {\r
90         memset(pOutputBuffer[COLORTYPE_COLOR], 0xFF, RSBuffSize);\r
91     }\r
92
93         int BlackBuffSize= (int)(((float)(iOutputWidth)) * ScaleBound );
94     pOutputBuffer[COLORTYPE_BLACK]=(BYTE*)pSS->AllocMem(BlackBuffSize);
95     if (pOutputBuffer[COLORTYPE_BLACK] == NULL)
96     {
97         constructor_error=ALLOCMEM_ERROR;
98         return;
99     }
100
101     if (ScaleFactor < 2.0)
102         ReplicateOnly = TRUE;
103     else ReplicateOnly=FALSE;
104
105     if (ScaleFactor > (float)factor)
106         fractional=TRUE;
107     else fractional=FALSE;
108 }
109
110 Scaler::~Scaler()
111 {
112         for (int i = COLORTYPE_COLOR; i < MAX_COLORTYPE; i++)
113         {
114                 if (pOutputBuffer[i]) 
115                 {
116                         pSS->FreeMemory(pOutputBuffer[i]);
117                         pOutputBuffer[i] = NULL;
118                 }
119         }
120 }
121
122 unsigned int Scaler::GetMaxOutputWidth(COLORTYPE  rastercolor)
123 {
124         if (rastercolor == COLORTYPE_COLOR)
125         {
126                 return (iOutputWidth-1)*NUMBER_PLANES;  // we padded it in case of roundoff error
127         }
128         else
129         {       
130                 return (iOutputWidth-1);  // we padded it in case of roundoff error
131         }
132 }
133
134 unsigned int Scaler::GetOutputWidth(COLORTYPE color)
135 {
136         if (color == COLORTYPE_COLOR)
137         {
138                 if (raster.rasterdata[color])
139                         if (myplane == COLORTYPE_BLACK)
140                                 return raster.rastersize[color];
141                         else
142                                 return (iOutputWidth-1)*NUMBER_PLANES;  // we padded it in case of roundoff error
143                 else
144                         return 0;
145         }
146         else
147         {       
148                 if (raster.rasterdata[color])
149                         if (myplane == COLORTYPE_COLOR)
150                                 return raster.rastersize[color];
151                         else
152                                 return (iOutputWidth-1);  // we padded it in case of roundoff error
153                 else 
154                         return 0;
155         }
156 }
157
158 APDK_END_NAMESPACE
159