void read_sup (parser_state_t *ps) { sup_header_t h; safe_read(&h, sizeof(h), ps->fh, "PG header"); conv_sup_header(&h); if (h.m1 != 80 || h.m2 != 71) die(ps->fh, sizeof(h), "Invalid PG header."); printf("Packet at 0x%08x:\nstart_time = ", (unsigned int)(ftell(ps->fh) - sizeof(h))); print_ts(h.start_time); printf("\ndts = "); print_ts(h.dts); printf("\ntype = 0x%02X\n", h.packet_type); printf("length = %u\n", h.packet_len); switch (h.packet_type) { case 0x14: read_palette(ps, h); break; case 0x15: read_odsf(ps, h); break; case 0x16: switch (h.packet_len) { case 11: read_pcs_end(ps, h); break; case 19: case 27: read_pcs_start(ps, h); break; default: die(ps->fh, sizeof(h), "Invalid PCS size."); } break; case 0x17: read_wds(ps, h); break; case 0x80: if (h.packet_len == 0) printf("Marker\n"); else die(ps->fh, sizeof(h), "Marker with payload."); break; default: die(ps->fh, sizeof(h), "Unknown packet type."); } printf("\n"); }
int main(int argc, char **argv){ if (argc != 4){ (void) fprintf(stdout,"Usage: %s palette input-file output-file\n",argv[0]); exit(0); } read_palette(argv[1]); print_palette(); char *description; ExceptionType severity; #define ThrowWandException(wand) \ { \ description=MagickGetException(wand,&severity); \ (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \ description=(char *) MagickRelinquishMemory(description); \ exit(-1); \ } MagickBooleanType status; MagickPixelPacket pixel; MagickWand *wand; WandView *view; /* Read an image. */ MagickWandGenesis(); wand = NewMagickWand(); status = MagickReadImage(wand, argv[2]); if (status == MagickFalse) ThrowWandException(wand); /* Sigmoidal non-linearity contrast control. */ view = NewWandView(wand); if (view == (WandView *) NULL) ThrowWandException(wand); status = UpdateWandViewIterator(view, recolor, (void *)NULL); if (status == MagickFalse) ThrowWandException(wand); view = DestroyWandView(view); /* Write the image then destroy it. */ status = MagickWriteImages(wand, argv[3], MagickTrue); if (status == MagickFalse) ThrowWandException(wand); wand = DestroyMagickWand(wand); MagickWandTerminus(); return 0; }
int read_pcx( char *file, pcx_picture_t *image ) { FILE *fp; // move the file pointer, to last 768 bytes file if( (fp = fopen( file, "r" )) == NULL ){ fprintf( stderr, "Error opening file: %s %d", __FILE__, __LINE__ ); return EXIT_FAILURE; } // reading header data read_header( fp, &image->header ); // reading image data read_data( fp, image ); // reading palette data read_palette( fp, image ); return 0; }
SV * Widget_palette( Handle self, Bool set, SV * palette) { int colors; if ( !set) return inherited-> palette( self, set, palette); if ( var-> stage > csFrozen) return nilSV; if ( var-> handle == nilHandle) return nilSV; /* aware of call from Drawable::init */ colors = var-> palSize; free( var-> palette); var-> palette = read_palette( &var-> palSize, palette); opt_clear( optOwnerPalette); if ( colors == 0 && var-> palSize == 0) return nilSV; /* do not bother apc */ if ( opt_InPaint) apc_gp_set_palette( self); else apc_widget_set_palette( self); return nilSV; }
static GIF_ANIMATION * load_object (PACKFILE * file, long size) { int version; BITMAP *bmp = NULL; int i, j; GIF_ANIMATION *gif = calloc (1, sizeof *gif); GIF_FRAME frame; int have_global_palette = 0; (void) size; gif->frames_count = 0; /* is it really a GIF? */ if (pack_getc (file) != 'G') goto error; if (pack_getc (file) != 'I') goto error; if (pack_getc (file) != 'F') goto error; if (pack_getc (file) != '8') goto error; /* '7' or '9', for 87a or 89a. */ version = pack_getc (file); if (version != '7' && version != '9') goto error; if (pack_getc (file) != 'a') goto error; gif->width = pack_igetw (file); gif->height = pack_igetw (file); i = pack_getc (file); /* Global color table? */ if (i & 128) gif->palette.colors_count = 1 << ((i & 7) + 1); else gif->palette.colors_count = 0; /* Background color is only valid with a global palette. */ gif->background_index = pack_getc (file); /* Skip aspect ratio. */ pack_fseek (file, 1); if (gif->palette.colors_count) { read_palette (file, &gif->palette); have_global_palette = 1; } memset(&frame, 0, sizeof frame); /* For first frame. */ frame.transparent_index = -1; do { i = pack_getc (file); switch (i) { case 0x2c: /* Image Descriptor */ { int w, h; int interlaced = 0; frame.xoff = pack_igetw (file); frame.yoff = pack_igetw (file); w = pack_igetw (file); h = pack_igetw (file); bmp = create_bitmap_ex (8, w, h); if (!bmp) goto error; i = pack_getc (file); /* Local palette. */ if (i & 128) { frame.palette.colors_count = 1 << ((i & 7) + 1); read_palette (file, &frame.palette); } else { frame.palette.colors_count = 0; } if (i & 64) interlaced = 1; if (LZW_decode (file, bmp)) goto error; if (interlaced) deinterlace (bmp); frame.bitmap_8_bit = bmp; bmp = NULL; gif->frames_count++; gif->frames = realloc (gif->frames, gif->frames_count * sizeof *gif->frames); gif->frames[gif->frames_count - 1] = frame; memset(&frame, 0, sizeof frame); /* For next frame. */ frame.transparent_index = -1; break; } case 0x21: /* Extension Introducer. */ j = pack_getc (file); /* Extension Type. */ i = pack_getc (file); /* Size. */ if (j == 0xf9) /* Graphic Control Extension. */ { /* size must be 4 */ if (i != 4) goto error; i = pack_getc (file); frame.disposal_method = (i >> 2) & 7; frame.duration = pack_igetw (file); if (i & 1) /* Transparency? */ { frame.transparent_index = pack_getc (file); } else { pack_fseek (file, 1); frame.transparent_index = -1; } i = pack_getc (file); /* Size. */ } /* Application Extension. */ else if (j == 0xff) { if (i == 11) { char name[12]; pack_fread (name, 11, file); i = pack_getc (file); /* Size. */ name[11] = '\0'; if (!strcmp (name, "NETSCAPE2.0")) { if (i == 3) { j = pack_getc (file); gif->loop = pack_igetw (file); if (j != 1) gif->loop = 0; i = pack_getc (file); /* Size. */ } } } } /* Possibly more blocks until terminator block (0). */ while (i) { pack_fseek (file, i); i = pack_getc (file); } break; case 0x3b: /* GIF Trailer. */ pack_fclose (file); return gif; } } while (TRUE); error: if (file) pack_fclose (file); if (gif) algif_destroy_raw_animation (gif); if (bmp) destroy_bitmap (bmp); return NULL; }
static int next_packet_smacker(bgav_demuxer_context_t * ctx) { bgav_stream_t * s; bgav_packet_t * p; int palette_change = 0, i; uint8_t frame_flags; uint32_t size; smacker_priv_t * priv; int64_t frame_end; priv = ctx->priv; frame_end = ctx->input->position + (priv->h.frame_sizes[priv->current_frame] & (~3)); frame_flags = priv->h.frame_flags[priv->current_frame]; /* Check if we should read the palette */ if(frame_flags & SMACKER_PAL) { if(!read_palette(ctx)) return 0; else palette_change = 1; } frame_flags >>= 1; /* Check for audio streams */ for(i = 0; i < 7; i++) { if(frame_flags & 0x01) { if(!bgav_input_read_32_le(ctx->input, &size)) return 0; size -= 4; /* Size is including counter */ /* Audio stream */ s = bgav_track_find_stream(ctx, i + AUDIO_OFFSET); if(!s) bgav_input_skip(ctx->input, size); else { #if 0 if(s->fourcc != BGAV_WAVID_2_FOURCC(0x0001)) { bgav_input_skip(ctx->input, 4); /* Uncompressed size */ size -= 4; } #endif p = bgav_stream_get_packet_write(s); bgav_packet_alloc(p, size); p->data_size = bgav_input_read_data(ctx->input, p->data, size); if(!p->data_size) return 0; bgav_stream_done_packet_write(s, p); } } frame_flags >>= 1; } /* Video packet */ s = bgav_track_find_stream(ctx, VIDEO_ID); if(!s) { bgav_input_skip(ctx->input, frame_end - ctx->input->position); priv->current_frame++; return 1; } p = bgav_stream_get_packet_write(s); bgav_packet_alloc(p, frame_end - ctx->input->position + 769); p->data[0] = 0; if(palette_change) p->data[0] |= 1; if(priv->h.frame_sizes[priv->current_frame] & 1) p->data[0] |= 2; /* Palette is always needed */ memcpy(p->data + 1, priv->pal, 768); /* Get rest of the frame */ size = frame_end - ctx->input->position; if(bgav_input_read_data(ctx->input, p->data + 769, size) < size) return 0; p->data_size = size + 769; p->pts = s->in_position * s->data.video.format.frame_duration; bgav_stream_done_packet_write(s, p); priv->current_frame++; return 1; }
ico_image_t * ico_image_read(ico_reader_t *file, int index, int *error) { io_glue *ig = file->ig; ico_reader_image_entry *im; long bi_size, width, height, planes, bit_count; ico_image_t *result; if (index < 0 || index >= file->count) { *error = ICOERR_Bad_Image_Index; return NULL; } im = file->images + index; if (i_io_seek(ig, im->offset, SEEK_SET) != im->offset) { *error = ICOERR_File_Error; return NULL; } if (!read_packed(ig, "dddww xxxx xxxx xxxx xxxx xxxx xxxx", &bi_size, &width, &height, &planes, &bit_count)) { *error = ICOERR_Short_File; return NULL; } /* the bitmapinfoheader height includes the height of the and and xor masks */ if (bi_size != 40 || width != im->width || height != im->height * 2 || planes != 1) { /* don't know how to handle planes != 1 */ *error = ICOERR_Invalid_File; return NULL; } if (bit_count != 1 && bit_count != 4 && bit_count != 8 && bit_count != 32) { *error = ICOERR_Unknown_Bits; return 0; } result = malloc(sizeof(ico_image_t)); if (!result) { *error = ICOERR_Out_Of_Memory; return NULL; } result->width = width; result->height = im->height; result->direct = bit_count > 8; result->bit_count = bit_count; result->palette = NULL; result->image_data = NULL; result->mask_data = NULL; result->hotspot_x = im->hotspot_x; result->hotspot_y = im->hotspot_y; if (bit_count == 32) { result->palette_size = 0; result->image_data = malloc(result->width * result->height * sizeof(ico_color_t)); if (!result->image_data) { free(result); *error = ICOERR_Out_Of_Memory; return NULL; } if (!read_32bit_data(file, result, error)) { free(result->image_data); free(result); return NULL; } } else { int read_result; result->palette_size = 1 << bit_count; result->palette = malloc(sizeof(ico_color_t) * result->palette_size); if (!result->palette) { free(result); *error = ICOERR_Out_Of_Memory; return NULL; } result->image_data = malloc(result->width * result->height); if (!result->image_data) { *error = ICOERR_Out_Of_Memory; free(result->palette); free(result); return 0; } if (!read_palette(file, result, error)) { free(result->palette); free(result->image_data); free(result); return 0; } switch (bit_count) { case 1: read_result = read_1bit_data(file, result, error); break; case 4: read_result = read_4bit_data(file, result, error); break; case 8: read_result = read_8bit_data(file, result, error); break; default: assert(0); /* this can't happen in theory */ read_result = 0; break; } if (!read_result) { free(result->palette); free(result->image_data); free(result); return 0; } } result->mask_data = malloc(result->width * result->height); if (!result->mask_data) { *error = ICOERR_Out_Of_Memory; free(result->palette); free(result->image_data); free(result); return 0; } if (!read_mask(file, result, error)) { free(result->mask_data); free(result->palette); free(result->image_data); free(result); return 0; } return result; }
static int test_data(void) { hid_t fid; hsize_t pal_dims[2]; hsize_t width; hsize_t height; unsigned char pal[256*3]; /* buffer to hold an HDF5 palette */ rgb_t rgb[256]; /* buffer to hold a .pal file palette */ int i, n; /* create a file using default properties */ if ((fid=H5Fcreate(FILE2,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT))<0) goto out; printf("Testing read ascii image data and generate images\n"); /*------------------------------------------------------------------------- * read 8bit image data *------------------------------------------------------------------------- */ TESTING2("make indexed image"); /* read first data file */ if (read_data(DATA_FILE1,&width,&height)<0) goto out; /* make an image */ if (H5IMmake_image_8bit(fid,IMAGE1_NAME,width,height,image_data)<0) goto out; PASSED(); TESTING2("attaching palettes"); /*------------------------------------------------------------------------- * palette #1. rainbow palette. data is contained in "pal_rgb.h" *------------------------------------------------------------------------- */ /* initialize the palette data */ pal_dims[0] = 256; pal_dims[1] = 3; /* make a palette */ if (H5IMmake_palette(fid,PAL1_NAME,pal_dims,pal_rgb)<0) goto out; /* attach a palette to the image dataset */ if (H5IMlink_palette(fid,IMAGE1_NAME,PAL1_NAME)<0) goto out; /*------------------------------------------------------------------------- * palette #2. sepia palette. * read a PAL file and attach the palette to the HDF5 file *------------------------------------------------------------------------- */ /* read a PAL file */ if (read_palette(PAL2_FILE, rgb, (sizeof(rgb) / sizeof(rgb[0]))) < 0) goto out; /* transfer to the HDF5 buffer */ for ( i=0, n=0; i<256*3; i+=3, n++) { pal[i] =rgb[n].r; pal[i+1]=rgb[n].g; pal[i+2]=rgb[n].b; } /* make a palette */ if (H5IMmake_palette(fid,PAL2_NAME,pal_dims,pal)<0) goto out; /* attach the palette to the image dataset */ if (H5IMlink_palette(fid,IMAGE1_NAME,PAL2_NAME)<0) goto out; /*------------------------------------------------------------------------- * palette #3. earth palette. * read a PAL file and attach the palette to the HDF5 file *------------------------------------------------------------------------- */ /* read a PAL file */ if (read_palette(PAL3_FILE, rgb, (sizeof(rgb) / sizeof(rgb[0]))) < 0) goto out; /* transfer to the HDF5 buffer */ for ( i=0, n=0; i<256*3; i+=3, n++) { pal[i] =rgb[n].r; pal[i+1]=rgb[n].g; pal[i+2]=rgb[n].b; } /* make a palette */ if (H5IMmake_palette(fid,PAL3_NAME,pal_dims,pal)<0) goto out; /* attach the palette to the image dataset */ if (H5IMlink_palette(fid,IMAGE1_NAME,PAL3_NAME)<0) goto out; PASSED(); /*------------------------------------------------------------------------- * palette #4. blue-red * make a palette whith blue to red colors *------------------------------------------------------------------------- */ for ( i=0, n=0; i<256*3; i+=3, n++) { pal[i] =(unsigned char)n; pal[i+1]=0; pal[i+2]=(unsigned char)(255 - n); } /* make a palette */ if (H5IMmake_palette(fid,PAL4_NAME,pal_dims,pal)<0) goto out; /* attach the palette to the image dataset */ if (H5IMlink_palette(fid,IMAGE1_NAME,PAL4_NAME)<0) goto out; /*------------------------------------------------------------------------- * true color image example with pixel interlace *------------------------------------------------------------------------- */ TESTING2("make true color image with pixel interlace"); /* read second data file */ if ((read_data(DATA_FILE2,&width,&height))<0) goto out; /* make image */ if ((H5IMmake_image_24bit(fid,IMAGE2_NAME,width,height,"INTERLACE_PIXEL",image_data))<0) goto out; PASSED(); /*------------------------------------------------------------------------- * True color image example with plane interlace *------------------------------------------------------------------------- */ TESTING2("make true color image with plane interlace"); /* read third data file */ if ((read_data(DATA_FILE3,&width,&height))<0) goto out; /* make image */ if ((H5IMmake_image_24bit(fid,IMAGE3_NAME,width,height,"INTERLACE_PLANE",image_data))<0) goto out; PASSED(); /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ if (H5Fclose(fid)<0) goto out; /* Release memory buffer */ HDfree(image_data); return 0; /* error zone, gracefully close */ out: /* Release memory buffer */ if(image_data) HDfree(image_data); H5E_BEGIN_TRY { H5Fclose(fid); } H5E_END_TRY; H5_FAILED(); return FAIL; }