ilreadimage.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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: ilreadimage.c /main/3 1995/10/23 15:59:31 rswiston $ */
  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 "ilint.h"
  40. #include "ilpipelem.h"
  41. #include "ilpipeint.h"
  42. #include "ilimage.h"
  43. #include "ilerrors.h"
  44. /* Private structure for ilRCIExecute(). (RCI = "Read Compressed Image") */
  45. typedef struct {
  46. ilImagePtr pImage; /* ptr to src image */
  47. long strip; /* Init(): index of next strip to write */
  48. long nLinesWritten; /* Init(): # of lines written so far */
  49. } ilRCIPrivRec, *ilRCIPrivPtr;
  50. /* ------------------------- ilRCIInit ----------------------------------- */
  51. /* Init() function for ilRCI().
  52. */
  53. static ilError ilRCIInit (
  54. ilRCIPrivPtr pPriv,
  55. ilImageInfo *pSrcImage,
  56. ilImageInfo *pDstImage
  57. )
  58. {
  59. pPriv->strip = 0; /* init strip index, for first strip */
  60. pPriv->nLinesWritten = 0;
  61. return IL_OK;
  62. }
  63. /* --------------------- ilRCIExecute -------------------------- */
  64. /* Execute() for ilReadImage() producer when the image is compressed.
  65. */
  66. static ilError ilRCIExecute (
  67. register ilExecuteData *pData,
  68. long dstLine, /* ignored */
  69. long *pNLines /* ignored on input */
  70. )
  71. {
  72. register ilRCIPrivPtr pPriv;
  73. register ilImagePtr pImage;
  74. long offset;
  75. pPriv = (ilRCIPrivPtr)pData->pPrivate;
  76. pImage = pPriv->pImage;
  77. /* Return error if image has never been written to. */
  78. if (!pImage->pStripOffsets || !pImage->i.plane[0].pPixels)
  79. return IL_ERROR_NULL_COMPRESSED_IMAGE;
  80. /* "write" current strip by patching next filter's offset to offset for
  81. current strip, and setting its # bytes to size of this strip.
  82. */
  83. offset = pImage->pStripOffsets[pPriv->strip];
  84. *pData->compressed.pDstOffset = offset;
  85. pPriv->strip++;
  86. *pData->compressed.pNBytesWritten = pImage->pStripOffsets[pPriv->strip] - offset;
  87. /* If last strip, # of lines is remaining, else is strip height */
  88. if ((pPriv->nLinesWritten + pImage->stripHeight) > pImage->i.height) {
  89. *pNLines = pImage->i.height - pPriv->nLinesWritten;
  90. return IL_ERROR_LAST_STRIP;
  91. }
  92. pPriv->nLinesWritten += pImage->stripHeight;
  93. *pNLines = pImage->stripHeight;
  94. return IL_OK;
  95. }
  96. /* ------------------------ ilReadImage ---------------------------------- */
  97. /* Public function: see spec.
  98. Adds the given image as a producer of the pipe.
  99. */
  100. ilBool ilReadImage (
  101. ilPipe pipe,
  102. ilObject image
  103. )
  104. {
  105. register ilImagePtr pImage;
  106. register ilRCIPrivPtr pPriv;
  107. /* Validate that pipe and image are such, have same context and pipe empty */
  108. pImage = (ilImagePtr)image;
  109. if (pipe->objectType != IL_PIPE) {
  110. pipe->context->error = IL_ERROR_OBJECT_TYPE;
  111. return FALSE;
  112. }
  113. if ((pImage->o.p.objectType != IL_INTERNAL_IMAGE)
  114. && (pImage->o.p.objectType != IL_CLIENT_IMAGE))
  115. return ilDeclarePipeInvalid (pipe, IL_ERROR_OBJECT_TYPE);
  116. if (pImage->o.p.context != pipe->context)
  117. return ilDeclarePipeInvalid (pipe, IL_ERROR_CONTEXT_MISMATCH);
  118. if (ilGetPipeInfo (pipe, FALSE, (ilPipeInfo *)NULL, (ilImageDes *)NULL,
  119. (ilImageFormat *)NULL) != IL_PIPE_EMPTY) {
  120. if (!pipe->context->error)
  121. ilDeclarePipeInvalid (pipe, IL_ERROR_PIPE_STATE);
  122. return FALSE;
  123. }
  124. /* If uncompressed, add producer image and exit. stripHeight = image height,
  125. need producer throttle filter before next element is added.
  126. */
  127. if (pImage->des.compression == IL_UNCOMPRESSED)
  128. return _ilAddProducerImage (pipe, pImage, IL_PIPE_IMAGE, pImage->i.height,
  129. pImage->i.height, TRUE, TRUE);
  130. /* Image is compressed: if no stripHeight yet, allocate strip offsets array using
  131. default strip height. Add producer image: constant strips the size of the
  132. image's strip height. Add producer throttle to feed each strip.
  133. */
  134. if (!pImage->pStripOffsets)
  135. if (!_ilAllocStripOffsets (pImage, 0))
  136. return FALSE;
  137. if (!_ilAddProducerImage (pipe, pImage, IL_PIPE_IMAGE, pImage->i.height,
  138. pImage->stripHeight, TRUE, FALSE))
  139. return FALSE;
  140. pPriv = (ilRCIPrivPtr)ilAddPipeElement (pipe, IL_FILTER, sizeof (ilRCIPrivRec),
  141. IL_ADD_PIPE_NO_DST, (ilSrcElementData *)NULL, (ilDstElementData *)NULL,
  142. ilRCIInit, IL_NPF, IL_NPF, ilRCIExecute, 0);
  143. if (!pPriv)
  144. return FALSE;
  145. pPriv->pImage = pImage;
  146. return TRUE;
  147. }