| 1 | /*****************************************************************************\ |
| 2 | hpcupscdmfax.cpp : HP CUPS fax filter |
| 3 | |
| 4 | Copyright (c) 2001 - 2010, HP 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 the 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 |
| 20 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 21 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 | NOT LIMITED TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 28 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | POSSIBILITY OF SUCH DAMAGE. |
| 30 | \*****************************************************************************/ |
| 31 | |
| 32 | #include <sys/stat.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <stdarg.h> |
| 36 | #include <syslog.h> |
| 37 | #include <string.h> |
| 38 | #include <unistd.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <stdint.h> |
| 41 | #include <time.h> |
| 42 | #include <sys/time.h> |
| 43 | #include <arpa/inet.h> |
| 44 | #include <math.h> |
| 45 | #include <cups/cups.h> |
| 46 | #include <cups/raster.h> |
| 47 | #include <string> |
| 48 | #ifdef FALSE |
| 49 | #undef FALSE |
| 50 | #endif |
| 51 | #ifdef TRUE |
| 52 | #undef TRUE |
| 53 | #endif |
| 54 | |
| 55 | #include "hpcupscdmfax.h" |
| 56 | #include "bug.h" |
| 57 | using namespace std; |
| 58 | |
| 59 | static int iLogLevel = 1; |
| 60 | char hpFileName[MAX_FILE_PATH_LEN]; |
| 61 | |
| 62 | #define DBG(args...) syslog(LOG_INFO, __FILE__ " " STRINGIZE(__LINE__) ": " args) |
| 63 | |
| 64 | static void GetLogLevel() |
| 65 | { |
| 66 | FILE *fp; |
| 67 | char str[258]; |
| 68 | char *p; |
| 69 | fp = fopen("/etc/cups/cupsd.conf", "r"); |
| 70 | if (fp == NULL) |
| 71 | return; |
| 72 | while (!feof(fp)) |
| 73 | { |
| 74 | if (!fgets(str, 256, fp)) |
| 75 | { |
| 76 | break; |
| 77 | } |
| 78 | if ((p = strstr(str, "hpLogLevel"))) |
| 79 | { |
| 80 | p += strlen("hpLogLevel") + 1; |
| 81 | iLogLevel = atoi(p); |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | fclose(fp); |
| 86 | } |
| 87 | |
| 88 | void PrintCupsHeader(cups_page_header2_t m_cupsHeader) |
| 89 | { |
| 90 | if (iLogLevel == 0) |
| 91 | { |
| 92 | return; |
| 93 | } |
| 94 | BUG("DEBUG: HPFAX - startPage...\n"); |
| 95 | BUG("DEBUG: HPFAX - MediaClass = \"%s\"\n", m_cupsHeader.MediaClass); |
| 96 | BUG("DEBUG: HPFAX - MediaColor = \"%s\"\n", m_cupsHeader.MediaColor); |
| 97 | BUG("DEBUG: HPFAX - MediaType = \"%s\"\n", m_cupsHeader.MediaType); |
| 98 | BUG("DEBUG: HPFAX - OutputType = \"%s\"\n", m_cupsHeader.OutputType); |
| 99 | BUG("DEBUG: HPFAX - AdvanceDistance = %d\n", m_cupsHeader.AdvanceDistance); |
| 100 | BUG("DEBUG: HPFAX - AdvanceMedia = %d\n", m_cupsHeader.AdvanceMedia); |
| 101 | BUG("DEBUG: HPFAX - Collate = %d\n", m_cupsHeader.Collate); |
| 102 | BUG("DEBUG: HPFAX - CutMedia = %d\n", m_cupsHeader.CutMedia); |
| 103 | BUG("DEBUG: HPFAX - Duplex = %d\n", m_cupsHeader.Duplex); |
| 104 | BUG("DEBUG: HPFAX - HWResolution = [ %d %d ]\n", m_cupsHeader.HWResolution[0], m_cupsHeader.HWResolution[1]); |
| 105 | BUG("DEBUG: HPFAX - ImagingBoundingBox = [ %d %d %d %d ]\n", |
| 106 | m_cupsHeader.ImagingBoundingBox[0], m_cupsHeader.ImagingBoundingBox[1], |
| 107 | m_cupsHeader.ImagingBoundingBox[2], m_cupsHeader.ImagingBoundingBox[3]); |
| 108 | BUG("DEBUG: HPFAX - InsertSheet = %d\n", m_cupsHeader.InsertSheet); |
| 109 | BUG("DEBUG: HPFAX - Jog = %d\n", m_cupsHeader.Jog); |
| 110 | BUG("DEBUG: HPFAX - LeadingEdge = %d\n", m_cupsHeader.LeadingEdge); |
| 111 | BUG("DEBUG: HPFAX - Margins = [ %d %d ]\n", m_cupsHeader.Margins[0], m_cupsHeader.Margins[1]); |
| 112 | BUG("DEBUG: HPFAX - ManualFeed = %d\n", m_cupsHeader.ManualFeed); |
| 113 | BUG("DEBUG: HPFAX - MediaPosition = %d\n", m_cupsHeader.MediaPosition); |
| 114 | BUG("DEBUG: HPFAX - MediaWeight = %d\n", m_cupsHeader.MediaWeight); |
| 115 | BUG("DEBUG: HPFAX - MirrorPrint = %d\n", m_cupsHeader.MirrorPrint); |
| 116 | BUG("DEBUG: HPFAX - NegativePrint = %d\n", m_cupsHeader.NegativePrint); |
| 117 | BUG("DEBUG: HPFAX - NumCopies = %d\n", m_cupsHeader.NumCopies); |
| 118 | BUG("DEBUG: HPFAX - Orientation = %d\n", m_cupsHeader.Orientation); |
| 119 | BUG("DEBUG: HPFAX - OutputFaceUp = %d\n", m_cupsHeader.OutputFaceUp); |
| 120 | BUG("DEBUG: HPFAX - PageSize = [ %d %d ]\n", m_cupsHeader.PageSize[0], m_cupsHeader.PageSize[1]); |
| 121 | BUG("DEBUG: HPFAX - Separations = %d\n", m_cupsHeader.Separations); |
| 122 | BUG("DEBUG: HPFAX - TraySwitch = %d\n", m_cupsHeader.TraySwitch); |
| 123 | BUG("DEBUG: HPFAX - Tumble = %d\n", m_cupsHeader.Tumble); |
| 124 | BUG("DEBUG: HPFAX - cupsWidth = %d\n", m_cupsHeader.cupsWidth); |
| 125 | BUG("DEBUG: HPFAX - cupsHeight = %d\n", m_cupsHeader.cupsHeight); |
| 126 | BUG("DEBUG: HPFAX - cupsMediaType = %d\n", m_cupsHeader.cupsMediaType); |
| 127 | BUG("DEBUG: HPFAX - cupsRowStep = %d\n", m_cupsHeader.cupsRowStep); |
| 128 | BUG("DEBUG: HPFAX - cupsBitsPerColor = %d\n", m_cupsHeader.cupsBitsPerColor); |
| 129 | BUG("DEBUG: HPFAX - cupsBitsPerPixel = %d\n", m_cupsHeader.cupsBitsPerPixel); |
| 130 | BUG("DEBUG: HPFAX - cupsBytesPerLine = %d\n", m_cupsHeader.cupsBytesPerLine); |
| 131 | BUG("DEBUG: HPFAX - cupsColorOrder = %d\n", m_cupsHeader.cupsColorOrder); |
| 132 | BUG("DEBUG: HPFAX - cupsColorSpace = %d\n", m_cupsHeader.cupsColorSpace); |
| 133 | BUG("DEBUG: HPFAX - cupsCompression = %d\n", m_cupsHeader.cupsCompression); |
| 134 | BUG("DEBUG: HPFAX - cupsPageSizeName = %s\n", m_cupsHeader.cupsPageSizeName); |
| 135 | } |
| 136 | |
| 137 | static void Gjpeg_error(j_common_ptr cinfo) |
| 138 | { |
| 139 | // The standard behavior is to send a message to stderr. |
| 140 | // Since this is icky for Gumby, we change it to do nothing |
| 141 | } |
| 142 | |
| 143 | bool CompressJPEG(cups_raster_t *cups_raster, |
| 144 | cups_page_header2_t cups_header, |
| 145 | QTABLEINFO *pQTable, |
| 146 | BYTE **oCompressedBuf, |
| 147 | unsigned long *oCompressedBufSize) |
| 148 | |
| 149 | { |
| 150 | // |
| 151 | // To remove the ugly if elses all over in this function. |
| 152 | // |
| 153 | int iColorsUsed = 1; |
| 154 | //---------------------------------------------------------------- |
| 155 | // Setup for compression |
| 156 | //---------------------------------------------------------------- |
| 157 | |
| 158 | //---------------------------------------------------------------- |
| 159 | // JPEG Lib Step 1: Allocate and initialize JPEG compression object |
| 160 | //---------------------------------------------------------------- |
| 161 | cinfo.err = jpeg_std_error(&jerr); |
| 162 | |
| 163 | // Fix the error handler to return when an error occurs, |
| 164 | // the default exit()s which is nasty for a driver to do. |
| 165 | jerr.error_exit = Gjpeg_error; |
| 166 | // Set the return jump address. This must be done now since |
| 167 | // jpeg_create_compress could cause an error. |
| 168 | if (setjmp(setjmp_buffer)) |
| 169 | { |
| 170 | |
| 171 | // If we get here, the JPEG code has signaled an error. |
| 172 | //* We need to clean up the JPEG object, and return. |
| 173 | |
| 174 | jpeg_destroy_compress(&cinfo); |
| 175 | return FALSE; |
| 176 | } |
| 177 | jpeg_create_compress(&cinfo); |
| 178 | jpeg_set_defaults(&cinfo); |
| 179 | jpeg_mem_dest(&cinfo, oCompressedBuf, oCompressedBufSize); |
| 180 | cinfo.in_color_space = JCS_GRAYSCALE; |
| 181 | |
| 182 | cinfo.image_width = cups_header.cupsWidth; |
| 183 | cinfo.image_height = cups_header.cupsHeight; |
| 184 | cinfo.input_components = iColorsUsed; // change if bit depths others than 24bpp are ever needed |
| 185 | cinfo.data_precision = 8; |
| 186 | |
| 187 | jpeg_default_colorspace(&cinfo); |
| 188 | //jpeg_set_quality(&cinfo, 50, true); |
| 189 | |
| 190 | // |
| 191 | // UGLY put the quant table addition to the JPEG in to a function. |
| 192 | // Create a static quant table here. |
| 193 | // |
| 194 | static unsigned int mojave_quant_table1[64] = {2, 3, 4, 5, 5, 5, 5, 5, |
| 195 | 3, 6, 5, 8, 5, 8, 5, 8, |
| 196 | 4, 5, 5, 5, 5, 5, 5, 5, |
| 197 | 5, 8, 5, 8, 5, 8, 5, 8, |
| 198 | 5, 5, 5, 5, 5, 5, 5, 5, |
| 199 | 5, 8, 5, 8, 5, 8, 5, 8, |
| 200 | 5, 5, 5, 5, 5, 5, 5, 5, |
| 201 | 5, 8, 5, 8, 5, 8, 5, 8}; |
| 202 | // |
| 203 | // Use this variable for representing the scale_factor for now. |
| 204 | // |
| 205 | unsigned int iScaleFactor = pQTable->qFactor; |
| 206 | |
| 207 | // |
| 208 | // Mojave specific Q-Tables will be added here. We do the following: |
| 209 | // 1. Add three Q-Tables. |
| 210 | // 2. Scale the Q-Table elemets with the given scale factor. |
| 211 | // 3. Check to see if any of the element is in the table is greater than 255 |
| 212 | // reser that elemet to 255. |
| 213 | // 5. There is a specific scaling need to be done to the first 6 |
| 214 | // elements in the matrix. This required to achieve the better |
| 215 | // compression ratio. |
| 216 | // 4. Check to see if any the of recently modified element is |
| 217 | // greater than 255, reset that with 255. |
| 218 | // Following for loop implements the above logic. |
| 219 | // |
| 220 | // Please refer to sRGBLaserHostBasedSoftwareERS.doc v9.0 section 5.2.5.3.1.1 |
| 221 | // for more details. |
| 222 | // |
| 223 | // [NOTE] These loop needs to be further optimized. |
| 224 | // |
| 225 | for (int i = 0; i < 3; i++) |
| 226 | { |
| 227 | // |
| 228 | // Adding Q-Table. |
| 229 | // |
| 230 | jpeg_add_quant_table(&cinfo, i, mojave_quant_table1, 0, false); |
| 231 | |
| 232 | // |
| 233 | // Scaling the Q-Table elements. |
| 234 | // Reset the element to 255, if it is greater than 255. |
| 235 | // |
| 236 | for (int j = 1; j < 64; j++) |
| 237 | { |
| 238 | cinfo.quant_tbl_ptrs[i]->quantval[j] = (unsigned short)(mojave_quant_table1[j] * iScaleFactor); |
| 239 | |
| 240 | // |
| 241 | // [PERF]Since we are using a scale_factor from 1 to 10 do we really need this. |
| 242 | // |
| 243 | if (cinfo.quant_tbl_ptrs[i]->quantval[j] > 255) |
| 244 | cinfo.quant_tbl_ptrs[i]->quantval[j] = 255; |
| 245 | } |
| 246 | // |
| 247 | // Special scaling for first 6 elements in the table. |
| 248 | // Reset the specially scaled elements 255, if it is greater than 255. |
| 249 | // |
| 250 | |
| 251 | // |
| 252 | // 1st component in the table. Unchanged, I need not change anything here. |
| 253 | // |
| 254 | cinfo.quant_tbl_ptrs[i]->quantval[0] = (unsigned short)mojave_quant_table1[0]; |
| 255 | |
| 256 | // |
| 257 | // 2nd and 3rd components in the zig zag order |
| 258 | // |
| 259 | // |
| 260 | // The following dTemp is being used to ciel the vales: e.g 28.5 to 29 |
| 261 | // |
| 262 | double dTemp = mojave_quant_table1[1] * (1 + 0.25 * (iScaleFactor - 1)); |
| 263 | dTemp = (dTemp + 0.5) / 1; |
| 264 | cinfo.quant_tbl_ptrs[i]->quantval[1] = (unsigned short)dTemp; |
| 265 | if (cinfo.quant_tbl_ptrs[i]->quantval[1] > 255) |
| 266 | cinfo.quant_tbl_ptrs[i]->quantval[1] = 255; |
| 267 | |
| 268 | dTemp = mojave_quant_table1[8] * (1 + 0.25 * (iScaleFactor - 1)); |
| 269 | dTemp = (dTemp + 0.5) / 1; |
| 270 | cinfo.quant_tbl_ptrs[i]->quantval[8] = (unsigned short)dTemp; |
| 271 | if (cinfo.quant_tbl_ptrs[i]->quantval[8] > 255) |
| 272 | cinfo.quant_tbl_ptrs[i]->quantval[8] = 255; |
| 273 | |
| 274 | // |
| 275 | // 4th, 5th and 6th components in the zig zag order |
| 276 | // |
| 277 | dTemp = mojave_quant_table1[16] * (1 + 0.50 * (iScaleFactor - 1)); |
| 278 | dTemp = (dTemp + 0.5) / 1; |
| 279 | cinfo.quant_tbl_ptrs[i]->quantval[16] = (unsigned short)dTemp; |
| 280 | if (cinfo.quant_tbl_ptrs[i]->quantval[16] > 255) |
| 281 | cinfo.quant_tbl_ptrs[i]->quantval[16] = 255; |
| 282 | |
| 283 | dTemp = mojave_quant_table1[9] * (1 + 0.50 * (iScaleFactor - 1)); |
| 284 | dTemp = (dTemp + 0.5) / 1; |
| 285 | cinfo.quant_tbl_ptrs[i]->quantval[9] = (unsigned short)dTemp; |
| 286 | if (cinfo.quant_tbl_ptrs[i]->quantval[9] > 255) |
| 287 | cinfo.quant_tbl_ptrs[i]->quantval[9] = 255; |
| 288 | |
| 289 | dTemp = mojave_quant_table1[2] * (1 + 0.50 * (iScaleFactor - 1)); |
| 290 | dTemp = (dTemp + 0.5) / 1; |
| 291 | cinfo.quant_tbl_ptrs[i]->quantval[2] = (unsigned short)dTemp; |
| 292 | if (cinfo.quant_tbl_ptrs[i]->quantval[2] > 255) |
| 293 | cinfo.quant_tbl_ptrs[i]->quantval[2] = 255; |
| 294 | } |
| 295 | // |
| 296 | // Hard code to use sampling mode 4:4:4 |
| 297 | // |
| 298 | cinfo.comp_info[0].h_samp_factor = 1; |
| 299 | cinfo.comp_info[0].v_samp_factor = 1; |
| 300 | cinfo.write_JFIF_header = cinfo.write_Adobe_marker = false; |
| 301 | jpeg_suppress_tables(&cinfo, true); //b4 true |
| 302 | |
| 303 | //---------------------------------------------------------------- |
| 304 | // JPEG Lib Step 4: Start the compression cycle |
| 305 | // set destination to table file |
| 306 | // jpeg_write_tables(cinfo); |
| 307 | // set destination to image file |
| 308 | // jpeg_start_compress(cinfo, FALSE); |
| 309 | //---------------------------------------------------------------- |
| 310 | jpeg_start_compress(&cinfo, true); |
| 311 | |
| 312 | //---------------------------------------------------------------- |
| 313 | // This completes the JPEG setup. |
| 314 | //---------------------------------------------------------------- |
| 315 | |
| 316 | // do the jpeg compression |
| 317 | JSAMPROW pRowArray[1]; |
| 318 | unsigned char *buffer = NULL; |
| 319 | |
| 320 | buffer = (unsigned char *)malloc(cups_header.cupsBytesPerLine); |
| 321 | /* read raster data */ |
| 322 | for (unsigned int y = 0; y < cups_header.cupsHeight; y++) |
| 323 | { |
| 324 | memset(buffer, 0, cups_header.cupsBytesPerLine); |
| 325 | if (cupsRasterReadPixels(cups_raster, buffer, cups_header.cupsBytesPerLine) == 0) |
| 326 | break; |
| 327 | pRowArray[0] = buffer; |
| 328 | jpeg_write_scanlines(&cinfo, pRowArray, 1); |
| 329 | } |
| 330 | if (buffer != NULL) |
| 331 | { |
| 332 | free(buffer); |
| 333 | } |
| 334 | //---------------------------------------------------------------- |
| 335 | // JPEG Lib Step 6: Finish compression |
| 336 | //---------------------------------------------------------------- |
| 337 | // Tell the compressor about the extra buffer space for the trailer |
| 338 | jpeg_finish_compress(&cinfo); |
| 339 | // |
| 340 | // Read the quantization tables used for the compression. |
| 341 | // BUGBUG - it's probably better to use the jpeg_write_tables routine, |
| 342 | // but this works for now. Hack alert!! |
| 343 | // |
| 344 | /* |
| 345 | if (cinfo.quant_tbl_ptrs[0] != NULL) |
| 346 | { |
| 347 | for (int iI = 0; iI < QTABLE_SIZE; iI++) |
| 348 | { |
| 349 | pQTable->qtable0[iI] = (unsigned int)cinfo.quant_tbl_ptrs[0]->quantval[iI]; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | if (cinfo.quant_tbl_ptrs[1] != NULL) |
| 354 | { |
| 355 | |
| 356 | for (int iI = 0; iI < QTABLE_SIZE; iI++) |
| 357 | { |
| 358 | pQTable->qtable1[iI] = (unsigned int)cinfo.quant_tbl_ptrs[1]->quantval[iI]; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if (cinfo.quant_tbl_ptrs[2] != NULL) |
| 363 | { |
| 364 | |
| 365 | for (int iI = 0; iI < QTABLE_SIZE; iI++) |
| 366 | { |
| 367 | pQTable->qtable2[iI] = (unsigned int)cinfo.quant_tbl_ptrs[2]->quantval[iI]; |
| 368 | } |
| 369 | } |
| 370 | */ |
| 371 | //---------------------------------------------------------------- |
| 372 | // JPEG Lib Step 7: Destroy the compression object |
| 373 | //---------------------------------------------------------------- |
| 374 | jpeg_destroy_compress(&cinfo); |
| 375 | |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | int ProcessRasterData(cups_raster_t *cups_raster, int fdFax) |
| 380 | { |
| 381 | int status = 0; |
| 382 | |
| 383 | cups_page_header2_t cups_header; |
| 384 | int iPageNum = 0; |
| 385 | |
| 386 | unsigned char *pDataArray = NULL; |
| 387 | unsigned char *pDataArray1 = NULL; |
| 388 | unsigned char *pDataArrayMoving = NULL; |
| 389 | unsigned short wCoordinate = 0; |
| 390 | unsigned long dwNumBytesAligned = 0, dwRemainder = 0, dwNumBytes = 0; |
| 391 | QTABLEINFO m_QTableInfo; |
| 392 | memset(&m_QTableInfo, 0, sizeof(m_QTableInfo)); |
| 393 | m_QTableInfo.qFactor = 6; |
| 394 | while (cupsRasterReadHeader2(cups_raster, &cups_header)) |
| 395 | { |
| 396 | iPageNum++; |
| 397 | clearStream(); |
| 398 | #ifdef PRINT_CUPS_HEADER |
| 399 | PrintCupsHeader(cups_header); |
| 400 | #endif |
| 401 | |
| 402 | fprintf(stderr, "b4 HP_BeginPage_1\n"); |
| 403 | HP_BeginPage_1(m_ptJetlibStream, HP_ePortraitOrientation, (HP_UByte)0); |
| 404 | write(fdFax, m_ptJetlibStream->HP_OutBuffer, m_ptJetlibStream->HP_CurrentBufferLen); |
| 405 | fprintf(stderr, "m_ptJetlibStream->HP_CurrentBufferLen = %ld \n", m_ptJetlibStream->HP_CurrentBufferLen); |
| 406 | clearStream(); |
| 407 | fprintf(stderr, "b4 HP_JR3BeginImage_1\n"); |
| 408 | |
| 409 | /*wCoordinate = (unsigned short)(((cups_header.cupsWidth * 200) / 254) - 200); |
| 410 | dwNumBytesAligned = (unsigned long)wCoordinate * 3; |
| 411 | dwRemainder = dwNumBytesAligned % 96; |
| 412 | if (dwRemainder != 0) |
| 413 | { |
| 414 | dwNumBytes = dwNumBytesAligned + (96 - dwRemainder); |
| 415 | dwNumBytesAligned = dwNumBytes / 3; |
| 416 | wCoordinate = (unsigned short)dwNumBytesAligned; |
| 417 | } |
| 418 | wCoordinate = cups_header.cupsWidth; |
| 419 | fprintf(stderr, "wCoordinate = %d \n", wCoordinate);*/ |
| 420 | HP_JR3BeginImage_1(m_ptJetlibStream, cups_header.cupsWidth, cups_header.cupsHeight, 1, cups_header.cupsHeight, hp_JetReadyVersion30, eJR_Gray, 0); |
| 421 | write(fdFax, m_ptJetlibStream->HP_OutBuffer, m_ptJetlibStream->HP_CurrentBufferLen); |
| 422 | fprintf(stderr, "m_ptJetlibStream->HP_CurrentBufferLen = %ld \n", m_ptJetlibStream->HP_CurrentBufferLen); |
| 423 | |
| 424 | fpJPEGBuffer = NULL; |
| 425 | fprintf(stderr, "b4 CompressJPEG \n"); |
| 426 | CompressJPEG(cups_raster, cups_header, &m_QTableInfo, &fpJPEGBuffer, &fBufferSize); |
| 427 | fprintf(stderr, "after CompressJPEG \n"); |
| 428 | fprintf(stderr, "fBufferSize = %ld \n", fBufferSize); |
| 429 | if (fpJPEGBuffer != NULL && fBufferSize > 0) |
| 430 | { |
| 431 | |
| 432 | long startLine = 0, blockHeight = 0, dataLength = 0, dwLocalCompressBmpBitsSize = 0; |
| 433 | dataLength = fBufferSize + 6; |
| 434 | fprintf(stderr, "dataLength = %ld \n", dataLength); |
| 435 | // Create the compressed data array along with the header |
| 436 | pDataArray = new BYTE[dataLength]; |
| 437 | // Zero-out the memory |
| 438 | memset(pDataArray, 0, dataLength); |
| 439 | pDataArrayMoving = pDataArray1 = pDataArray; |
| 440 | // Hack Hack. TODO: Change later |
| 441 | dwLocalCompressBmpBitsSize = fBufferSize; |
| 442 | *pDataArrayMoving++ = 0x21; |
| 443 | *pDataArrayMoving++ = 0x90; |
| 444 | *pDataArrayMoving++ = (BYTE)(255 & (dwLocalCompressBmpBitsSize)); |
| 445 | *pDataArrayMoving++ = (BYTE)(255 & (dwLocalCompressBmpBitsSize = (dwLocalCompressBmpBitsSize >> 8))); |
| 446 | *pDataArrayMoving++ = (BYTE)(255 & (dwLocalCompressBmpBitsSize = (dwLocalCompressBmpBitsSize >> 8))); |
| 447 | *pDataArrayMoving++ = (BYTE)(255 & (dwLocalCompressBmpBitsSize = (dwLocalCompressBmpBitsSize >> 8))); |
| 448 | |
| 449 | memcpy(pDataArrayMoving, fpJPEGBuffer, fBufferSize); |
| 450 | fprintf(stderr, "b4 HP_JR3ReadImage_1 \n"); |
| 451 | clearStream(); |
| 452 | HP_JR3ReadImage_1(m_ptJetlibStream, 0, cups_header.cupsHeight, hp_JetReadyVersion30, 0, dataLength); |
| 453 | write(fdFax, m_ptJetlibStream->HP_OutBuffer, m_ptJetlibStream->HP_CurrentBufferLen); |
| 454 | // Send the data using HP_RawUByteArray |
| 455 | clearStream(); |
| 456 | fprintf(stderr, "b4 HP_RawUByteArray \n"); |
| 457 | HP_RawUByteArray(m_ptJetlibStream, pDataArray1, dataLength); |
| 458 | #ifdef STORE_JPEG |
| 459 | char jepgFileName[MAX_FILE_PATH_LEN]; |
| 460 | memset(jepgFileName, 0, MAX_FILE_PATH_LEN); |
| 461 | snprintf(jepgFileName, sizeof(jepgFileName), "%s/hp_fax_page%d.jpg", CUPS_TMP_DIR, iPageNum); |
| 462 | FILE *pFile = fopen(jepgFileName, "w+b"); |
| 463 | if (pFile) |
| 464 | fwrite(fpJPEGBuffer, sizeof(BYTE), fBufferSize, pFile); |
| 465 | if (pFile) |
| 466 | fclose(pFile); |
| 467 | #endif |
| 468 | |
| 469 | fprintf(stderr, "m_ptJetlibStream->HP_CurrentBufferLen = %ld \n", m_ptJetlibStream->HP_CurrentBufferLen); |
| 470 | fprintf(stderr, "after HP_RawUByteArray \n"); |
| 471 | write(fdFax, m_ptJetlibStream->HP_OutBuffer, m_ptJetlibStream->HP_CurrentBufferLen); |
| 472 | // write(fdFax, pDataArray1, dataLength); |
| 473 | if (pDataArray != NULL) |
| 474 | { |
| 475 | free(pDataArray); |
| 476 | pDataArray = NULL; |
| 477 | pDataArray1 = NULL; |
| 478 | pDataArrayMoving = NULL; |
| 479 | } |
| 480 | if (fpJPEGBuffer != NULL) |
| 481 | { |
| 482 | free(fpJPEGBuffer); |
| 483 | fpJPEGBuffer = NULL; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | write(fdFax, JREndPageSeq, sizeof(JREndPageSeq)); |
| 488 | |
| 489 | } /* end while (1) */ |
| 490 | |
| 491 | //lseek(fdFax, 9, SEEK_SET); |
| 492 | fprintf(stderr, "end of compress data \n"); |
| 493 | BUGOUT: |
| 494 | return status; |
| 495 | } |
| 496 | |
| 497 | void clearStream() |
| 498 | { |
| 499 | if (m_ptJetlibStream) |
| 500 | { |
| 501 | memset(m_ptJetlibStream->HP_OutBuffer, 0, m_ptJetlibStream->HP_OutBufferMaxSize); |
| 502 | m_ptJetlibStream->HP_CurrentBufferLen = 0; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | int send_data_to_stdout(int fromFD) |
| 507 | { |
| 508 | int iSize, i; |
| 509 | int len; |
| 510 | BYTE *pTmp = NULL; |
| 511 | |
| 512 | iSize = lseek(fromFD, 0, SEEK_END); |
| 513 | lseek(fromFD, 0, SEEK_SET); |
| 514 | |
| 515 | // DBG("hpcupsfax: lseek(fromFD) returned %d", iSize); |
| 516 | if (iSize > 0) |
| 517 | { |
| 518 | pTmp = (BYTE *)malloc(iSize); |
| 519 | } |
| 520 | if (pTmp == NULL) |
| 521 | { |
| 522 | iSize = 1024; |
| 523 | pTmp = (BYTE *)malloc(iSize); |
| 524 | if (pTmp == NULL) |
| 525 | { |
| 526 | return 1; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | while ((len = read(fromFD, pTmp, iSize)) > 0) |
| 531 | { |
| 532 | write(STDOUT_FILENO, pTmp, len); |
| 533 | } |
| 534 | free(pTmp); |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | int createTempFile(char *szFileName, FILE **pFilePtr) |
| 540 | { |
| 541 | int iFD = -1; |
| 542 | |
| 543 | if (szFileName == NULL || szFileName[0] == '\0' || pFilePtr == NULL) |
| 544 | { |
| 545 | // BUG("Invalid Filename/ pointer\n"); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | if (strstr(szFileName, "XXXXXX") == NULL) |
| 550 | strcat(szFileName, "_XXXXXX"); |
| 551 | |
| 552 | iFD = mkstemp(szFileName); |
| 553 | if (-1 == iFD) |
| 554 | { |
| 555 | // BUG("Failed to create the temp file Name[%s] errno[%d : %s]\n",szFileName,errno,strerror(errno)); |
| 556 | return 0; |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | *pFilePtr = fdopen(iFD, "w+"); |
| 561 | } |
| 562 | |
| 563 | return iFD; |
| 564 | } |
| 565 | |
| 566 | int main(int argc, char **argv) |
| 567 | { |
| 568 | int status = 0; |
| 569 | int fd = 0; |
| 570 | int fdFax = -1; |
| 571 | int i = 0; |
| 572 | FILE *pFilePtrFax; |
| 573 | cups_raster_t *cups_raster; |
| 574 | ppd_file_t *ppd; |
| 575 | |
| 576 | GetLogLevel(); |
| 577 | openlog("hpcupscdmfax", LOG_PID, LOG_DAEMON); |
| 578 | |
| 579 | if (argc < 6 || argc > 7) |
| 580 | { |
| 581 | BUG("ERROR: %s job-id user title copies options [file]\n", *argv); |
| 582 | return 1; |
| 583 | } |
| 584 | |
| 585 | if (argc == 7) |
| 586 | { |
| 587 | if ((fd = open(argv[6], O_RDONLY)) == -1) |
| 588 | { |
| 589 | BUG("ERROR: Unable to open raster file %s\n", argv[6]); |
| 590 | return 1; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | while (argv[i] != NULL) |
| 595 | { |
| 596 | DBG("hpcupscdmfax: argv[%d] = %s\n", i, argv[i]); |
| 597 | i++; |
| 598 | } |
| 599 | |
| 600 | snprintf(hpFileName, sizeof(hpFileName), "%s/hp_%s_fax_Log_XXXXXX", CUPS_TMP_DIR, argv[2]); |
| 601 | |
| 602 | fdFax = createTempFile(hpFileName, &pFilePtrFax); |
| 603 | if (fdFax < 0) |
| 604 | { |
| 605 | BUG("ERROR: Unable to open Fax output file - %s for writing\n", hpFileName); |
| 606 | return 1; |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | chmod(hpFileName, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 611 | } |
| 612 | cups_raster = cupsRasterOpen(fd, CUPS_RASTER_READ); |
| 613 | if (cups_raster == NULL) |
| 614 | { |
| 615 | // dbglog("cupsRasterOpen failed, fd = %d\n", fd); |
| 616 | if (fd != 0) |
| 617 | { |
| 618 | close(fd); |
| 619 | } |
| 620 | // closeFilter(); |
| 621 | return 1; |
| 622 | } |
| 623 | char szEnumField[10] = "", szCountField[10] = "", *szStringField = NULL, szField[128] = ""; |
| 624 | m_ulCookie = 0; |
| 625 | m_ptJetlibStream = HP_NewStream(MAX_BUFF_SIZE, m_ulCookie); |
| 626 | //this part will be taken care in cdmfax.py |
| 627 | /* |
| 628 | clearStream(); |
| 629 | HP_BeginSession_4(m_ptJetlibStream, 200, 200, HP_eInch, HP_eBackChAndErrPage, HP_eSendToFax); |
| 630 | write(fdFax, m_ptJetlibStream->HP_OutBuffer, (int)m_ptJetlibStream->HP_CurrentBufferLen); |
| 631 | fprintf(stderr, "m_ptJetlibStream->HP_CurrentBufferLen = %ld \n", m_ptJetlibStream->HP_CurrentBufferLen); |
| 632 | |
| 633 | clearStream(); |
| 634 | HP_OpenDataSource_1(m_ptJetlibStream, HP_eDefaultDataSource, HP_eBinaryLowByteFirst); |
| 635 | write(fdFax, m_ptJetlibStream->HP_OutBuffer, (int)m_ptJetlibStream->HP_CurrentBufferLen); |
| 636 | fprintf(stderr, "m_ptJetlibStream->HP_CurrentBufferLen = %ld \n", m_ptJetlibStream->HP_CurrentBufferLen); |
| 637 | */ |
| 638 | status = ProcessRasterData(cups_raster, fdFax); |
| 639 | //this part will be taken care in cdmfax.py |
| 640 | /* |
| 641 | write(fdFax, JRCloseDataSourceSeq, sizeof(JRCloseDataSourceSeq)); |
| 642 | write(fdFax, JREndSessionSeq, sizeof(JREndSessionSeq)); |
| 643 | */ |
| 644 | //write(fdFax,PJLExit,sizeof(PJLExit)); |
| 645 | |
| 646 | cupsRasterClose(cups_raster); |
| 647 | |
| 648 | // DBG("hpcupscdmfax: Send data to stdout \n"); |
| 649 | status = send_data_to_stdout(fdFax); |
| 650 | |
| 651 | if (fd != 0) |
| 652 | { |
| 653 | close(fd); |
| 654 | } |
| 655 | |
| 656 | if (fdFax > 0) |
| 657 | { |
| 658 | close(fdFax); |
| 659 | unlink(hpFileName); |
| 660 | } |
| 661 | |
| 662 | return status; |
| 663 | } |