Пример #1
0
// Load object from stream
void FXGLVertices::load(FXStream& store){
  FXGLShape::load(store);
  store >> vertexNumber;
  FXMALLOC(&vertices, FXVec3f, vertexNumber);
  store.load((FXfloat *) vertices, 3*vertexNumber);
  store >> pointSize >> color;
  }
Пример #2
0
// load object from stream
void FXBaseObject::load(FXStream& store) {
    FXObject::load(store);
    store >> app;
    store >> target;
    store >> message;
    store >> flags;
    store >> options;
    store >> datalen;
    store.load((FXuchar*)data, (unsigned long)datalen);
}
Пример #3
0
// Load a JPEG image
FXbool fxloadJP2(FXStream& store,FXColor*& data,FXint& width,FXint& height,FXint&){
  register FXint x,y,cw,rsh,gsh,bsh,ash,rof,gof,bof,aof;
  register FXuchar r,g,b,a;
  FXbool swap=store.swapBytes();
  FXlong pos=store.position();
  FXbool result=false;
  FXuint box[4];
  FXlong boxsize;
  FXuint size;
  FXuchar *ptr;

  // Null out
  data=NULL;
  width=0;
  height=0;

  // Switch big-endian to grab header
  store.setBigEndian(true);

  // Grab signature
  store.load(box,3);

  // Check signature, bail quickly if no match
  if(box[0]==12 && box[1]==BOX_JP && box[2]==SIGNATURE){

    // Figure size
    store.position(0,FXFromEnd);
    size=store.position()-pos;
    store.position(pos);

    FXTRACE((90,"fxloadJP2: file size=%d\n",size));

    // Allocate chunk for file data
    if(allocElms(ptr,size)){

      // Load entire file
      store.load(ptr,size);

      // Create decompressor
      opj_dinfo_t *decompressor=opj_create_decompress(CODEC_JP2);
      if(decompressor){
        opj_dparameters_t     parameters;
        opj_event_mgr_t       event_mgr;
        opj_cio_t            *cio=NULL;
        opj_image_t          *image=NULL;

        // Set up callbacks
        event_mgr.error_handler=j2k_error_callback;
        event_mgr.warning_handler=j2k_warning_callback;
        event_mgr.info_handler=j2k_info_callback;

        // Set event manager
        opj_set_event_mgr((opj_common_ptr)decompressor,&event_mgr,NULL);

        // Initialize decompression parameters
        opj_set_default_decoder_parameters(&parameters);

        // Setup the decoder decoding parameters using user parameters
        opj_setup_decoder(decompressor,&parameters);

        // Open a byte stream */
        cio=opj_cio_open((opj_common_ptr)decompressor,ptr,size);
        if(cio){

          // Decode the stream and fill the image structure
          image=opj_decode(decompressor,cio);
          if(image){

            // Image size
            width=image->x1-image->x0;
            height=image->y1-image->y0;

            FXTRACE((90,"fxloadJP2: width=%d height=%d numcomps=%d color_space=%d\n",width,height,image->numcomps,image->color_space));

            // Only support GREY, RGB, and RGBA
            if(((image->numcomps==1) && (image->color_space==CLRSPC_GRAY)) || ((image->numcomps==3 || image->numcomps==4) && (image->color_space==CLRSPC_SRGB))){

              // Allocate image data
              if(allocElms(data,width*height)){
                rof=gof=bof=aof=rsh=gsh=bsh=ash=0;
                switch(image->numcomps){
                  case 1:
                    if(image->comps[0].sgnd) gof=1<<(image->comps[0].prec-1);
                    gsh=image->comps[0].prec-8;
                    cw=image->comps[0].w;
                    for(y=0; y<height; ++y){
                      for(x=0; x<width; ++x){
                        g=(image->comps[0].data[y*cw+x]+gof)>>gsh;
                        data[y*width+x]=FXRGB(g,g,g);
                        }
                      }
                    break;
                  case 3:
                    if(image->comps[0].sgnd) rof=1<<(image->comps[0].prec-1);
                    if(image->comps[1].sgnd) gof=1<<(image->comps[1].prec-1);
                    if(image->comps[2].sgnd) bof=1<<(image->comps[2].prec-1);
                    rsh=image->comps[0].prec-8;
                    gsh=image->comps[1].prec-8;
                    bsh=image->comps[2].prec-8;
                    cw=image->comps[0].w;
                    for(y=0; y<height; ++y){
                      for(x=0; x<width; ++x){
                        r=(image->comps[0].data[y*cw+x]+rof)>>rsh;
                        g=(image->comps[1].data[y*cw+x]+gof)>>gsh;
                        b=(image->comps[2].data[y*cw+x]+bof)>>bsh;
                        data[y*width+x]=FXRGB(r,g,b);
                        }
                      }
                    break;
                  default:
                    if(image->comps[0].sgnd) rof=1<<(image->comps[0].prec-1);
                    if(image->comps[1].sgnd) gof=1<<(image->comps[1].prec-1);
                    if(image->comps[2].sgnd) bof=1<<(image->comps[2].prec-1);
                    if(image->comps[3].sgnd) aof=1<<(image->comps[3].prec-1);
                    rsh=image->comps[0].prec-8;
                    gsh=image->comps[1].prec-8;
                    bsh=image->comps[2].prec-8;
                    ash=image->comps[3].prec-8;
                    cw=image->comps[0].w;
                    for(y=0; y<height; ++y){
                      for(x=0; x<width; ++x){
                        r=(image->comps[0].data[y*cw+x]+rof)>>rsh;
                        g=(image->comps[1].data[y*cw+x]+gof)>>gsh;
                        b=(image->comps[2].data[y*cw+x]+bof)>>bsh;
                        a=(image->comps[3].data[y*cw+x]+aof)>>ash;
                        data[y*width+x]=FXRGBA(r,g,b,a);
                        }
                      }
                    break;
                  }
                result=true;
                }
              }
            opj_image_destroy(image);
            }
          opj_cio_close(cio);
          }
        opj_destroy_decompress(decompressor);
        }
      freeElms(ptr);
      }
    }
Пример #4
0
// Check if stream contains a JPG
FXbool fxcheckJP2(FXStream& store){
  FXuchar ss[12];
  store.load(ss,12);
  store.position(-12,FXFromCurrent);
  return ss[0]==0 && ss[1]==0 && ss[2]==0 && ss[3]==12 && ss[4]=='j' && ss[5]=='P' && ss[6]==' ' && ss[7]==' ' && ss[8]==0x0D && ss[9]==0x0A && ss[10]==0x87 && ss[11]==0x0A;
  }
Пример #5
0
// Load image from stream
FXbool fxloadXPM(FXStream& store,FXColor*& data,FXint& width,FXint& height){
  FXchar lookuptable[1024][8],line[100],name[100],word[100],flag,best,ch;
  FXColor colortable[16384],*pix,color;
  const FXchar *src;
  FXint i,j,ncolors,cpp,c;

  // Null out
  data=NULL;
  width=0;
  height=0;
  color=0;

  // Read header line
  readline(store,name,sizeof(name));
  if(!strstr(name,"XPM")) return false;

  // Read description
  readtext(store,line,sizeof(line));

  // Parse size description
  if(__sscanf(line,"%d %d %u %u",&width,&height,&ncolors,&cpp)!=4) return false;

  // Check size
  if(width<1 || height<1 || width>16384 || height>16384) return false;

  // Sensible inputs
  if(cpp<1 || cpp>8 || ncolors<1) return false;

  // Limited number of colors for long lookup strings
  if(cpp>2 && ncolors>1024) return false;

  // Allow more colors for short lookup strings
  if(ncolors>16384) return false;

  //FXTRACE((100,"fxloadXPM: width=%d height=%d ncolors=%d cpp=%d\n",width,height,ncolors,cpp));

  // Read the color table
  for(c=0; c<ncolors; c++){
    readtext(store,line,sizeof(line));
    src=line+cpp;
    nextword(src,word);
    best='z';
    while(iskey(word)){
      flag=word[0];
      name[0]=0;
      while(nextword(src,word) && !iskey(word)){
        strncat(name,word,sizeof(name));
        }
      if(flag<best){                    // c < g < m < s
        color=fxcolorfromname(name);
        best=flag;
        }
      }
    if(cpp==1){
      colortable[(FXuchar)line[0]]=color;
      }
    else if(cpp==2){
      colortable[(((FXuchar)line[1])<<7)+(FXuchar)line[0]]=color;
      }
    else{
      colortable[c]=color;
      strncpy(lookuptable[c],line,cpp);
      }
    }

  // Try allocate pixels
  if(!allocElms(data,width*height)){
    return false;
    }

  // Read the pixels
  for(i=0,pix=data; i<height; i++){
    while(!store.eof() && (store>>ch,ch!='"')){}
    for(j=0; j<width; j++){
      store.load(line,cpp);
      if(cpp==1){
        color=colortable[(FXuchar)line[0]];
        }
      else if(cpp==2){
        color=colortable[(((FXuchar)line[1])<<7)+(FXuchar)line[0]];
        }
      else{
        for(c=0; c<ncolors; c++){
          if(strncmp(lookuptable[c],line,cpp)==0){ color=colortable[c]; break; }
          }
        }
      *pix++=color;
      }
    while(!store.eof() && (store>>ch,ch!='"')){}
    }

  // We got the image, but we're not done yet; need to read few more bytes
  // the number of bytes read here must match the number of bytes written
  // by fxsaveXPM() so that the stream won't get out of sync
  while(!store.eof()){
    store >> ch;
    if(ch=='\n') break;
    }
  return true;
  }
Пример #6
0
// Check if stream contains a PNG
bool fxcheckPNG(FXStream& store){
  FXuchar signature[8];
  store.load(signature,8);
  store.position(-8,FXFromCurrent);
  return signature[0]==137 && signature[1]==80 && signature[2]==78 && signature[3]==71 && signature[4]==13 && signature[5]==10 && signature[6]==26 && signature[7]==10;
  }
Пример #7
0
// Check if stream contains a TIFF
bool fxcheckTIF(FXStream& store) {
    FXuchar signature[2];
    store.load(signature,2);
    store.position(-2,FXFromCurrent);
    return (signature[0]==0x4d && signature[1]==0x4d) || (signature[0]==0x49 && signature[1]==0x49);
}
Пример #8
0
// Load image from stream
bool fxloadRGB(FXStream& store,FXColor*& data,FXint& width,FXint& height){
  FXint i,j,c,tablen,sub,t,total;
  FXuchar temp[4096],*array,storage,bpc,swap;
  FXuint *starttab,*lengthtab;
  FXushort magic,dimension,nchannels,w,h;
  FXlong base,start;

  // Null out
  data=NULL;
  width=0;
  height=0;

  // Remember swap state
  swap=store.swapBytes();
  store.setBigEndian(TRUE);

  // Where the image format starts
  base=store.position();

  // Load header
  store >> magic;       // MAGIC (2)
  store >> storage;     // STORAGE (1)
  store >> bpc;         // BPC (1)
  store >> dimension;   // DIMENSION (2)
  store >> w;           // XSIZE (2)
  store >> h;           // YSIZE (2)
  store >> nchannels;   // ZSIZE (2)

  FXTRACE((50,"fxloadRGB: magic=%d width=%d height=%d nchannels=%d dimension=%d storage=%d bpc=%d\n",magic,w,h,nchannels,dimension,storage,bpc));

  // Check magic number and other parameters
  if(magic==474 && nchannels==3 && bpc==1 && w>0 && h>0){

    // Make room for image
    if(FXMALLOC(&data,FXColor,w*h)){

      // Clear
      memset(data,0xff,sizeof(FXColor)*w*h);

      // Skip stuff
      store.position(500,FXFromCurrent);

      // RLE compressed
      if(storage){
        tablen=h*3;

        // Allocate line tables
        if(FXMALLOC(&starttab,FXuint,tablen*2)){
          lengthtab=&starttab[tablen];

          // Read line tables
          store.load(starttab,tablen);
          store.load(lengthtab,tablen);

          // Where the RLE chunks start
          start=store.position();

          // Substract this amount to get offset from chunk start
          sub=start-base;

          total=0;

          // Fix up the line table & figure space for RLE chunks
          // Intelligent RGB writers (not ours ;-)) may re-use RLE
          // chunks for more than 1 line...
          for(i=0; i<tablen; i++){
            starttab[i]-=sub;
            t=starttab[i]+lengthtab[i];
            if(t>total) total=t;
            }

          // Make room for the compressed lines
          if(FXMALLOC(&array,FXuchar,total)){

            // Load all RLE chunks
            store.load(array,total);
            for(c=0; c<3; c++){
              for(j=h-1; j>=0; j--){
                expandrow(((FXuchar*)(data+j*w))+c,&array[starttab[h-1-j+c*h]]);
                }
              }

            // Free RLE chunks
            FXFREE(&array);
            }

          // Free line tables
          FXFREE(&starttab);
          }
        }

      // NON compressed
      else{
        for(c=0; c<3; c++){
          for(j=h-1; j>=0; j--){
            store.load(temp,w);
            for(i=0; i<w; i++) ((FXuchar*)(data+j*w+i))[c]=temp[i];
            }
          }
        }

      // Set width and height
      width=w;
      height=h;

      // Reset swap status
      store.swapBytes(swap);
      return TRUE;
      }
    }

  // Reset swap status
  store.swapBytes(swap);
  return false;
  }
Пример #9
0
// Check if stream contains a RGB
bool fxcheckRGB(FXStream& store){
  FXuchar signature[2];
  store.load(signature,2);
  store.position(-2,FXFromCurrent);
  return signature[0]==0x01 && signature[1]==0xDA;
  }
Пример #10
0
// Load SUN raster image file format
bool fxloadRAS(FXStream& store,FXColor*& data,FXint& width,FXint& height){
  FXuchar red[256],green[256],blue[256],*line,count,c,*p,*q,bit;
  register FXint npixels,depth,linesize,x,y,i;
  HEADER header;

  // Null out
  data=NULL;
  line=NULL;
  width=0;
  height=0;

  // Read header
  header.magic=read32(store);
  header.width=read32(store);
  header.height=read32(store);
  header.depth=read32(store);
  header.length=read32(store);
  header.type=read32(store);
  header.maptype=read32(store);
  header.maplength=read32(store);

  //FXTRACE((1,"fxloadRAS: magic=%08x width=%d height=%d depth=%d length=%d type=%d maptype=%d maplength=%d\n",header.magic,header.width,header.height,header.depth,header.length,header.type,header.maptype,header.maplength));

  // Check magic code
  if(header.magic!=RAS_MAGIC) return false;

  // Trivial reject
  if(header.width<1 || header.height<1) return false;

  // Bad colormap size
  if(header.maplength<0 || header.maplength>768) return false;

  // Verify depth options; must be 1,8,24, or 32
  if(header.depth!=1 && header.depth!=8 && header.depth!=24 && header.depth!=32) return false;

  // Verify supported types
  if(header.type!=RT_OLD && header.type!=RT_STANDARD && header.type!=RT_BYTE_ENCODED && header.type!=RT_FORMAT_RGB) return false;

  // Verify map types
  if(header.maptype!=RMT_RAW && header.maptype!=RMT_NONE && header.maptype!=RMT_EQUAL_RGB) return false;

  // Get size
  width=header.width;
  height=header.height;
  depth=header.depth;
  npixels=width*height;
  linesize=((width*depth+15)/16)*2;

  //FXTRACE((1,"fxloadRAS: header.length=%d linesize=%d 4*npixels=%d\n",header.length,linesize,4*npixels));

  // Read in the colormap
  if(header.maptype==RMT_EQUAL_RGB && header.maplength){
    //FXTRACE((1,"fxloadRAS: RMT_EQUAL_RGB\n"));
    store.load(red,header.maplength/3);
    store.load(green,header.maplength/3);
    store.load(blue,header.maplength/3);
    }

  // Skip colormap
  else if(header.maptype==RMT_RAW && header.maplength){
    //FXTRACE((1,"fxloadRAS: RMT_RAW\n"));
    store.position(header.maplength,FXFromCurrent);
    }

  // Black and white
  else if(header.depth==1){
    //FXTRACE((1,"fxloadRAS: 1 bit\n"));
    red[0]=green[0]=blue[0]=0;
    red[1]=green[1]=blue[1]=255;
    }

  // Gray scale
  else if(header.depth==8){
    //FXTRACE((1,"fxloadRAS: 8 bit\n"));
    for(i=0; i<256; i++){
      red[i]=green[i]=blue[i]=i;
      }
    }

  // Allocate pixel data
  if(!FXMALLOC(&data,FXColor,npixels)) return false;

  // Allocate scanline
  if(!FXMALLOC(&line,FXuchar,linesize)){ FXFREE(&data); return false; }

  // Now read the image
  for(y=0,p=(FXuchar*)data,count=c=0; y<height; y++){
    if(header.type!=RT_BYTE_ENCODED){           // Load uncompressed
      store.load(line,linesize);
      }
    else{
      for(i=0; i<linesize; i++){                // Load RLE compressed
        if(count){
          line[i]=c;
          count--;
          }
        else{
          store >> c;
          if(c==0x80){
            store >> count;
            if(count==0){
              line[i]=0x80;
              }
            else{
              store >> c;
              line[i]=c;
              }
            }
          else{
            line[i]=c;
            }
          }
        }
      }