pike.git / src / modules / Image / encodings / psd.c

version» Context lines:

pike.git/src/modules/Image/encodings/psd.c:4:   || for more information.   */      #include "global.h"   #include "image_machine.h"      #include "pike_macros.h"   #include "object.h"   #include "module_support.h"   #include "interpret.h" - #include "object.h" +    #include "svalue.h"   #include "threads.h" - #include "interpret.h" - #include "svalue.h" +    #include "mapping.h"   #include "pike_error.h"   #include "stralloc.h"   #include "builtin_functions.h"      #include "image.h"   #include "colortable.h"   #include "bignum.h"      #ifdef HAVE_NETINET_IN_H
pike.git/src/modules/Image/encodings/psd.c:83:    unsigned char res = 0;    if(from->len)    {    res = from->str[0];    from->str++;    from->len--;    }    return res;   }    - static int psd_read_char( struct buffer *from ) + static int psd_read_schar(struct buffer *from)   { -  return (char)psd_read_uchar( from ); +  return (signed char)psd_read_uchar(from);   }    -  +    static char *psd_read_data( struct buffer * from, size_t len )   {    char *res;    if( from->len < len ) -  Pike_error("Not enough space for %lu bytes\n", -  DO_NOT_WARN((unsigned long)len)); +  Pike_error("Not enough space for %lu bytes\n", (unsigned long)len);    res = (char *)from->str;    from->str += len;    from->len -= len;    return res;   }      static struct buffer psd_read_string( struct buffer *data )   {    struct buffer res;    res.len = psd_read_int( data );
pike.git/src/modules/Image/encodings/psd.c:198:    struct layer *layer = NULL;    ptrdiff_t exp_offset;    if(!src->len)    return;       exp_offset = src->len-psd_read_int( src ); /* size of layer region */    count = psd_read_short( src );       if( count < 0 )    { -  count = -count; +  if (DO_INT16_NEG_OVERFLOW(count, &count)) +  Pike_error("overflow\n");    first_alpha_is_magic = 1;    } else if(count == 0)    return;    while( count-- )    {    unsigned int cnt; -  struct layer *l= -  layer = (struct layer *)xcalloc( sizeof( struct layer ), 1); +  struct layer *l = layer = xcalloc( sizeof( struct layer ), 1);    layer->next = dst->first_layer;    if(dst->first_layer) dst->first_layer->prev = layer;    dst->first_layer = layer;    layer->top = psd_read_int( src );    layer->left = psd_read_int( src );    layer->bottom = psd_read_int( src );    layer->right = psd_read_int( src ); -  layer->num_channels = psd_read_short( src ); +  layer->num_channels = psd_read_ushort( src ); +  if (layer->num_channels * 6 > src->len) +  Pike_error("Too many channels.\n");    for(cnt=0; cnt<layer->num_channels; cnt++)    {    layer->channel_info[cnt].id = psd_read_ushort(src);    layer->channel_info[cnt].data.len = psd_read_uint(src);    }    psd_read_uint( src ); /* '8BIM' */    layer->mode.len = 4;    layer->mode.str = (unsigned char *)psd_read_data( src, 4 );    layer->opacity = psd_read_uchar( src );    layer->clipping = psd_read_uchar( src );
pike.git/src/modules/Image/encodings/psd.c:270:    (unsigned char *)psd_read_data(src,layer->channel_info[i].data.len);    }    layer = layer->prev;    }   }         static struct buffer   packbitsdecode(struct buffer src,    struct buffer dst, -  int nbytes) +  size_t nbytes)   {    int n;       while( nbytes-- )    { -  n = psd_read_uchar( &src ); -  if(n >= 128) -  n -= 256; +  n = psd_read_schar( &src );    if (n > 0)    {    ++n;    while(n--)    {    if(dst.len)    {    *(dst.str++) = psd_read_uchar( &src );    dst.len--;    } else
pike.git/src/modules/Image/encodings/psd.c:310:    {    *(dst.str++) = val;    dst.len--;    } else    return src;    }    }    }    if(dst.len)    fprintf(stderr, "%ld bytes left to write! (should be 0)\n", -  DO_NOT_WARN((long)dst.len)); +  (long)dst.len);    return src;   }      static void f_decode_packbits_encoded(INT32 args)   {    struct pike_string *src = NULL;    int nelems = 0;    int width = 0;    int multiplier = 1;    struct pike_string *dest;    int compression = -1;    struct buffer b, ob, d;    if(TYPEOF(sp[-args]) != T_STRING)    Pike_error("Internal argument error.\n");    -  get_all_args("decode_packbits_encoded", args, +  get_all_args(NULL, args,    "%T%d%d.%d%d",    &src, &nelems, &width,    &multiplier, &compression);       nelems *= multiplier;    b.str = (unsigned char *)src->str;    b.len = src->len;    if (compression < 0) { -  compression = (src->str[0] << 8) | src->str[1]; -  b.str += 2; -  b.len -= 2; +  compression = psd_read_ushort(&b);    }       pop_n_elems(args-1);    -  +  if (nelems < 0 || b.len < (size_t)nelems*2) +  Pike_error("Not enough space for %d short integers.\n", nelems); +     ob = b;    ob.str += nelems*2;    ob.len -= nelems*2;       switch(compression)    {    case 1: -  dest = begin_shared_string( width * nelems ); -  d.str = (unsigned char *)dest->str; d.len = width*nelems; - /* while(nelems--) */ -  /*ob =*/ -  packbitsdecode( ob, d, width*nelems ); +  d.len = width * nelems; +  dest = begin_shared_string( d.len ); +  d.str = (unsigned char *)dest->str; +  packbitsdecode( ob, d, d.len );    push_string( end_shared_string( dest ) );    break;    case 0:    push_string( make_shared_binary_string((char *)b.str,b.len));    break;    default:    Pike_error("Unsupported compression (%d)!\n", compression);    }    stack_swap();    pop_stack();
pike.git/src/modules/Image/encodings/psd.c:375:   }      static void f_decode_image_channel( INT32 args )   {    INT_TYPE w, h;    int y;    struct pike_string *s;    struct object *io;    unsigned char *source;    rgb_group *dst; -  get_all_args( "_decode_image_channel",args, "%i%i%S", &w,&h,&s); +  get_all_args( NULL, args, "%i%i%S", &w,&h,&s);       ref_push_string( s );    push_int( h );    push_int( w );    f_decode_packbits_encoded( 3 );    s = sp[-1].u.string;    stack_swap();    pop_stack();    if(s->len < w*h)    Pike_error("Not enough data in string for this channel\n");    source = (unsigned char *)s->str;    push_int( w ); push_int( h );    io = clone_object( image_program, 2 ); -  dst = ((struct image *)get_storage(io,image_program))->img; +  dst = ((struct image*)get_storage(io,image_program))->img;    for(y=0; y<w*h; y++)    {    dst->r = dst->g = dst->b = *(source++);    dst++;    }    pop_n_elems(args);    push_object( io );   }            static void f_decode_image_data( INT32 args )   {    INT_TYPE w, h, c, d, m;    int y;    struct pike_string *s, *ct;    struct object *io;    unsigned char *source, *source2, *source3, *source4;    rgb_group *dst; -  get_all_args( "_decode_image_data",args, "%i%i%i%i%i%S%S", +  get_all_args( NULL, args, "%i%i%i%i%i%S%S",    &w,&h,&d,&m,&c,&s,&ct);       if(!ct->len) ct = NULL;       ref_push_string( s );    push_int( h );    push_int( w );    push_int( d );    push_int( c );    f_decode_packbits_encoded( 5 );
pike.git/src/modules/Image/encodings/psd.c:431:    stack_swap();    pop_stack();    if(s->len < w*h*d)    Pike_error("Not enough data in string for this channel\n");    source = (unsigned char *)s->str;    source2 = source+w*h;    source3 = source+w*h*2;    source4 = source+w*h*3;    push_int( w ); push_int( h );    io = clone_object( image_program, 2 ); -  dst = ((struct image *)get_storage(io,image_program))->img; +  dst = ((struct image*)get_storage(io,image_program))->img;    for(y=0; y<w*h; y++)    {    switch( d )    {    case 4:    /* cmyk.. */    dst->r = MAXIMUM(255-(*(source++) + *source4), 0);    dst->g = MAXIMUM(255-(*(source2++) + *source4), 0);    dst->b = MAXIMUM(255-(*(source3++) + *source4), 0);    dst++; source4++;
pike.git/src/modules/Image/encodings/psd.c:495:    i->first_layer = t->next;    free(t);    }   }      static struct psd_image low_psd_decode( struct buffer *b )   {    int *s = (int *)b->str;    struct psd_image i;    ONERROR err; -  MEMSET(&i, 0, sizeof(i)); +  memset(&i, 0, sizeof(i));    SET_ONERROR( err, free_image, &i );    i.num_channels = psd_read_ushort( b );    i.rows = psd_read_uint( b );    i.columns = psd_read_uint( b );    i.depth = psd_read_ushort( b );    i.mode = psd_read_ushort( b );    i.color_data = psd_read_string( b );    i.resource_data = psd_read_string( b ); i.resource_data.len++;    i.layer_data = psd_read_string( b ); /*i.layer_data.len++;*/    i.compression = psd_read_short( b );
pike.git/src/modules/Image/encodings/psd.c:548:    ref_push_string( s_channels );    for( i = 0; i<l->num_channels; i++ )    {    ref_push_string( s_id );    push_int( l->channel_info[i].id );    ref_push_string( s_data );    push_buffer( &l->channel_info[i].data );    f_aggregate_mapping( 4 );    }    f_aggregate( l->num_channels ); -  f_aggregate_mapping(DO_NOT_WARN((INT32)(sp - osp))); +  f_aggregate_mapping((INT32)(sp - osp));   }         static void decode_resources( struct buffer *b );   void push_psd_image( struct psd_image *i )   {    struct svalue *osp = sp, *tsp;    struct layer *l;    ref_push_string( s_channels ); push_int( i->num_channels );    ref_push_string( s_height ); push_int( i->rows );
pike.git/src/modules/Image/encodings/psd.c:574:    ref_push_string( s_resources ); decode_resources( &i->resource_data );    ref_push_string( s_image_data ); push_buffer( &i->image_data );    ref_push_string( s_layers );    l = i->first_layer;    tsp = sp;    while( l )    {    push_layer( l );    l = l->next;    } -  f_aggregate(DO_NOT_WARN((INT32)(sp - tsp))); -  f_aggregate_mapping(DO_NOT_WARN((INT32)(sp - osp))); +  f_aggregate((INT32)(sp - tsp)); +  f_aggregate_mapping((INT32)(sp - osp));   }      static void decode_resources( struct buffer *b )   {    struct svalue *osp = Pike_sp;       while( b->len > 11 )    {    char *signature = psd_read_data( b, 4 );   
pike.git/src/modules/Image/encodings/psd.c:610:    data = psd_read_string( b );    data.len++;       if( data.len & 1 ) psd_read_uchar( b );       switch( id )    {    case 0x03f0: /* caption */    {    struct buffer b = psd_read_pstring( &data ); -  push_constant_text( "caption" ); +  push_static_text( "caption" );    push_buffer( &b );    }    break;       case 0x0400: /* layer state info */ -  push_constant_text( "active_layer" ); +  push_static_text( "active_layer" );    push_int( psd_read_short( &data ) );    break;    case 0x0408: /* guides */ -  push_constant_text( "guides" ); +  push_static_text( "guides" );    {    int i,num_guides;    short magic1, magic2, magic3, magic4, magic5, magic6; /*from gimp.*/    magic1 = psd_read_short( &data ); magic2 = psd_read_short( &data );    magic3 = psd_read_short( &data ); magic4 = psd_read_short( &data );    magic5 = psd_read_short( &data ); magic6 = psd_read_short( &data );    num_guides = psd_read_int( &data );       if( data.len != (unsigned)(num_guides * 5) )    {
pike.git/src/modules/Image/encodings/psd.c:642:    break;    }    for (i=0; i<num_guides; i++)    {    ptrdiff_t p = psd_read_int( &data );    int h = psd_read_uchar( &data );    if( h )    p = (ptrdiff_t)((((double)p) * (magic4>>8)) / ((double)(magic4&255)));    else    p = (ptrdiff_t)((((double)p) * (magic6>>8)) / ((double)(magic6&255))); -  push_constant_text( "pos" ); push_int64( p ); -  push_constant_text( "vertical" ); push_int( !h ); +  push_static_text( "pos" ); push_int64( p ); +  push_static_text( "vertical" ); push_int( !h );    f_aggregate_mapping( 4 );    }    f_aggregate( num_guides );    }    break;    case 0x040b: /* URL */    {    struct buffer b = psd_read_pstring( &data ); -  push_constant_text( "url" ); +  push_static_text( "url" );    push_buffer( &b );    }    break;    case 0x03ed: /* res. info. */ -  push_constant_text( "resinfo" ); +  push_static_text( "resinfo" );    -  push_constant_text( "hres" ); push_int(psd_read_int( &data ) ); -  push_constant_text( "hres_unit" ); push_int(psd_read_short( &data ) ); -  push_constant_text( "width_unit" ); push_int(psd_read_short( &data ) ); +  push_static_text( "hres" ); push_int(psd_read_int( &data ) ); +  push_static_text( "hres_unit" ); push_int(psd_read_short( &data ) ); +  push_static_text( "width_unit" ); push_int(psd_read_short( &data ) );    -  push_constant_text( "vres" ); push_int(psd_read_int( &data ) ); -  push_constant_text( "vres_unit" ); push_int(psd_read_short( &data ) ); -  push_constant_text( "height_unit" );push_int(psd_read_short( &data ) ); +  push_static_text( "vres" ); push_int(psd_read_int( &data ) ); +  push_static_text( "vres_unit" ); push_int(psd_read_short( &data ) ); +  push_static_text( "height_unit" );push_int(psd_read_short( &data ) );       f_aggregate_mapping( 12 );    break;       case 0x03fc: /* Declared completely obsolete by Adobe */    case 0x03ff:    case 0x0403:    break;       case 0x03e8: /* Photoshop 2.0. Obsolete! */
pike.git/src/modules/Image/encodings/psd.c:732:    2 bytes bleed width scale. */    default:    if ((id >= 0x07d0) && (id <= 0x0bb6)) {    /* Path information */    }    push_int( id );    push_buffer( &data );    break;    }    } -  f_aggregate_mapping( DO_NOT_WARN((int)(sp-osp)) ); +  f_aggregate_mapping( (int)(sp-osp) );   }      static void image_f_psd___decode( INT32 args )   {    struct pike_string *s;    struct buffer b; -  get_all_args( "Image.PSD.___decode", args, "%S", &s ); +  get_all_args( NULL, args, "%S", &s );    if(args > 1)    pop_n_elems( args-1 );    if(s->len < 26+4+4+4 ) /* header+color mode+image res+layers */    Pike_error("This is not a Photoshop PSD file (too short)\n");    if(s->str[0] != '8' || s->str[1] != 'B'    || s->str[2] != 'P' || s->str[3] != 'S' )    Pike_error("This is not a Photoshop PSD file (invalid signature)\n");    if(s->str[4] || s->str[5] != 1)    Pike_error("This is not a Photoshop PSD file (invalid version)\n");   
pike.git/src/modules/Image/encodings/psd.c:771:    }   }      static void f_apply_cmap( INT32 args )   {    struct object *io;    struct image *i;    rgb_group *d;    struct pike_string *cmap;    int n; -  get_all_args( "apply_cmap", args, "%o%S", &io, &cmap ); +  get_all_args( NULL, args, "%o%S", &io, &cmap );    if(cmap->len < 256*3)    Pike_error("Invalid colormap resource\n"); -  if(!(i = (struct image *)get_storage( io, image_program ))) +  if(!(i = get_storage( io, image_program )))    Pike_error("Invalid image object\n");    n = i->xsize * i->ysize;    d = i->img;    THREADS_ALLOW();    while(n--)    {    int i = d->g;    d->r = cmap->str[i];    d->g = cmap->str[i+256];    d->b = cmap->str[i+512];    }    THREADS_DISALLOW(); -  pop_n_elems(args); -  push_int(0); +    }      static struct program *image_encoding_psd_program=NULL; - void init_image_psd() + void init_image_psd(void)   {    ADD_FUNCTION( "___decode", image_f_psd___decode, tFunc(tStr,tMapping), 0);    ADD_FUNCTION( "___decode_image_channel", f_decode_image_channel,tFunction,0);    ADD_FUNCTION( "___decode_image_data", f_decode_image_data,tFunction,0);    ADD_FUNCTION( "__apply_cmap", f_apply_cmap, tFunc(tObj tStr,tVoid), 0);       add_integer_constant("Bitmap" , Bitmap, 0 );    add_integer_constant("Greyscale" , Greyscale, 0 );    add_integer_constant("Indexed" , Indexed, 0 );    add_integer_constant("RGB" , RGB, 0 );
pike.git/src/modules/Image/encodings/psd.c:820:    add_integer_constant("LAYER_FLAG_BIT4", 0x04, 0 );    add_integer_constant("LAYER_FLAG_NOPIX", 0x08, 0 );      #define STRING(X) PIKE_CONCAT(s_, X) = \    make_shared_binary_string(#X,sizeof( #X )-sizeof(""))   #include "psd_constant_strings.h"   #undef STRING   }       - void exit_image_psd() + void exit_image_psd(void)   {   #define STRING(X) free_string(PIKE_CONCAT(s_, X))   #include "psd_constant_strings.h"   #undef STRING   }