/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * The contents of this file are subject to the Netscape Public License * Version 1.0 (the "NPL"); you may not use this file except in * compliance with the NPL. You may obtain a copy of the NPL at * http://www.mozilla.org/NPL/ * * Software distributed under the NPL is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL * for the specific language governing rights and limitations under the * NPL. * * The Initial Developer of this code under the NPL is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ /* -*- Mode: C; tab-width: 4 -*- scale.c --- Controls rendering of scan lines to screen, including scaling and transparency */ #include "if.h" /* Image library internal declarations */ #include "il.h" /* Image library external API */ #include "il_strm.h" /* For image types. */ #include "prmem.h" #include "nsVoidArray.h" #include "nsITimer.h" #include "nsITimerCallback.h" /* Approximate size of pixel data chunks sent to the FE for display */ #ifdef XP_OS2 #define OUTPUT_CHUNK_SIZE 30000 #else #define OUTPUT_CHUNK_SIZE 15000 #endif /* Delay from decode to display of first scanline, in milliseconds. */ #define ROW_OUTPUT_INITIAL_DELAY 50 /* Delays between subsequent sets of scanlines */ #define ROW_OUTPUT_DELAY 300 /* for png */ typedef struct _IL_IRGBGA { uint8 index; uint8 red, green, blue, gray, alpha; } IL_IRGBGA; static nsVoidArray *gTimeouts = NULL; static void il_timeout_callback(void *closure) { int delay; il_container *ic = (il_container *)closure; NI_PixmapHeader *img_header = &ic->image->header; ic->row_output_timeout = NULL; if (ic->state == IC_ABORT_PENDING) return; /* * Don't schedule any more timeouts once we start decoding the * second image in a multipart image sequence. Instead display * will take place when the entire image is decoded. */ if (ic->multi && ((uint32)img_header->height * img_header->width < 100000)) { return; } il_flush_image_data(ic); delay = (ic->pass > 1) ? 2 * ROW_OUTPUT_DELAY : ROW_OUTPUT_DELAY; ic->row_output_timeout = IL_SetTimeout(il_timeout_callback, ic, delay); } /*----------------------------------------------------------------------------- * Display a specified number of rows of pixels for the purpose of * progressive image display. The data is accumulated without forwarding * it to the front-end for display until either the row-output timeout * fires or the image is fully decoded. *---------------------------------------------------------------------------*/ void il_partial( il_container *ic, /* The image container */ int row, /* Starting row; zero is top row of image */ int row_count, /* Number of rows to output, including starting row */ int pass) /* Zero, unless interlaced GIF, in which case ranges 1-4, or progressive JPEG, in which case ranges from 1-n. */ { NI_PixmapHeader *img_header = &ic->image->header; if (!ic->new_data_for_fe) { ic->update_start_row = row; ic->update_end_row = row + row_count - 1; ic->new_data_for_fe = TRUE; } else { if (row < ic->update_start_row) ic->update_start_row = row; if ((row + row_count - 1) > ic->update_end_row) ic->update_end_row = row + row_count - 1; } ic->pass = pass; if (ic->img_cx->progressive_display) { #ifdef XP_WIN /* The last pass of an image is displayed with less delay. */ if (!ic->multi && (pass == IL_FINAL_PASS)) #else /* The first and last pass of an image are displayed with less delay. */ if (!ic->multi && ((pass <= 1) || (pass == IL_FINAL_PASS))) #endif { int num_rows = ic->update_end_row - ic->update_start_row + 1; if (num_rows * img_header->widthBytes > OUTPUT_CHUNK_SIZE) il_flush_image_data(ic); } /* * Don't schedule any more timeouts once we start decoding the * second image in a multipart image sequence. Instead display * will take place when the entire image is decoded. */ if (ic->multi && ((uint32)img_header->height * img_header->width < 100000)) { return; } if (!ic->row_output_timeout){ /* Set a timer that will actually display the image data. */ ic->row_output_timeout = IL_SetTimeout(il_timeout_callback, ic, ROW_OUTPUT_INITIAL_DELAY); } } } /*----------------------------------------------------------------------------- * Force the front-end to to display any lines in the image bitmap * that have been decoded, but haven't yet been sent to the screen. * (Progressively displayed images are normally displayed several * lines at a time for efficiency. This routine flushes out the last * few undisplayed lines in the image.) *---------------------------------------------------------------------------*/ void il_flush_image_data(il_container *ic) { IL_GroupContext *img_cx = ic->img_cx; IL_Pixmap *image = ic->image; IL_Pixmap *mask = ic->mask; NI_PixmapHeader *img_header = &image->header; NI_PixmapHeader *mask_header = mask ? &mask->header : NULL; int row, start_row, end_row, row_interval; /* If we never called il_size(), we have no data for the FE. There may also be no new data if a previous flush has occurred. */ if (!image->bits || !ic->new_data_for_fe) return; start_row = ic->update_start_row; end_row = ic->update_end_row; row_interval = (2 * OUTPUT_CHUNK_SIZE) / img_header->widthBytes; row = start_row; #ifdef XP_UNIX /* If the amount of image data becomes really large, break it * up into chunks to BLT out to the screen. Otherwise, there * can be a noticeable delay as the FE processes a large image. * (In the case of the XFE, it can take a long time to send * it to the server.) */ for (;row < (end_row - row_interval); row += row_interval) { img_cx->img_cb->UpdatePixmap(img_cx->dpy_cx, image, 0, row, img_header->width, row_interval); if (mask) { img_cx->img_cb->UpdatePixmap(img_cx->dpy_cx, mask, 0, row, mask_header->width, row_interval); } } #endif /* XP_UNIX */ /* Draw whatever is leftover after sending the chunks */ img_cx->img_cb->UpdatePixmap(img_cx->dpy_cx, image, 0, row, img_header->width, end_row - row + 1); if (mask) { img_cx->img_cb->UpdatePixmap(img_cx->dpy_cx, mask, 0, row, mask_header->width, end_row - row + 1); } /* Update the displayable area of the pixmap. */ ic->displayable_rect.x_origin = 0; ic->displayable_rect.y_origin = 0; ic->displayable_rect.width = img_header->width; ic->displayable_rect.height = MAX(ic->displayable_rect.height, end_row + 1); /* Notify observers that the image pixmap has been updated. */ il_pixmap_update_notify(ic); /* Notify observers of image progress. */ il_progress_notify(ic); ic->new_data_for_fe = FALSE; ic->update_end_row = ic->update_start_row = 0; } /* Copy a packed RGB triple */ #define COPY_RGB(src, dest) \ {dest[0] = src[0]; dest[1] = src[1]; dest[2] = src[2];} /*----------------------------------------------------------------------------- * Scale a row of packed RGB pixels using the Bresenham algorithm. * Output is also packed RGB pixels. *---------------------------------------------------------------------------*/ static void il_scale_RGB_row( uint8 XP_HUGE *src, /* Source row of packed RGB pixels */ int src_len, /* Number of pixels in source row */ uint8 *dest, /* Destination, packed RGB pixels */ int dest_len) /* Length of target row, in pixels */ { uint8 *dest_end = dest + (3 * dest_len); int n = 0; PR_ASSERT(dest); PR_ASSERT(src_len != dest_len); /* Two cases */ /* Scaling down ? ... */ if (src_len > dest_len) { while (dest < dest_end) { COPY_RGB(src, dest); dest += 3; n += src_len; while (n >= dest_len) { src += 3; n -= dest_len; } } } else /* Scaling up. */ { while (dest < dest_end) { n += dest_len; while (n >= src_len) { COPY_RGB(src, dest); dest += 3; n -= src_len; } src += 3; } } } #ifdef M12N /*----------------------------------------------------------------------------- * Scale a row of single-byte pixels using the Bresenham algorithm. * Output is also single-byte pixels. *---------------------------------------------------------------------------*/ static void il_scale_CI_row( uint8 XP_HUGE *src, /* Source row of packed RGB pixels */ int src_len, /* Number of pixels in source row */ uint8 *dest, /* Destination, packed RGB pixels */ int dest_len, /* Length of target row, in pixels */ uint8* indirect_map,/* image-to-FE color mapping */ int transparent_pixel_color) { int src_pixel, mapped_src_pixel; uint8 *dest_end = dest + dest_len; int n = 0; PR_ASSERT(dest); PR_ASSERT(src_len != dest_len); /* Two cases */ /* Scaling down ? ... */ if (src_len > dest_len) { while (dest < dest_end) { if (*src != transparent_pixel_color) *dest = indirect_map[*src]; dest ++; n += src_len; while (n >= dest_len) { src ++; n -= dest_len; } } } else /* Scaling up. */ { while (dest < dest_end) { n += dest_len; src_pixel = *src; mapped_src_pixel = indirect_map[src_pixel]; while (n >= src_len) { if (src_pixel != transparent_pixel_color) *dest = mapped_src_pixel; dest ++; n -= src_len; } src++; } } } #endif /* M12N */ /* Convert row coordinate from image space to display space. */ #define SCALE_YCOORD(ih, sh, y) \ ((int)((uint32)(y) * (ih)->height / (sh)->height)) #define SCALE_XCOORD(ih, sh, x) \ ((int)((uint32)(x) * (ih)->width / (sh)->width)) /* Add a bit to the row of mask bits. Flush accumulator to memory if full. */ #define SHIFT_IMAGE_MASK(not_transparent_flag) \ { \ fgmask32 |= ((uint32)not_transparent_flag ) << M32(mask_bit); \ bgmask32 |= ((uint32)not_transparent_flag ^ 1) << M32(mask_bit); \ \ /* Filled up 32-bit mask word. Write it to memory. */ \ if (mask_bit-- == 0) { \ uint32 mtmp = *m; \ mtmp |= fgmask32; \ if (draw_mode == ilErase) \ mtmp &= ~bgmask32; \ *m++ = mtmp; \ mask_bit = 31; \ bgmask32 = 0; \ fgmask32 = 0; \ } \ output_bits_remaining--; \ } /*----------------------------------------------------------------------------- * * Create a 1 bit mask bitmap from alpha channel. * Accumulate the mask in 32-bit chunks for efficiency. *---------------------------------------------------------------------------*/ static void il_alpha_mask( uint8 *src, /* RGBa, input data */ int src_len, /* Number of pixels in source row */ int x_offset, /* Destination offset from left edge */ uint8 XP_HUGE *maskp, /* Output pointer, left-justified bitmask */ int mask_len, /* Number of pixels in output row */ il_draw_mode draw_mode) /* ilOverlay or ilErase */ { int not_transparent,n =0; int output_bits_remaining = mask_len; uint32 bgmask32 = 0; /* 32-bit temporary mask accumulators */ uint32 fgmask32 = 0; int mask_bit; /* next bit to write in setmask32 */ uint32 *m = ((uint32*)maskp) + (x_offset >> 5); mask_bit = ~x_offset & 0x1f; PR_ASSERT(mask_len); /* Handle case in which we have a mask for a non-transparent image. This can happen when we have a LOSRC that is a transparent GIF and a SRC that is a JPEG. For now, we avoid crashing. Later we should fix that case so it does the right thing and gets rid of the mask. */ if (!src) return; /* Two cases */ /* Scaling down ? (or source and dest same size) ... */ if (src_len >= mask_len) { while (output_bits_remaining ) { not_transparent = (*(src+3) > 0x60 ); SHIFT_IMAGE_MASK(not_transparent); n += src_len; while ( n >= mask_len){ src += 4; n -= mask_len; } } } else /* Scaling up */ { while (output_bits_remaining) { n += mask_len; not_transparent = (*src != 0); while (n >= src_len) { SHIFT_IMAGE_MASK(not_transparent); n -= src_len; } src++; } } /* End of scan line. Write out any remaining mask bits. */ if (mask_bit < 31) { uint32 mtmp = *m; mtmp |= fgmask32; if (draw_mode == ilErase) mtmp &= ~bgmask32; *m = mtmp; } } /*----------------------------------------------------------------------------- * Create a transparency mask bitmap. Perform horizontal scaling if * requested using a Bresenham algorithm. Accumulate the mask in * 32-bit chunks for efficiency. *---------------------------------------------------------------------------*/ static void il_generate_scaled_transparency_mask( IL_IRGB *transparent_pixel, /* The transparent pixel */ uint8 *src, /* Row of pixels, 8-bit pseudocolor data */ int src_len, /* Number of pixels in source row */ int x_offset, /* Destination offset from left edge */ uint8 XP_HUGE *maskp, /* Output pointer, left-justified bitmask */ int mask_len, /* Number of pixels in output row */ il_draw_mode draw_mode) /* ilOverlay or ilErase */ { int not_transparent, n = 0; int src_trans_pixel_index = transparent_pixel ? transparent_pixel->index : -1; int output_bits_remaining = mask_len; uint32 bgmask32 = 0; /* 32-bit temporary mask accumulators */ uint32 fgmask32 = 0; int mask_bit; /* next bit to write in setmask32 */ uint32 *m = ((uint32*)maskp) + (x_offset >> 5); mask_bit = ~x_offset & 0x1f; PR_ASSERT(mask_len); /* Handle case in which we have a mask for a non-transparent image. This can happen when we have a LOSRC that is a transparent GIF and a SRC that is a JPEG. For now, we avoid crashing. Later we should fix that case so it does the right thing and gets rid of the mask. */ if (!src) return; /* Two cases */ /* Scaling down ? (or source and dest same size) ... */ if (src_len >= mask_len) { while (output_bits_remaining) { not_transparent = (*src != src_trans_pixel_index); SHIFT_IMAGE_MASK(not_transparent); n += src_len; while (n >= mask_len) { src++; n -= mask_len; } } } else /* Scaling up */ { while (output_bits_remaining) { n += mask_len; not_transparent = (*src != src_trans_pixel_index); while (n >= src_len) { SHIFT_IMAGE_MASK(not_transparent); n -= src_len; } src++; } } /* End of scan line. Write out any remaining mask bits. */ if (mask_bit < 31) { uint32 mtmp = *m; mtmp |= fgmask32; if (draw_mode == ilErase) mtmp &= ~bgmask32; *m = mtmp; } } /*----------------------------------------------------------------------------- * Scale 1-bit transparency mask: * Perform horizontal scaling if requested using a Bresenham algorithm. * Accumulate the mask in 32-bit chunks for efficiency. *---------------------------------------------------------------------------*/ static void il_scale_mask( uint8 *src, /* input mask data */ int src_len, /* Number of pixels in source row */ int x_offset, /* Destination offset from left edge */ uint8 XP_HUGE *maskp, /* Output pointer, left-justified bitmask */ int mask_len, /* Number of pixels in output row */ il_draw_mode draw_mode) /* ilOverlay or ilErase */ { int not_transparent,n = 0; int output_bits_remaining = mask_len; uint32 bgmask32 = 0; /* 32-bit temporary mask accumulators */ uint32 fgmask32 = 0; uint8 src_bit; /* next bit to read in setmask32 */ int mask_bit; /* next bit to write in setmask32 */ uint32 *m = ((uint32*)maskp) + (x_offset >> 5); mask_bit = ~x_offset & 0x1f; src_bit = 0x07; PR_ASSERT(mask_len); /* Handle case in which we have a mask for a non-transparent image. This can happen when we have a LOSRC that is a transparent GIF and a SRC that is a JPEG. For now, we avoid crashing. Later we should fix that case so it does the right thing and gets rid of the mask. */ if (!src) return; if (src_len >= mask_len) /* Scaling down */ { while (output_bits_remaining ) { not_transparent = ((*src & ((uint8)1 << src_bit)) != 0); SHIFT_IMAGE_MASK(not_transparent); n += src_len; while ( n >= mask_len){ if (src_bit-- == 0){ src ++; src_bit = 7; } n -= mask_len; } } } else /* Scaling up */ { while (output_bits_remaining){ n += mask_len; not_transparent = ((*src & ((uint8)1 << src_bit)) != 0); while (n >= src_len){ SHIFT_IMAGE_MASK(not_transparent); n -= src_len; } if (src_bit-- == 0){ src ++; src_bit = 7; } } } /* End of scan line. Write out any remaining mask bits. */ if (mask_bit < 31){ uint32 mtmp = *m; mtmp |= fgmask32; if (draw_mode == ilErase) mtmp &= ~bgmask32; *m = mtmp; } } #undef SHIFT_IMAGE_MASK /*----------------------------------------------------------------------------- * When color quantization (possibly accompanied by dithering) takes * place, the background pixels in a transparent image that overlays a * solid-color background, e.g.
, will get * mapped to a color in the color-cube. The real background color, * however, may not be one of these colors reserved for images. This * routine serves to return transparent pixels to their background * color. This routine must performing scaling because the source * pixels are in the image space and the target pixels are in the * display space. *---------------------------------------------------------------------------*/ static void il_reset_background_pixels( il_container *ic, /* The image container */ uint8 *src, /* Row of pixels, 8-bit pseudocolor data */ int src_len, /* Number of pixels in row */ uint8 XP_HUGE *dest, /* Output pointer, 8-bit pseudocolor data */ int dest_len) /* Width of output pixel row */ { int is_transparent, n = 0; uint8 XP_HUGE *dest_end = dest + dest_len; NI_PixmapHeader *img_header = &ic->image->header; int src_trans_pixel_index = ic->src_header->transparent_pixel->index; int img_trans_pixel_index = img_header->transparent_pixel->index; int dpy_trans_pixel_index = img_header->color_space->cmap.index ? img_header->color_space->cmap.index[img_trans_pixel_index] : il_identity_index_map[img_trans_pixel_index]; /* Two cases */ /* Scaling down ? (or not scaling ?) ... */ if (src_len >= dest_len) { while (dest < dest_end) { is_transparent = (*src == src_trans_pixel_index); if (is_transparent) *dest = dpy_trans_pixel_index; dest++; n += src_len; while (n >= dest_len) { src++; n -= dest_len; } } } else { /* Scaling up */ while (dest < dest_end) { n += dest_len; is_transparent = (*src++ == src_trans_pixel_index); if (is_transparent) while (n >= src_len) { *dest++ = dpy_trans_pixel_index; n -= src_len; } else while (n >= src_len) { dest++; n -= src_len; } } } } static void il_generate_byte_mask( il_container *ic, /* The image container */ uint8 *src, /* Row of pixels, 8-bit pseudocolor data */ int src_len, /* Number of pixels in row */ uint8 *dest, /* Output pointer, 8-bit pseudocolor data */ int dest_len) /* Width of output pixel row */ { int is_transparent, n = 0; uint8 XP_HUGE *dest_end = dest + dest_len; int src_trans_pixel_index = ic->src_header->transparent_pixel->index; /* Two cases */ /* Scaling down ? (or not scaling ?) ... */ if (src_len >= dest_len) { while (dest < dest_end) { is_transparent = (*src == src_trans_pixel_index); *dest = is_transparent - 1; dest++; n += src_len; while (n >= dest_len) { src++; n -= dest_len; } } } else { /* Scaling up */ while (dest < dest_end) { n += dest_len; is_transparent = (*src++ == src_trans_pixel_index); if (is_transparent) while (n >= src_len) { *dest++ = 0; n -= src_len; } else while (n >= src_len) { *dest++ = (uint8)-1; n -= src_len; } } } } static void il_overlay(uint8 *src, uint8 *dest, uint8 *byte_mask, int num_cols, int bytes_per_pixel) { int i, col; #if 0 uint8 *s = src; uint8 *s_end = src + (num_cols * bytes_per_pixel); #endif for (col = num_cols; col > 0; col--) { if (*byte_mask++) { for (i = bytes_per_pixel-1; i >= 0; i--) { dest[i] = src[i]; } } dest += bytes_per_pixel; src += bytes_per_pixel; } } /*----------------------------------------------------------------------------- * Scale a row of packed RGB pixels using the Bresenham algorithm. * Output is also packed RGB pixels. *---------------------------------------------------------------------------*/ static void il_scale_alpha8( uint8 XP_HUGE *src, /* Source row of packed RGB pixels */ int src_len, /* Number of pixels in source row */ uint8 *dest, /* Destination, packed RGB pixels */ int dest_len) /* Length of target row, in pixels */ { uint8 *dest_end = dest + dest_len; int n = 0; PR_ASSERT(dest); PR_ASSERT(src_len != dest_len); /* Two cases */ /* Scaling down ? ... */ if (src_len > dest_len) { while (dest < dest_end) { *dest= *src; dest ++; n += src_len; while (n >= dest_len) { src++; n -= dest_len; } } } else /* Scaling up. */ { while (dest < dest_end) { n += dest_len; while (n >= src_len) { *dest = *src; dest ++; n -= src_len; } src ++; } } } static uint8 il_tmpbuf[MAX_IMAGE_WIDTH]; /*----------------------------------------------------------------------------- * Emit a complete row of pixel data into the image. This routine * provides any necessary conversion to the display depth, optional dithering * for pseudocolor displays, scaling and transparency, including mask * generation, if necessary. If sufficient data is accumulated, the screen * image is updated, as well. * * PN note: Function too long. Needs to be split. *---------------------------------------------------------------------------*/ void il_emit_row( il_container *ic, /* The image container */ uint8 *cbuf, /* Color index data source, or NULL if source is RGB data */ uint8 *rgbbuf, /* Packed RGBa data or RGBa workspace if