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

version» Context lines:

pike.git/src/modules/Image/encodings/ras.c:1:   /*   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information.   */      /*   **! module Image   **! submodule RAS   **! - **! This submodule keep the RAS encode/decode capabilities - **! of the <ref>Image</ref> module. + **! This submodule keep the RAS (Sun Raster, files commonly use the + **! 'sun' file extension) encode/decode capabilities of the + **! <ref>Image</ref> module.   **! -  + **! The format was commonly used in the 1990:s for images in reasearch + **! papers and such, since SUN workstations was the standard computer + **! in university settings. + **! + **! The format uses RLE compression, supports rgb24, 8-bit + **! palette, grayscale and monochrome without alpha channels. + **!   **! see also: Image, Image.Image, Image.Colortable   */   #include "global.h"      #include "stralloc.h"   #include "object.h"   #include "interpret.h"   #include "svalue.h"   #include "mapping.h"   #include "pike_error.h"
pike.git/src/modules/Image/encodings/ras.c:107:    --dstlen;    }    return dst-dst0;   }      void img_ras__decode(INT32 args)   {    /* Double check args to give the correct function name in the error    messages. */    if(args<1) -  SIMPLE_TOO_FEW_ARGS_ERROR("Image.RAS._decode", 1); +  SIMPLE_WRONG_NUM_ARGS_ERROR("_decode", 1);    if(TYPEOF(Pike_sp[-1]) != T_STRING) -  SIMPLE_BAD_ARG_ERROR("Image.RAS._decode", 1, "string"); +  SIMPLE_ARG_TYPE_ERROR("_decode", 1, "string");    img_ras_decode(args); -  push_constant_text("image"); +  push_static_text("image");    stack_swap(); -  push_constant_text("format"); -  push_constant_text("image/x-sun-raster"); +  push_static_text("format"); +  push_static_text("image/x-sun-raster");    f_aggregate_mapping(4);   }      void img_ras_decode(INT32 args)   {    struct pike_string *str;    struct rasterfile rs;    struct object *o, *ctab=NULL;    struct image *img;    rgb_group *rgb;    unsigned char *src, *tmpdata=NULL;    ptrdiff_t len;    INT32 x, y;    size_t numcolors = 0;    struct nct_flat_entry *entries = NULL;    -  get_all_args("Image.RAS.decode", args, "%S", &str); +  get_all_args(NULL, args, "%S", &str);       if(str->len < 32)    Pike_error("Image.RAS.decode: header too small\n");       decode_ras_header(&rs, STR0(str));       if(rs.ras_magic != 0x59a66a95)    Pike_error("Image.RAS.decode: bad magic\n");       if(rs.ras_type < 0 || rs.ras_type > RT_BYTE_ENCODED)
pike.git/src/modules/Image/encodings/ras.c:229:    case 1:    img_sz = ((rs.ras_width+15)>>4)*2*rs.ras_height;    break;    case 8:    img_sz = ((rs.ras_width+1)&~1)*rs.ras_height;    break;    case 24:    img_sz = ((rs.ras_width+1)&~1)*3*rs.ras_height;    break;    } -  tmpdata = (unsigned char *)xalloc(img_sz); +  tmpdata = xalloc(img_sz);    len = unpack_rle(src, len, tmpdata, img_sz);    src = tmpdata;    }       push_int(rs.ras_width);    push_int(rs.ras_height);    o=clone_object(image_program,2); -  img=(struct image*)get_storage(o,image_program); +  img=get_storage(o,image_program);    rgb=img->img;    if(ctab != NULL) {    struct neo_colortable *ctable = -  (struct neo_colortable*)get_storage(ctab, image_colortable_program); +  get_storage(ctab, image_colortable_program);    if(ctable!=NULL && ctable->type==NCT_FLAT) {    numcolors = ctable->u.flat.numentries;    entries = ctable->u.flat.entries;    }    }       for(y=0; y<rs.ras_height; y++)    switch(rs.ras_depth) {    case 24:    for(x=0; x<rs.ras_width; x++) {    if(len<3) {    /* Better to proceed and make a partly black image? */    if(tmpdata != NULL) -  free((char *)tmpdata); +  free(tmpdata);    if(ctab != NULL)    free_object(ctab);    free_object(o);    Pike_error("Image.RAS.decode: image data too short\n");    }    rgb->b = *src++;    rgb->g = *src++;    rgb->r = *src++;    rgb++;    len -= 3;
pike.git/src/modules/Image/encodings/ras.c:277:    if(rs.ras_width & 1) {    src++;    --len;    }    break;    case 8:    for(x=0; x<rs.ras_width; x++) {    if(len<1) {    /* Better to proceed and make a partly black image? */    if(tmpdata != NULL) -  free((char *)tmpdata); +  free(tmpdata);    if(ctab != NULL)    free_object(ctab);    free_object(o);    Pike_error("Image.RAS.decode: image data too short\n");    }    if(*src<numcolors)    *rgb++ = entries[*src++].color;    else {    rgb++;    src++;
pike.git/src/modules/Image/encodings/ras.c:304:    }    break;    case 1:    {    int bits = 0, data = 0;    for(x=0; x<rs.ras_width; x++) {    if(!bits) {    if(len<2) {    /* Better to proceed and make a partly black image? */    if(tmpdata != NULL) -  free((char *)tmpdata); +  free(tmpdata);    if(ctab != NULL)    free_object(ctab);    free_object(o);    Pike_error("Image.RAS.decode: image data too short\n");    }    data = (src[0]<<8)|src[1];    src += 2;    len -= 2;    bits = 16;    }
pike.git/src/modules/Image/encodings/ras.c:338:    }    if(rs.ras_width & 1) {    src++;    --len;    }    }    break;    }       if(tmpdata != NULL) -  free((char *)tmpdata); +  free(tmpdata);    if(ctab != NULL)    free_object(ctab);    pop_n_elems(args);    push_object(o);   }         /** encoding *****************************************/      /*
pike.git/src/modules/Image/encodings/ras.c:426:    struct neo_colortable *ct = NULL;    struct pike_string *res, *res2;    struct rasterfile rs;    struct nct_dither dith;    rgb_group *rgb;    INT32 x, y, llen = 0;    unsigned char *dst;    void (*ctfunc)(rgb_group *, unsigned char *, int,    struct neo_colortable *, struct nct_dither *, int) = NULL;    -  get_all_args("Image.RAS.decode", args, +  get_all_args(NULL, args,    (args>1 && !UNSAFE_IS_ZERO(&sp[1-args])? "%o%m":"%o"),    &imgo, &optm);    -  if((img=(struct image*)get_storage(imgo, image_program))==NULL) +  if((img=get_storage(imgo, image_program))==NULL)    Pike_error("Image.RAS.encode: illegal argument 1\n");       if(optm != NULL) {    struct svalue *s;       if((s=simple_mapping_string_lookup(optm, "palette"))!=NULL && !UNSAFE_IS_ZERO(s))    if(TYPEOF(*s) != T_OBJECT || -  (ct=(struct neo_colortable*) -  get_storage(s->u.object, image_colortable_program))==NULL) +  (ct=get_storage(s->u.object, image_colortable_program))==NULL)    Pike_error("Image.RAS.encode: option (arg 2) \"palette\" has illegal type\n");    }       if (!img->img)    Pike_error("Image.RAS.encode: no image\n");       rgb = img->img;       if(ct && ct->type == NCT_NONE)    ct = NULL;
pike.git/src/modules/Image/encodings/ras.c:466:    rs.ras_type = RT_STANDARD;    rs.ras_maptype = RMT_NONE;    rs.ras_maplength = 0;       if(ct) {    struct pike_string *cts;    ptrdiff_t i, n = image_colortable_size(ct);    unsigned char *tmp;    rs.ras_depth = 8;    rs.ras_maptype = RMT_EQUAL_RGB; -  rs.ras_maplength = DO_NOT_WARN((INT32)n*3); +  rs.ras_maplength = (INT32)n*3;    cts = begin_shared_string(rs.ras_maplength+(rs.ras_maplength&1));    if(rs.ras_maplength & 1) {    STR0(cts)[rs.ras_maplength] = '\0';    rs.ras_maplength++;    } -  tmp = (unsigned char *)xalloc(rs.ras_maplength); +  tmp = xalloc(rs.ras_maplength);    image_colortable_write_rgb(ct, tmp);    for(i=0; i<n; i++) {    STR0(cts)[i] = tmp[i*3];    STR0(cts)[i+n] = tmp[i*3+1];    STR0(cts)[i+2*n] = tmp[i*3+2];    } -  free((char *)tmp); +  free(tmp);    push_string(end_shared_string(cts));    image_colortable_initiate_dither(ct, &dith, img->xsize);    ctfunc = image_colortable_index_8bit_function(ct);    } else    push_empty_string();       if(!rs.ras_depth) {    INT32 px = img->xsize * img->ysize;    while(px--)    if((rgb[px].r != 0 || rgb[px].g != 0 || rgb[px].b != 0) &&
pike.git/src/modules/Image/encodings/ras.c:549:    rgb++;    }    if(img->xsize&1)    *dst++ = 0;    break;    }    if(ct != NULL)    image_colortable_free_dither(&dith);       { -  unsigned char *pkdata = (unsigned char *)xalloc(rs.ras_length+16); +  unsigned char *pkdata = xalloc(rs.ras_length+16);    unsigned char *pk = pkdata, *src = STR0(res2);    ptrdiff_t pklen = 0, pkleft = rs.ras_length+16;    for(y=0; y<img->ysize; y++) {    ptrdiff_t n = pack_rle(src, llen, pk, pkleft);    src += llen;    pk += n;    pkleft -= n;    if((pklen += n)>rs.ras_length)    break;    }       if(pklen<rs.ras_length) {    do_free_unlinked_pike_string(res2);    res2 = make_shared_binary_string((char *)pkdata, pklen); -  rs.ras_length = DO_NOT_WARN((INT32)pklen); +  rs.ras_length = (INT32)pklen;    rs.ras_type = RT_BYTE_ENCODED;    } else    res2 = end_shared_string(res2);       free(pkdata);    }       res = begin_shared_string(32);    encode_ras_header(&rs, STR0(res));    push_string(end_shared_string(res));