Exemple #1
0
int	main(int argc, char *argv[])
{
	FILE		*fp;
	char		in_filename[1024], out_filename[1024];
	char		field[128];
	char		header_bytes[6];
	int		data_size, header_size_char;
	int		cbf_status;
	int		i, j, k;
	int		size1, size2;
	int		output_packing;
	int		status_pclose;
	char		*header;
	unsigned short	*data;
	 char *    lbo; /* local byte order */   
	static char	*endings[] = {
					".img",
					".img.gz",
					".img.bz2",
					NULL
				     };
	static char	*flags[] = {
					"--gz",
					"--bz2",
					NULL
				   };

	int		cbf2adscimg_sub(char *filename, char **header, unsigned short **data);

	if(argc < 2)
	{
		usage();
		exit(0);
	}

	output_packing = -1;

	while(argc > 1 && argv[1][0] == '-' && argv[1][1] == '-')
	{
		for(j = 0; flags[j] != NULL; j++)
			if(NULL != strstr(argv[1], flags[j]))
				break;
		if(NULL == flags[j])
		{
			fprintf(stderr,"cbf2adscimg: %s is an unknown flag\n\n", argv[1]);
			usage();
			exit(0);
		}
		output_packing = j;
		argc--;
		argv++;
	}
	output_packing++;

	while(argc > 1)
	{
		strcpy(in_filename, argv[1]);
		i = strlen(in_filename);
		k = strlen(".cbf");
		if(0 == endswith(in_filename, ".cbf"))
		{
			fprintf(stderr,"cbf2adscimg: Input file name %s does not end in .cbf\n", in_filename);
			exit(0);
		}
		strcpy(out_filename, in_filename);
		out_filename[i - k] = '\0';
		strcat(out_filename, endings[output_packing]);

		cbf_status = cbf2adscimg_sub(in_filename, &header, &data);

		if(0 != cbf_status)
		{
			fprintf(stderr, "cbf2adscimg: Error converting cbf file %s to .img format\n", in_filename);
			exit(0);
		}
		if(NULL == header)
		{
			fprintf(stderr, "cbf2adscimg: Error: cbf2adscimg_sub returned NULL for header on file %s\n", in_filename);
			exit(0);
		}

		if(0 == output_packing)
		{
			if(NULL == (fp = fopen(out_filename, "wb")))
			{
				fprintf(stderr, "cbf2adscimg: Cannot create %s as output .img file\n", out_filename);
				exit(0);
			}
		}
		else
		{
			if(0 == output_packing)
				sprintf(popen_command, "gzip > %s", out_filename);
			else
				sprintf(popen_command, "bzip2 > %s", out_filename);
			if(NULL == (fp = popen(popen_command, "w")))
			{
				fprintf(stderr, "cbf2adscimg: Cannot exec %s command to compress output image file.\n", popen_command);
				exit(0);
			}
		}

		/*
		 *	Output the header block(s).
		 */

		for(i = 0; i < 5; i++)
			header_bytes[0 + i] = header[15 + i];

		header_bytes[5] = '\0';
		header_size_char = atoi(header_bytes);
		

		if(header_size_char != fwrite(header, sizeof (char), header_size_char, fp))
		{
			fprintf(stderr, "cbf2adscimg: Cannot write header, size %d bytes, of file %s.\n", header_size_char, in_filename);
			if(0 == output_packing)
				fclose(fp);
			else
			{
				status_pclose = pclose(fp);
				if(0 != status_pclose)
				{
					fprintf(stderr, "Status returned from compress command via popen NON-ZERO: %d\n", status_pclose);
					perror("popen command (maybe this will be useful)");
					fprintf(stderr, "Filename being compressed: %s with command: %s\n", out_filename, popen_command);
					fprintf(stderr, "Program exiting.\n");
					exit(0);
				}
			}
			exit(0);
		}

		field[0] = '\0';
		gethd("SIZE1", field, header);
		if('\0' == field[0])
		{
			fprintf(stderr,"cbf2adscimg: keyword SIZE1 not found in header.  Cannot convert file %s\n", in_filename);
			exit(0);
		}
		size1 = atoi(field);
		
		field[0] = '\0';
		gethd("SIZE2", field, header);
		if('\0' == field[0])
		{
			fprintf(stderr,"cbf2adscimg: keyword SIZE2 not found in header.  Cannot convert file %s\n", in_filename);
			exit(0);
		}
		size2 = atoi(field);
		
		data_size = size1 * size2 * sizeof(unsigned short);

		gethd("BYTE_ORDER", field, header);

		cbf_get_local_integer_byte_order(&lbo);

		if (cbf_cistrcmp(field,lbo)) {
                  unsigned char *p;
		  unsigned char temp;
		  size_t ii;
		  p = (unsigned char *)data;
		  for (ii=0; ii<data_size; ii+=2) {
		    temp = p[ii];
		    p[ii] = p[ii+1];
		    p[ii+1] = temp;
                  }
		}

		if(data_size != fwrite(data, sizeof (char), data_size, fp))
		{
			fprintf(stderr, "cbf2adscimg: Cannot write data (size %d bytes) from input file %s.\n", 
					data_size, in_filename);
			if(0 == output_packing)
				fclose(fp);
			else
			{
				status_pclose = pclose(fp);
				if(0 != status_pclose)
				{
					fprintf(stderr, "Status returned from compress command via popen NON-ZERO: %d\n", status_pclose);
					perror("popen command (maybe this will be useful)");
					fprintf(stderr, "Filename being compressed: %s with command: %s\n", out_filename, popen_command);
					fprintf(stderr, "Program exiting.\n");
					exit(0);
				}
			}
			exit(0);
		}
		if(0 == output_packing)
			fclose(fp);
		else
		{
			status_pclose = pclose(fp);
			if(0 != status_pclose)
			{
				fprintf(stderr, "Status returned from compress command via popen NON-ZERO: %d\n", status_pclose);
				perror("popen command (maybe this will be useful)");
				fprintf(stderr, "Filename being compressed: %s with command: %s\n", out_filename, popen_command);
				fprintf(stderr, "Program exiting.\n");
				exit(0);
			}
		}


		free(header);
		free(data);

		argv++;
		argc--;
	}
	exit(0);
}
int cbf_compress_none (void         *source,
                       size_t        elsize,
                       int           elsign,
                       size_t        nelem,
                       unsigned int  compression,
                       cbf_file     *file,
                       size_t       *compressedsize,
                       int          *storedbits,
                       int           realarray,
                       const char   *byteorder,
                       size_t        dimfast,
                       size_t        dimmid,
                       size_t        dimslow,
                       size_t        padding)
{
  unsigned int count, element[4], unsign, sign, limit, bits;

  unsigned char *unsigned_char_data;

  int numints, iint;

  char * border;

  char * rformat;



    /* Is the element size valid? */

  if (elsize != sizeof (int) &&
      elsize != 2* sizeof (int) &&
      elsize != 4* sizeof (int) &&
      elsize != sizeof (short) &&
      elsize != sizeof (char))

    return CBF_ARGUMENT;

    /* check for compatible real format */

  if ( realarray ) {

    cbf_failnez (cbf_get_local_real_format(&rformat) )

    if ( strncmp(rformat,"ieee",4) ) return CBF_ARGUMENT;

  }

   bits = elsize * CHAR_BIT;

   if (bits < 1 || bits > 64)

     return CBF_ARGUMENT;

   numints = (bits + CHAR_BIT*sizeof (int) -1)/(CHAR_BIT*sizeof (int));


    /* Maximum limits */

  sign = 1 << ((elsize-(numints-1)*sizeof(int))* CHAR_BIT - 1);

  if (elsize == sizeof (int) || elsize == numints*sizeof(int) )

    limit = ~0;

  else

    if (numints == 1) {

      limit = ~-(1 << (elsize * CHAR_BIT));

    } else {

      limit = ~-(1 << ((elsize-(numints-1)*sizeof(int)) * CHAR_BIT));

    }


  if (storedbits)

    *storedbits = bits;


    /* Offset to make the value unsigned */

  if (elsign)

    unsign = sign;

  else

    unsign = 0;

    /* Get the local byte order */

  if (realarray) {

    cbf_get_local_real_byte_order(&border);

  } else {

    cbf_get_local_integer_byte_order(&border);

  }


    /* Initialise the pointer */

  unsigned_char_data = (unsigned char *) source;


    /* Write the elements */
    
    /* Try for a fast memory-memory transfer */
    
  switch (elsize) {
  
  	case (1):

  	  if (!cbf_set_output_buffersize(file,nelem))  {

  	    memmove((void *)(file->characters+file->characters_used),
  	      (void *)unsigned_char_data,nelem);

  	    file->characters_used+=nelem;

  	    if (compressedsize)

          *compressedsize = nelem;
        
        return 0;
      
  	  }
  	  
  	  break;
  	
  	case (2):
  	
  	case (4):
  	
  	case (8):
  	
    	if (!cbf_set_output_buffersize(file,nelem*elsize)) {

  	      if (toupper(border[0]) == toupper(byteorder[0])) {
  	      
 	        memmove((void *)(file->characters+file->characters_used),
  	          (void *)unsigned_char_data,nelem*elsize);
  	          
  	      } else {
  	      
  	        if ((elsize == 4 || elsize == 8) && sizeof(short int) !=2 ) break;

  	        if (elsize == 8 && sizeof(int) !=4 ) break;

  	        cbf_swab((void *)unsigned_char_data,
  	          (void *)(file->characters+file->characters_used),
  	          nelem*elsize);
  	          
  	        if (elsize == 4 || elsize == 8) {
  	        
  	          short int temp;
  	          
  	          short int *sint;
  	          
  	          sint = (short int *)(file->characters+file->characters_used);
  	          
  	          for (count = 0; count < elsize * nelem; count+= 4) {
  	          
  	            temp = *sint;
  	            
  	            *sint = sint[1];
  	            
  	            sint[1] = temp;
  	            
  	            sint+=2; 	              
  	          	
  	          }
  	        	
  	        } 
  	        
  	        if (elsize == 8) {
  	        
    	      int temp;
  	          
  	          int *oint;
  	          
  	          oint = (int *)(file->characters+file->characters_used);
  	          
  	          for (count = 0; count < elsize * nelem; count+= 8) {
  	          
  	            temp = *oint;
  	            
  	            *oint = oint[1];
  	            
  	            oint[1] = temp;
  	            
  	            oint+=2; 	              
  	          	
  	          }
	         
  	        }
  	            	      	
  	      }
  	        
  	      file->characters_used+=nelem*elsize;
  	  	
  	      if (compressedsize)

          *compressedsize = nelem*elsize;
        
          return 0;
  	  }

  	break;
  	
  	default:
  	break;
  }
  
  /* If we got here, we will do it the slow, painful way */

  for (count = 0; count < nelem; count++)
  {
      /* Get the next element */

    if (numints > 1) {

      if (border[0] == 'b') {

        for (iint = numints; iint; iint--) {

            element[iint-1] = *((unsigned int *) unsigned_char_data);

            unsigned_char_data += sizeof (int);

        }

      } else {

        for (iint = 0; iint < numints; iint++) {

            element[iint] = *((unsigned int *) unsigned_char_data);

            unsigned_char_data += sizeof (int);
        }
      }

    } else {

    if (elsize == sizeof (int))

      element[0] = *((unsigned int *) unsigned_char_data);

    else

      if (elsize == sizeof (short))

        element[0] = *((unsigned short *) unsigned_char_data);

      else

        element[0] = *unsigned_char_data;

    unsigned_char_data += elsize;

    }


      /* Make the element unsigned */

    element[numints-1] += unsign;
    
    element[numints-1] &= limit;



      /* Write the element to the file */

    element[numints-1] -= unsign;

    if (numints > 1) {

      for (iint = 0; iint < numints; iint++) {

        cbf_failnez (cbf_put_integer (file, element[iint], 0,
                     iint<(numints-1)?(CHAR_BIT*sizeof (int)):
                     bits-(CHAR_BIT*sizeof (int))*iint ))
      }

    } else {

      cbf_failnez (cbf_put_integer (file, element[0], 0, bits))

    }
  }