home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / xlate / xlateimg.c < prev   
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.6 KB  |  289 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20. ** All the wonderful code for dealing with images
  21. */
  22. #define JMC_INIT_PSIMGCB_ID
  23.  
  24. #include "xlate_i.h"
  25. #include "libimg.h"             /* Image Library public API. */
  26. #include "il_util.h"            /* Colormap/Colorspace API. */
  27.  
  28. /* Create and initialize the Image Library JMC callback interface.
  29.    Also create an IL_GroupContext for the current context. */
  30. XP_Bool
  31. psfe_init_image_callbacks(MWContext *cx)
  32. {
  33.     IL_GroupContext *img_cx;
  34.     PSIMGCB* img_cb;
  35.     JMCException *exc = NULL;
  36.  
  37.     if (!cx->img_cx) {
  38.         img_cb = PSIMGCBFactory_Create(&exc); /* JMC Module */
  39.         if (exc) {
  40.             JMC_DELETE_EXCEPTION(&exc); /* XXXM12N Should really return
  41.                                            exception */
  42.             return FALSE;
  43.         }
  44.  
  45.         /* Create an Image Group Context.  IL_NewGroupContext augments the
  46.            reference count for the JMC callback interface.  The opaque argument
  47.            to IL_NewGroupContext is the Front End's display context, which will
  48.            be passed back to all the Image Library's FE callbacks. */
  49.         img_cx = IL_NewGroupContext((void*)cx, (IMGCBIF *)img_cb);
  50.  
  51.         /* Attach the IL_GroupContext to the FE's display context. */
  52.         cx->img_cx = img_cx;
  53.     }
  54.     return TRUE;
  55. }
  56.  
  57. /*****************************************************************************/
  58. /*                       Image Library callbacks                             */
  59. /*****************************************************************************/
  60.  
  61. #define HOWMANY(x, r)     (((x) + ((r) - 1)) / (r))
  62. #define ROUNDUP(x, r)     (HOWMANY(x, r) * (r))
  63.  
  64. JMC_PUBLIC_API(void)
  65. _PSIMGCB_init(struct PSIMGCB* self, JMCException* *exception)
  66. {
  67.     /* Nothing to be done here. */
  68. }
  69.  
  70. extern JMC_PUBLIC_API(void*)
  71. _PSIMGCB_getBackwardCompatibleInterface(struct PSIMGCB* self,
  72.                                         const JMCInterfaceID* iid,
  73.                                         JMCException* *exception)
  74. {
  75.     return NULL;                /* Nothing to be done here. */
  76. }
  77.  
  78.  
  79. /**************************** Pixmap creation ********************************/
  80. JMC_PUBLIC_API(void)
  81. _PSIMGCB_NewPixmap(PSIMGCB* img_cb, jint op, void *dpy_cx, jint width,
  82.                    jint height, IL_Pixmap *image, IL_Pixmap *mask)
  83. {
  84.     uint8 img_depth, pixmap_depth;
  85.     NI_PixmapHeader *img_header = &image->header;
  86.     NI_PixmapHeader *mask_header = mask ? &mask->header : NULL;
  87.     MWContext *context = (MWContext *)dpy_cx; /* XXXM12N This should be the
  88.                                                  FE's display context. */
  89.  
  90.  
  91.     /* Determine the depth of the image. */
  92.     pixmap_depth = context->color_space->pixmap_depth;
  93.  
  94.     /* Override the image colorspace with the display colorspace.  This
  95.        instructs the image library to decode to the display colorspace
  96.        instead of decoding to the image's source colorspace. */
  97.     IL_ReleaseColorSpace(img_header->color_space);
  98.     img_header->color_space = context->color_space;
  99.     IL_AddRefToColorSpace(img_header->color_space);
  100.  
  101.     /* Ask the image library to scale to the requested dimensions.
  102.        Context-specific scaling, however, will be handled by the PSFE. */
  103.     img_header->width = width;
  104.     img_header->height = height;
  105.     if (mask) {
  106.         mask_header->width = width;
  107.         mask_header->height = height;
  108.     }
  109.     
  110.     /* Compute the number of bytes per scan line for the image and mask,
  111.        and make sure it is quadlet aligned. */
  112.     img_depth = img_header->color_space->pixmap_depth;
  113.     XP_ASSERT(img_depth == pixmap_depth);
  114.     img_header->widthBytes = (img_header->width * img_depth + 7) / 8;
  115.     img_header->widthBytes = ROUNDUP(img_header->widthBytes, 4);
  116.     if (mask) {
  117.         mask_header->widthBytes = (mask_header->width + 7) / 8;
  118.         mask_header->widthBytes = ROUNDUP(mask_header->widthBytes, 4);
  119.     }
  120.  
  121.     /* Allocate memory for the image bits, and for the mask bits (if
  122.        required.) */
  123.     image->bits = calloc(img_header->widthBytes * img_header->height, 1);
  124.     if (!image->bits)
  125.         return;
  126.     if (mask) {
  127.         mask->bits = calloc(mask_header->widthBytes * mask_header->height, 1);
  128.         if (!mask->bits) {
  129.             free(image->bits);
  130.             image->bits = NULL;
  131.             return;
  132.         }
  133.     }
  134. }
  135.  
  136.  
  137. /**************************** Pixmap update **********************************/
  138. JMC_PUBLIC_API(void)
  139. _PSIMGCB_UpdatePixmap(PSIMGCB* img_cb, jint op, void* dpy_cx,
  140.                       IL_Pixmap* pixmap, jint x_offset, jint y_offset,
  141.                       jint width, jint height)
  142. {
  143. }
  144.  
  145.  
  146. /************************** Pixmap memory management *************************/
  147. JMC_PUBLIC_API(void)
  148. _PSIMGCB_ControlPixmapBits(PSIMGCB* img_cb, jint op, void* dpy_cx,
  149.                            IL_Pixmap* pixmap, IL_PixmapControl message)
  150. {
  151. }
  152.  
  153.  
  154. /**************************** Pixmap destruction *****************************/
  155. /* XXXM12N The dpy_cx argument is not used in DestroyPixmap and should be
  156.    removed. */
  157. JMC_PUBLIC_API(void)
  158. _PSIMGCB_DestroyPixmap(PSIMGCB* img_cb, jint op, void* dpy_cx,
  159.                        IL_Pixmap* pixmap)
  160. {
  161.     /* Free the pixmap's bits. */
  162.     if (pixmap->bits) {
  163.         free(pixmap->bits);
  164.         pixmap->bits = NULL;
  165.     }
  166. }
  167.  
  168.  
  169. /**************************** Pixmap display *********************************/
  170. JMC_PUBLIC_API(void)
  171. _PSIMGCB_DisplayPixmap(PSIMGCB* img_cb, jint op, void* dpy_cx,
  172.                        IL_Pixmap* image, IL_Pixmap* mask, jint x, jint y,
  173.                        jint x_offset, jint y_offset, jint width, jint height)
  174. {
  175.     int32 img_x_offset, img_y_offset;   /* Offset of image in drawable. */
  176.     int32 rect_x_offset, rect_y_offset; /* Offset of update rect in
  177.                                            drawable. */
  178.     NI_PixmapHeader *img_header = &image->header;
  179.     uint32 img_width = img_header->width;     /* Image width. */
  180.     uint32 img_height = img_header->height;   /* Image height. */
  181.     MWContext *context = (MWContext *)dpy_cx; /* XXX This should be the FE's
  182.                                                  display context. */
  183.     XP_Bool tiling_required = FALSE;
  184.  
  185.     /* Check for zero display area. */
  186.     if (width == 0 || height == 0)
  187.         return;
  188.  
  189.     /* Perform context scaling. */
  190.     x *= context->convertPixX;
  191.     y *= context->convertPixY;
  192.     x_offset *= context->convertPixX;
  193.     y_offset *= context->convertPixY;
  194.     width *= context->convertPixX;
  195.     height *= context->convertPixY;
  196.     img_width *= context->convertPixX;
  197.     img_height *= context->convertPixY;
  198.  
  199.     /* Determine whether tiling is required. */
  200.     if ((x_offset + width > img_width) || (y_offset + height > img_height))
  201.         tiling_required = TRUE;
  202.  
  203.     /* Compute the offset into the drawable of the image origin. */
  204.     img_x_offset = x; /* + fe_drawable->x_origin; */
  205.     img_y_offset = y; /* + fe_drawable->y_origin; */
  206.  
  207.     /* Check whether the image is ready to be displayed. */
  208.     if (!XP_CheckElementSpan(context, img_y_offset, img_height))
  209.         return;
  210.  
  211.     /* Draw the image. */
  212.     xl_colorimage(context, x, y, width, height, image, mask);
  213. }
  214.  
  215.  
  216. /**************************** Icon dimensions ********************************/
  217. JMC_PUBLIC_API(void)
  218. _PSIMGCB_GetIconDimensions(PSIMGCB* img_cb, jint op, void* dpy_cx, int* width,
  219.                            int* height, jint icon_number)
  220. {
  221.     /* Call the screen FE to get the icon dimensions in pixels. */
  222.     FE_GetPSIconDimensions(icon_number, width, height);
  223. }
  224.  
  225.  
  226. /**************************** Icon display ***********************************/
  227. JMC_PUBLIC_API(void)
  228. _PSIMGCB_DisplayIcon(PSIMGCB* img_cb, jint op, void* dpy_cx, jint x, jint y,
  229.                      jint icon_number)
  230. {
  231.     int width;       /* Width of the icon in context scaled coordinates. */
  232.     int height;      /* Height of the icon in context scaled coordinates. */
  233.     int img_depth;   /* Depth of the image. */
  234.     MWContext *context = (MWContext *)dpy_cx;
  235.     IL_Pixmap image; /* Image pixmap. */
  236.     NI_PixmapHeader *img_header = &image.header;
  237.  
  238.     /* Call the screen FE to get the icon dimensions in pixels. */
  239.     FE_GetPSIconDimensions(icon_number, &width, &height);
  240.     img_header->width = (int32)width;
  241.     img_header->height = (int32)height;
  242.  
  243.     /* Apply context scaling to the dimensions. */
  244.     width *= context->convertPixX;
  245.     height *= context->convertPixY;
  246.  
  247.     /* Check whether we are ready to display the icon. */
  248.     if (!XP_CheckElementSpan(context, y, height))
  249.         return;
  250.  
  251.     /* Don't attempt to display the icon if either dimension is zero. */
  252.     if (!width || !height)
  253.         return;
  254.  
  255.     /* Get ourselves a colorspace. */
  256.     img_header->color_space = context->color_space;
  257.     IL_AddRefToColorSpace(img_header->color_space);
  258.  
  259.     /* Allocate bits for the icon.  We don't cache icons created for the
  260.        purpose of printing. */
  261.     img_depth = img_header->color_space->pixmap_depth;
  262.     img_header->widthBytes = (img_header->width * img_depth + 7) / 8;
  263.     img_header->widthBytes = ROUNDUP(img_header->widthBytes, 4);
  264.  
  265.     /* Allocate memory for the image bits. */
  266.     image.bits = calloc(img_header->widthBytes * img_header->height, 1);
  267.     if (!image.bits) {
  268.         IL_ReleaseColorSpace(img_header->color_space);
  269.         return;
  270.     }
  271.  
  272.     /* Get the icon data from the screen FE and draw the icon. */
  273.     if (FE_GetPSIconData(icon_number, &image, NULL))
  274.         xl_colorimage(context, x, y, width, height, &image, NULL);
  275.     
  276.     /* Clean up. */
  277.     free(image.bits);
  278.     IL_ReleaseColorSpace(img_header->color_space);
  279. }
  280.  
  281. /*****************************************************************************/
  282. /*                       End of Image Library callbacks                      */
  283. /*****************************************************************************/
  284.  
  285.  
  286.  
  287.  
  288.  
  289.