ilcodec.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 libraries 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. #include <stdlib.h>
  40. #include "ilint.h"
  41. #include "ilpipelem.h"
  42. #include "ilpipeint.h"
  43. #include "ilcodec.h"
  44. #include "ilerrors.h"
  45. /* ------------------------ ilReallocCompressedBuffer ------------------------ */
  46. /* Realloc (or alloc the first time) the pixel buffer for plane "plane" of the
  47. compressed image "*pImage", so that its "bufferSize" is a minimum of
  48. "minNewSize" bytes in size.
  49. */
  50. IL_PRIVATE ilBool _ilReallocCompressedBuffer (
  51. ilImageInfo *pImage,
  52. unsigned int plane,
  53. unsigned long minNewSize
  54. )
  55. {
  56. ilImagePlaneInfo *pPlane;
  57. pPlane = &pImage->plane[plane];
  58. pPlane->bufferSize = minNewSize + 10000; /* A GUESS - DO SOMETHING SMARTER !!!!! */
  59. if (!pPlane->pPixels) pPlane->pPixels = (ilPtr)IL_MALLOC (pPlane->bufferSize);
  60. else pPlane->pPixels = (ilPtr)IL_REALLOC (pPlane->pPixels, pPlane->bufferSize);
  61. if (!pPlane->pPixels) {
  62. pPlane->bufferSize = 0;
  63. return FALSE;
  64. }
  65. return TRUE;
  66. }
  67. /* ------------------------ ilCopyCompressedExecute ------------------------ */
  68. /* Execute() function for ilInsertCompressedCopyFilter() to copy compressed images.
  69. Copies one strip of compressed data.
  70. */
  71. static ilError ilCopyCompressedExecute (
  72. ilExecuteData *pData,
  73. long dstLine,
  74. long *pNLines
  75. )
  76. {
  77. ilImagePlaneInfo *pSrcPlane, *pDstPlane;
  78. long nBytes, dstOffset, requiredBufferSize;
  79. nBytes = pData->compressed.nBytesToRead; /* # of bytes to write */
  80. pSrcPlane = &pData->pSrcImage->plane[0];
  81. if (!pSrcPlane->pPixels || (nBytes <= 0)) /* nothing to copy; exit */
  82. return IL_OK;
  83. dstOffset = *pData->compressed.pDstOffset; /* byte offset into dst buffer */
  84. pDstPlane = &pData->pDstImage->plane[0];
  85. requiredBufferSize = nBytes + dstOffset; /* # bytes needed in dst buffer */
  86. /* Check for space in output buffer; realloc/malloc if not enough */
  87. if (requiredBufferSize > pDstPlane->bufferSize) {
  88. pDstPlane->pPixels = (pDstPlane->pPixels) ?
  89. (ilPtr)IL_REALLOC (pDstPlane->pPixels, requiredBufferSize) :
  90. (ilPtr)IL_MALLOC (requiredBufferSize);
  91. if (!pDstPlane->pPixels) {
  92. pDstPlane->bufferSize = 0;
  93. return IL_ERROR_MALLOC;
  94. }
  95. pDstPlane->bufferSize = requiredBufferSize;
  96. }
  97. /* Copy nBytes from src to dst buffer, using offsets from *pData */
  98. bcopy ((char *)(pSrcPlane->pPixels + pData->compressed.srcOffset),
  99. (char *)(pDstPlane->pPixels + dstOffset), nBytes);
  100. *pData->compressed.pNBytesWritten = nBytes;
  101. return IL_OK;
  102. }
  103. /* ------------------- ilInsertCompressedCopyFilter ------------------------- */
  104. /* Insert a "filter" which copies the compressed pipe image to the dest image.
  105. This is the equivalent to ilInsertCopyFilter(), except for compressed images.
  106. */
  107. IL_PRIVATE ilBool _ilInsertCompressedCopyFilter (
  108. ilPipe pipe
  109. )
  110. {
  111. /* Add a filter which copies src to dst. No private needed; the Execute()
  112. function does all necessary setup.
  113. */
  114. return (ilAddPipeElement (pipe, IL_FILTER, 0, 0,
  115. (ilSrcElementData *)NULL, (ilDstElementData *)NULL,
  116. IL_NPF, IL_NPF, IL_NPF, ilCopyCompressedExecute, NULL, 0)) ? TRUE : FALSE;
  117. }