home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libimg / src / if.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  19.5 KB  |  506 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. /*   if.h --- Top-level image library internal routines
  20.  *
  21.  * $Id: if.h,v 3.1 1998/03/28 03:35:01 ltabb Exp $
  22.  */
  23.  
  24. #ifndef _if_h
  25. #define _if_h
  26.  
  27. #define M12N
  28.  
  29. #define IL_INTERNAL
  30.  
  31. #include "xp_mcom.h"            /* XP definitions and types. */
  32. #include "ntypes.h"             /* typedefs for commonly used Netscape data
  33.                                    structures */
  34. #include "net.h"
  35.  
  36. typedef struct _IL_GroupContext IL_GroupContext;
  37. typedef struct _IL_ImageReq IL_ImageReq;
  38. typedef struct il_context_list il_context_list;
  39. typedef struct il_container_list il_container_list;
  40. typedef struct il_container_struct il_container;
  41.  
  42. #include "il_icons.h"           /* Image icons. */
  43. #include "libimg.h"             /* Public API to Image Library. */
  44. #include "il_utilp.h"           /* Private header for colormap/colorspaces. */
  45. #include "il_util.h"            /* Public API for colormaps/colorspaces. */
  46.  
  47. /*****************************XXXM12N Get rid of this. ***********************/
  48.  
  49. typedef void
  50. (*TimeoutCallbackFunction) (void * closure);
  51.  
  52. extern void * 
  53. FE_SetTimeout(TimeoutCallbackFunction func, void * closure, uint32 msecs);
  54. extern void
  55. FE_ClearTimeout(void *timer_id);
  56.  
  57. /*********************************XXXM12N*************************************/
  58.  
  59. #include "il.h"
  60.  
  61. /* For debugging counts of loading, looping and aborted images, needed for
  62.    group observer notification. */
  63. /* #define DEBUG_GROUP_OBSERVER */
  64.  
  65. #ifdef DEBUG
  66. #ifndef XP_MAC
  67. #define Debug 1
  68. #else
  69. extern int Debug;
  70. #endif
  71. #endif
  72.  
  73. #ifdef XP_WIN
  74. #define _USD 1              /* scanlines upside-down */ 
  75. #endif
  76.  
  77. #ifdef DEBUG
  78. #define ILTRACE(l,t) { if(il_debug>l) {XP_TRACE(t);} }
  79. #else
  80. #define ILTRACE(l,t) {}
  81. #endif
  82.  
  83. #define FREE_IF_NOT_NULL(x)    do {if (x) {XP_FREE(x); (x) = NULL;}} while (0)
  84.  
  85. #ifndef NSPR20
  86. #include "prosdep.h"  /* for IS_LITTLE_ENDIAN / IS_BIG_ENDIAN */
  87. #else
  88. #include "prtypes.h"  /* for IS_LITTLE_ENDIAN / IS_BIG_ENDIAN */
  89. #endif
  90.  
  91. /* The imagelib labels bits in a 32-bit word from 31 on the left to 0 on the right.
  92.    This macro performs the necessary conversion to make that definition work on
  93.    little-endian platforms */
  94. #if defined(IS_LITTLE_ENDIAN)
  95. #    define M32(bit)  ((bit) ^ 0x18)
  96. #elif defined(IS_BIG_ENDIAN)
  97. #    define M32(bit)  (bit)
  98. #else
  99.      ENDIANNESS UNKNOWN!
  100. #endif
  101.  
  102. /* Don't change these unless you know what you're doing or you will
  103.    break 16-bit binaries. */
  104. #define MAX_IMAGE_WIDTH  8000
  105. #define MAX_IMAGE_HEIGHT 8000
  106.  
  107. /* MAX and MIN are almost universal, but be paranoid and use our
  108.    definitions. */
  109. #ifdef MAX
  110. #undef MAX
  111. #endif
  112. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  113.  
  114. #ifdef MIN
  115. #undef MIN
  116. #endif
  117. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  118.  
  119. /* Last output pass of an image */
  120. #define IL_FINAL_PASS     -1
  121.  
  122. /* Nature of the conversion from source image colorspace to target image
  123.    colorspace. */
  124. typedef enum _IL_ConversionType {
  125.     IL_TrueToTrue     = 0x09,
  126.     IL_TrueToPseudo   = 0x0a,
  127.     IL_TrueToGrey     = 0x0c,
  128.     IL_PseudoToTrue   = 0x11,
  129.     IL_PseudoToPseudo = 0x12,
  130.     IL_PseudoToGrey   = 0x14,
  131.     IL_GreyToTrue     = 0x21,
  132.     IL_GreyToPseudo   = 0x22,
  133.     IL_GreyToGrey     = 0x24
  134. } IL_ConversionType;
  135.  
  136. typedef void (*il_converter)(il_container *ic, const uint8 *mask, 
  137.                              const uint8 *sp, int x_offset,
  138.                              int num, void XP_HUGE *out);
  139.  
  140. enum icstate {
  141.     IC_VIRGIN       = 0x00, /* Newly-created container */
  142.     IC_START        = 0x01, /* Requested stream from netlib, but no data yet */
  143.     IC_STREAM       = 0x02, /* Stream opened, but insufficient data
  144.                                received to determine image size  */ 
  145.     IC_SIZED        = 0x04, /* Image size determined - still loading */
  146.     IC_MULTI        = 0x06, /* Same as IC_SIZED, but for second or
  147.                                subsequent images in multipart MIME */
  148.     IC_NOCACHE      = 0x11, /* Image deferred for loading later */
  149.     IC_COMPLETE     = 0x20, /* Image loaded - no errors */
  150.     IC_BAD          = 0x21, /* Corrupt or illegal image data */
  151.     IC_INCOMPLETE   = 0x22, /* Partially loaded image data */
  152.     IC_MISSING      = 0x23, /* No such file on server */
  153.     IC_ABORT_PENDING= 0x24  /* Image download abort in progress */
  154. };
  155.  
  156. /* Still receiving data from the netlib ? */
  157. #define IMAGE_CONTAINER_ACTIVE(ic)  ((ic)->state <= IC_MULTI)
  158.  
  159. /* Force memory cache to be flushed ? */
  160. #define FORCE_RELOAD(reload_method)                                           \
  161.  (((reload_method)==NET_NORMAL_RELOAD) || ((reload_method)==NET_SUPER_RELOAD))
  162.  
  163. /* Simple list of image contexts. */
  164. struct il_context_list {
  165.     IL_GroupContext *img_cx;
  166.     struct il_context_list *next;
  167. };
  168.  
  169. /* Simple list of image containers. */
  170. struct il_container_list {
  171.     il_container *ic;
  172.     struct il_container_list *next;
  173. };
  174.  
  175.  
  176. /* There is one il_container per real image */
  177. struct il_container_struct {
  178.     il_container *next;         /* Cache bidirectional linked list */
  179.     il_container *prev;
  180.  
  181.     IL_URL *url;
  182.     char *url_address;          /* Same as url->address if there is no redirection*/
  183.  
  184.     uint32 hash;
  185.     uint32 urlhash;
  186.     
  187.     enum icstate state;
  188.     int sized;
  189.  
  190.     int is_alone;               /* only image on a page */
  191.     int is_in_use;              /* Used by some context */
  192.     int32 loop_count;           /* Remaining number of times to repeat image,
  193.                                    -1 means loop infinitely */
  194.     int is_looping;             /* TRUE if this is the second or subsequent
  195.                                    pass of an animation. */
  196.     int is_aborted;             /* True if aborted by NetLib. */
  197.     PRPackedBool is_url_loading;/* TRUE if NetLib is currently loading the URL. */
  198.     int is_multipart;           /* TRUE if stream is known to be multipart. */
  199.     int multi;                  /* Known to be either multipart-MIME 
  200.                                    or multi-image format */
  201.     int new_data_for_fe;        /* Any Scanlines that FE doesn't know about ? */
  202.     int update_start_row;       /* Scanline range to send to FE */
  203.     int update_end_row;
  204.  
  205.     uint32 bytes_consumed;      /* Bytes read from the stream so far */
  206.  
  207.     NI_PixmapHeader *src_header; /* Source image header information. */
  208.     IL_Pixmap *image;           /* Destination image pixmap structure. */
  209.     IL_Pixmap *mask;            /* Destination mask pixmap structure. */
  210.  
  211.     intn type;
  212.     void *ds;                   /* decoder's private data */
  213.  
  214.     il_converter converter;
  215.     void *quantize;             /* quantizer's private data */
  216.  
  217.     int (*write)(il_container *ic, const uint8*, int32);
  218.     void (*complete)(il_container *ic);
  219.     void (*abort)(il_container *ic);
  220.  
  221.     unsigned int (*write_ready)(il_container *ic);
  222.  
  223.     void *row_output_timeout;
  224.     uint8 *scalerow;
  225.     int pass;                   /* pass (scan #) of a multi-pass image.
  226.                                    Used for interlaced GIFs & p-JPEGs */
  227.  
  228.     NET_StreamClass *stream;    /* back pointer to the stream */
  229.  
  230.     int forced;
  231.     uint32 content_length;
  232.  
  233.     int dest_width, dest_height; /* Target dimensions of the image */
  234.     PRPackedBool natural_size;  /* True if the image is decoded to its natural
  235.                                    size. */
  236.     PRPackedBool aspect_distorted; /* True if the image undergoes aspect ratio
  237.                                       distortion during decoding. */
  238.  
  239.     IL_IRGB *background_color;  /* The requested background color for this
  240.                                    image (only applies if the image is
  241.                                    determined to be transparent.)  A mask will
  242.                                    be created for a transparent image only if
  243.                                    no background color was requested. */
  244.     
  245.     char *comment;              /* Human-readable text stored in image */
  246.     int comment_length;
  247.  
  248.     int colormap_serial_num;    /* serial number of last installed colormap */
  249.  
  250.     int dont_use_custom_palette;
  251.     int rendered_with_custom_palette;
  252.     IL_DitherMode dither_mode;  /* ilDither or ilClosestColor */
  253.  
  254.     IL_GroupContext *img_cx;    /* The context in which this image was created.
  255.                                    Used during image decoding only. */
  256.     IL_DisplayType display_type; /* Type of display for which the container
  257.                                     is created. */
  258.     IMGCBIF *img_cb;            /* JMC callback interface. */
  259.     IL_NetContext *net_cx;      /* Context which initiated this transfer. */
  260.  
  261.     IL_ImageReq *clients;       /* List of clients of this container. */
  262.     IL_ImageReq *lclient;       /* Last client in the client list. */
  263.     il_context_list *img_cx_list; /* List of contexts which have clients of
  264.                                      this container. */
  265.  
  266.     IL_Rect displayable_rect;   /* The area of the image pixmap which is in a
  267.                                    displayable state.  Used as a filter
  268.                                    between client calls to IL_DisplaySubImage
  269.                                    and Image Library calls to DisplayPixmap, in
  270.                                    the event that the client requests us to
  271.                                    draw a part of the pixmap that has yet to
  272.                                    be decoded. */
  273.  
  274.     time_t expires;             /* Expiration date for the corresponding URL */
  275.  
  276. #ifdef DEBUG
  277.     time_t start_time;
  278. #endif
  279.     char *fetch_url;            /* actual url address used */
  280. };
  281.  
  282.  
  283. typedef enum { ilUndefined, ilCI, ilGrey, ilRGB } il_mode;
  284.  
  285. typedef enum il_draw_mode 
  286. {
  287.     ilErase,                    /* Transparent areas are reset to background */
  288.     ilOverlay                   /* Transparent areas overlay existing data */
  289. } il_draw_mode;
  290.     
  291.  
  292. /* A context for a group of images. */
  293. struct _IL_GroupContext {
  294.     IMGCBIF *img_cb;            /* JMC callback interface to front ends. */
  295.     void *dpy_cx;               /* An opaque pointer passed back to all
  296.                                    callbacks in the interface vtable. */
  297.  
  298.     IL_DisplayType display_type; /* IL_Console, IL_Printer or IL_PostScript. */
  299.     IL_ColorSpace *color_space;  /* Display colorspace. */
  300.  
  301.     /* Preferences */
  302.     PRPackedBool progressive_display; /* If TRUE, images are displayed while
  303.                                          loading */
  304.     IL_DitherMode dither_mode;  /* IL_ClosestColor, IL_Dither or IL_Auto. */
  305.     int dontscale;              /* Used for Macs, which do their own scaling */
  306.     int nolowsrc;               /* If TRUE, never display LOSRC images */
  307.  
  308.     /* Per-context state */
  309.     il_container_list *container_list;/* List of containers in this context. */
  310.     int32 num_containers;       /* Number of containers in this context. */
  311.     int32 num_loading;          /* Number of containers which are currently
  312.                                    loading. */
  313.     int32 num_looping;          /* Number of containers which are currently
  314.                                    looping i.e. on second or subsequent
  315.                                    iteration of an animation. */
  316.     int32 num_aborted;          /* Number of containers which have aborted
  317.                                    so far. */
  318.     
  319.     XP_ObserverList obs_list;   /* List of observers for this image group. */
  320.  
  321.     struct _IL_GroupContext *next; /* Next entry in a list of image group
  322.                                       contexts. */
  323. };
  324.  
  325.  
  326. /* Tag to indicate whether a request represents an image or an icon. */
  327. typedef enum 
  328. {
  329.     IL_IMAGE,
  330.     IL_ICON
  331. } IL_ImageType;
  332.  
  333.  
  334. /* This is Image Library's internal representation of a client's image request.
  335.    It represents a handle on a specific instance of an image container.  */
  336. struct _IL_ImageReq {
  337.     il_container *ic;           /* The image container for this request (may
  338.                                    be shared with other requests.) */
  339.     IL_ImageType image_type;    /* Image or icon. */
  340.  
  341.     IL_GroupContext *img_cx;    /* The group context to which this request
  342.                                    belongs. */
  343.     IL_NetContext *net_cx;      /* A clone of the net context which the image
  344.                                    library was given when this image handle was
  345.                                    created.  This serves as a backup in case
  346.                                    the image container's net_cx becomes invalid,
  347.                                    (for example, when the client for which the
  348.                                    container was initially created is destroyed.) */                               
  349.     PRPackedBool stopped;       /* TRUE - if user hit "Stop" button */
  350.     int is_view_image;          /* non-zero if client is
  351.                                    internal-external-reconnect */
  352.  
  353.     XP_ObserverList obs_list;   /* List of observers for this request. */
  354.  
  355.     struct _IL_ImageReq *next;  /* Next entry in a list of image requests. */
  356. };
  357.  
  358.  
  359. extern int il_debug;
  360.  
  361. extern void il_delete_container(il_container *ic);
  362. extern il_container *il_removefromcache(il_container *ic);
  363. extern void il_image_abort(il_container *ic);
  364. extern void il_image_complete(il_container *ic);
  365. extern PRBool il_image_stopped(il_container *ic);
  366.  
  367. extern unsigned int il_write_ready(NET_StreamClass *stream);
  368. extern int il_first_write(NET_StreamClass *stream, const unsigned char *str, int32 len);
  369. extern void il_abort(NET_StreamClass *stream, int status);
  370. extern void il_stream_complete(NET_StreamClass *stream);
  371.  
  372. extern int  il_gif_init(il_container *ic);
  373. extern int  il_gif_write(il_container *, const uint8 *, int32);
  374. extern void il_gif_complete(il_container *ic);
  375. extern int  il_gif_compute_percentage_complete(int row, il_container *ic);
  376. extern unsigned int il_gif_write_ready(il_container *ic);
  377. extern void il_gif_abort(il_container *ic);
  378.  
  379. extern int  il_xbm_init(il_container *ic);
  380. extern int  il_xbm_write(il_container *, const uint8 *, int32);
  381. extern void il_xbm_complete(il_container *ic);
  382. extern void il_xbm_abort(il_container *ic);
  383.  
  384. extern int  il_jpeg_init(il_container *ic);
  385. extern int  il_jpeg_write(il_container *, const uint8 *, int32);
  386. extern void il_jpeg_complete(il_container *ic);
  387. extern void il_jpeg_abort(il_container *ic);
  388.  
  389. extern int  il_png_init(il_container *ic);
  390. extern int  il_png_write(il_container *, const uint8 *, int32);
  391. extern void il_png_complete(il_container *ic);
  392. extern void il_png_abort(il_container *ic);
  393.  
  394. /* Allocate and initialize the destination image's transparent_pixel with
  395.    the Image Library's preferred transparency color i.e. the background color
  396.    passed into IL_GetImage.  The image decoder is encouraged to use this
  397.    color, but may opt not to do so. */
  398. extern int il_init_image_transparent_pixel(il_container *ic);
  399.  
  400. /* Destroy the destination image's transparent pixel. */
  401. extern void il_destroy_image_transparent_pixel(il_container *ic);
  402.  
  403. /* Inform the Image Library of the source image's dimensions.  This function
  404.    determines the size of the destination image, and calls the front end to
  405.    allocate storage for the destination image's bits.  If the source image's
  406.    transparent pixel is set, and a background color was not specified for this
  407.    image request, then a mask will also be allocated for the destination
  408.    image. */
  409. extern int  il_size(il_container *);
  410.  
  411. extern int  il_setup_quantize(void);
  412. extern int  il_init_quantize(il_container *ic);
  413. extern void il_free_quantize(il_container *ic);
  414. extern void il_quantize_fs_dither(il_container * ic,
  415.                                   const uint8* mask,
  416.                                   const uint8 *samp_in,
  417.                                   int x_offset,
  418.                                   uint8 XP_HUGE *samp_out,
  419.                                   int width);
  420.  
  421. extern void il_emit_row(il_container *ic, uint8 *buf, uint8 *rgbbuf,
  422.                         int start_column, int len, int row, int row_count,
  423.                         il_draw_mode draw_mode, int ipass);
  424.  
  425. extern void il_flush_image_data(il_container *ic);
  426. extern PRBool il_setup_color_space_converter(il_container *ic);
  427.  
  428. extern void il_convert_image_to_default_colormap(il_container *ic);
  429.  
  430. #ifndef M12N                    /* XXXM12N Fix custom colormaps. */
  431. extern int  il_set_color_palette(MWContext *cx, il_container *ic);
  432. #endif /* M12N */
  433.  
  434. extern void il_reset_palette(il_container *ic);
  435.  
  436. extern void il_reverse_bits(uint8 *buf, int n);
  437. extern void il_reconnect(il_container *cx);
  438. extern void il_abort_reconnect(void);
  439.  
  440. /* Get an image container from the image cache.  If no cache entry exists,
  441.    then create and return a new container. */
  442. extern il_container
  443. *il_get_container(IL_GroupContext *image_context,
  444.                   NET_ReloadMethod reload_cache_policy,
  445.                   const char *image_url,
  446.                   IL_IRGB *background_color,
  447.                   IL_DitherMode dither_mode,
  448.                   int req_depth,
  449.                   int req_width,
  450.                   int req_height);
  451.  
  452. /* Destroy an IL_Pixmap. */
  453. extern void
  454. il_destroy_pixmap(IMGCBIF *img_cb, IL_Pixmap *pixmap);
  455.  
  456. extern uint32 il_hash(const char *ubuf);
  457.  
  458. extern void il_partial(il_container *ic, int row, int row_count, int pass);
  459. extern void il_scour_container(il_container *ic);
  460. extern void il_adjust_cache_fullness(int32 bytes);
  461. extern PRBool il_add_client(IL_GroupContext *img_cx, il_container *ic,
  462.                             IL_ImageReq *image_req, int is_view_image);
  463. extern void il_reduce_image_cache_size_to(uint32 new_size);
  464.  
  465.  
  466. /************************ Image observer notifiers ***************************/
  467.  
  468. /* Notify observers of image progress. */
  469. extern void
  470. il_progress_notify(il_container *ic);
  471.  
  472. /* Notify observers that the image pixmap has been updated. */
  473. extern void
  474. il_pixmap_update_notify(il_container *ic);
  475.  
  476. /* Notify observers that the image has finished decoding. */
  477. extern void
  478. il_image_complete_notify(il_container *ic);
  479.  
  480. /* Notify observers that one frame of image/animation has finished decoding. */
  481. extern void
  482. il_frame_complete_notify(il_container *ic);
  483.  
  484. /* Notify observers of a cached image that has already completed decoding.
  485.    Note that this notification is per image request and not per image
  486.    container. */
  487. extern void
  488. il_cache_return_notify(IL_ImageReq *image_req);
  489.  
  490. /* Notify observers that the image request is being destroyed.  This
  491.    provides an opportunity for observers to remove themselves from the
  492.    observer callback list and to do any related cleanup. */
  493. extern void
  494. il_image_destroyed_notify(IL_ImageReq *image_req);
  495.  
  496.  
  497. /********************* Image group observer notifier *************************/
  498.  
  499. /* Notify observers that images have started/stopped loading in the context,
  500.    or started/stopped looping in the context. */
  501. extern void
  502. il_group_notify(IL_GroupContext *img_cx, XP_ObservableMsg message);
  503.  
  504. #endif /* _if_h */
  505.  
  506.