Esempio n. 1
0
static void jpg_put_pixel_rows(j_decompress_ptr cinfo, jpg_dest_t *dinfo,
  JDIMENSION rows_supplied)
{
    JSAMPLE *bufptr;
    int cmptno;
    JDIMENSION x;
    uint_fast32_t width;

    if (dinfo->error) {
        return;
    }

    assert(cinfo->output_components == jas_image_numcmpts(dinfo->image));

    for (cmptno = 0; cmptno < cinfo->output_components; ++cmptno) {
        width = jas_image_cmptwidth(dinfo->image, cmptno);
        bufptr = (dinfo->buffer[0]) + cmptno;
        for (x = 0; x < width; ++x) {
            jas_matrix_set(dinfo->data, 0, x, GETJSAMPLE(*bufptr));
            bufptr += cinfo->output_components;
        }
        if (jas_image_writecmpt(dinfo->image, cmptno, 0, dinfo->row, width, 1,
          dinfo->data)) {
            dinfo->error = 1;
        }
    }
    dinfo->row += rows_supplied;
}
Esempio n. 2
0
jas_image_t *makediffimage(jas_matrix_t *origdata, jas_matrix_t *recondata)
{
	jas_image_t *diffimage;
	jas_matrix_t *diffdata[3];
	int width;
	int height;
	int i;
	int j;
	int k;
	jas_image_cmptparm_t compparms[3];
	jas_seqent_t a;
	jas_seqent_t b;

	width = jas_matrix_numcols(origdata);
	height = jas_matrix_numrows(origdata);

	for (i = 0; i < 3; ++i) {
		compparms[i].tlx = 0;
		compparms[i].tly = 0;
		compparms[i].hstep = 1;
		compparms[i].vstep = 1;
		compparms[i].width = width;
		compparms[i].height = height;
		compparms[i].prec = 8;
		compparms[i].sgnd = jas_false;
	}
	if (!(diffimage = jas_image_create(3, compparms, JAS_CLRSPC_SRGB))) {
		abort();
	}

	for (i = 0; i < 3; ++i) {
		if (!(diffdata[i] = jas_matrix_create(height, width))) {
			jas_eprintf("internal error\n");
			return 0;
		}
	}

	for (j = 0; j < height; ++j) {
		for (k = 0; k < width; ++k) {
			a = jas_matrix_get(origdata, j, k);
			b = jas_matrix_get(recondata, j, k);
			if (a > b) {
				jas_matrix_set(diffdata[0], j, k, 255);
				jas_matrix_set(diffdata[1], j, k, 0);
				jas_matrix_set(diffdata[2], j, k, 0);
			} else if (a < b) {
				jas_matrix_set(diffdata[0], j, k, 0);
				jas_matrix_set(diffdata[1], j, k, 255);
				jas_matrix_set(diffdata[2], j, k, 0);
			} else {
				jas_matrix_set(diffdata[0], j, k, a);
				jas_matrix_set(diffdata[1], j, k, a);
				jas_matrix_set(diffdata[2], j, k, a);
			}
		}
	}

	for (i = 0; i < 3; ++i) {
		if (jas_image_writecmpt(diffimage, i, 0, 0, width, height, diffdata[i])) {
			return 0;
		}
	}

	return diffimage;
}
Esempio n. 3
0
static void WriteJP2KImage( const GenericImage<P>& img,
                            const ImageInfo& info, const ImageOptions& options,
                            jas_stream_t* jp2Stream, jas_image_t* jp2Image, int jp2Format,
                            const JPEG2000ImageOptions& jp2Options )
{
   jas_matrix_t* pixels = nullptr;

   try
   {
      pixels = jas_matrix_create( 1, img.Width() );
      if ( pixels == nullptr )
         throw Error( "Memory allocation error writing JPEG2000 image." );

      for ( int c = 0; c < img.NumberOfChannels(); ++c )
      {
         for ( int y = 0; y < img.Height(); ++y )
         {
            const typename P::sample* f = img.ScanLine( y, c );

            if ( options.bitsPerSample == 8 )
            {
               if ( jp2Options.signedSample )
               {
                  int8 v;
                  for ( int x = 0; x < img.Width(); ++x )
                  {
                     P::FromSample( v, *f++ );
                     jas_matrix_set( pixels, 0, x, v );
                  }
               }
               else
               {
                  uint8 v;
                  for ( int x = 0; x < img.Width(); ++x )
                  {
                     P::FromSample( v, *f++ );
                     jas_matrix_set( pixels, 0, x, v );
                  }
               }
            }
            else
            {
               if ( jp2Options.signedSample )
               {
                  int16 v;
                  for ( int x = 0; x < img.Width(); ++x )
                  {
                     P::FromSample( v, *f++ );
                     jas_matrix_set( pixels, 0, x, v );
                  }
               }
               else
               {
                  uint16 v;
                  for ( int x = 0; x < img.Width(); ++x )
                  {
                     P::FromSample( v, *f++ );
                     jas_matrix_set( pixels, 0, x, v );
                  }
               }
            }

            jas_image_writecmpt( jp2Image, c, 0, y, img.Width(), 1, pixels );
         }
      }

      IsoString jp2OptionsStr;

      jp2OptionsStr.AppendFormat( "mode=%s", jp2Options.lossyCompression ? "real" : "int" );

      if ( jp2Options.lossyCompression )
         jp2OptionsStr.AppendFormat( " rate=%g", jp2Options.compressionRate );

      if ( jp2Options.tiledImage )
      {
         jp2OptionsStr.AppendFormat( " tilewidth=%d", Range( jp2Options.tileWidth, 8, img.Width() ) );
         jp2OptionsStr.AppendFormat( " tileheight=%d", Range( jp2Options.tileHeight, 8, img.Height() ) );
      }

      if ( jp2Options.numberOfLayers > 1 )
      {
         jp2OptionsStr.Append( " ilyrrates=" );

         float dr = (jp2Options.lossyCompression ? jp2Options.compressionRate : 1.0F)/jp2Options.numberOfLayers;

         for ( int l = 1; ; )
         {
            jp2OptionsStr.AppendFormat( "%g", l*dr );

            if ( ++l == jp2Options.numberOfLayers )
               break;

            jp2OptionsStr.Append( ',' );
         }

         jp2OptionsStr.Append( " prg=" );

         switch ( jp2Options.progressionOrder )
         {
         default:
         case JPEG2000ProgressionOrder::LRCP: jp2OptionsStr.Append( "lrcp" ); break;
         case JPEG2000ProgressionOrder::RLCP: jp2OptionsStr.Append( "rlcp" ); break;
         case JPEG2000ProgressionOrder::RPCL: jp2OptionsStr.Append( "rpcl" ); break;
         case JPEG2000ProgressionOrder::PCRL: jp2OptionsStr.Append( "pcrl" ); break;
         case JPEG2000ProgressionOrder::CPRL: jp2OptionsStr.Append( "cprl" ); break;
         }
      }

      if ( jas_image_encode( jp2Image, jp2Stream, jp2Format, jp2OptionsStr.Begin() ) < 0 )
         throw Error( "Unable to encode JPEG2000 image." );

      jas_matrix_destroy( pixels ), pixels = nullptr;
   }
   catch ( ... )
   {
      if ( pixels != nullptr )
         jas_matrix_destroy( pixels );
      throw;
   }
}
Esempio n. 4
0
static int pnm_getdata(jas_stream_t *in, pnm_hdr_t *hdr, jas_image_t *image)
{
    int ret;
#if 0
    int numcmpts;
#endif
    int cmptno;
    int fmt;
    jas_matrix_t *data[3];
    int x;
    int y;
    int_fast64_t v;
    int depth;
    int type;
    int c;
    int n;

    ret = -1;

#if 0
    numcmpts = jas_image_numcmpts(image);
#endif
    fmt = pnm_fmt(hdr->magic);
    type = pnm_type(hdr->magic);
    depth = pnm_maxvaltodepth(hdr->maxval);

    data[0] = 0;
    data[1] = 0;
    data[2] = 0;
    for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) {
        if (!(data[cmptno] = jas_matrix_create(1, hdr->width))) {
            goto done;
        }
    }

    for (y = 0; y < hdr->height; ++y) {
        if (type == PNM_TYPE_PBM) {
            if (fmt == PNM_FMT_BIN) {
                for (x = 0; x < hdr->width;) {
                    if ((c = jas_stream_getc(in)) == EOF) {
                        goto done;
                    }
                    n = 8;
                    while (n > 0 && x < hdr->width) {
                        jas_matrix_set(data[0], 0, x, 1 - ((c >> 7) & 1));
                        c <<= 1;
                        --n;
                        ++x;
                    }
                }
            } else {
                for (x = 0; x < hdr->width; ++x) {
                    int uv;
                    if (pnm_getbitstr(in, &uv)) {
                        goto done;
                    }
                    jas_matrix_set(data[0], 0, x, 1 - uv);
                }
            }
        } else {
            for (x = 0; x < hdr->width; ++x) {
Esempio n. 5
0
static void
createJasperRaster(struct pam *  const inpamP, 
                   jas_image_t * const jasperP) {

    jas_matrix_t ** matrix;  /* malloc'ed */
        /* matrix[X] is the data for Plane X of the current row */
    unsigned int plane;
    unsigned int row;
    tuple * tuplerow;
    bool oddMaxval;
    sample jasperMaxval;

    matrix = malloc(inpamP->depth * sizeof(jas_matrix_t *));
    if (matrix == NULL)
        pm_error("Out of memory");

    for (plane = 0; plane < inpamP->depth; ++plane) {
        matrix[plane] = jas_matrix_create(1, inpamP->width);

        if (matrix[plane] == NULL)
            pm_error("Unable to create matrix for plane %u.  "
                     "jas_matrix_create() failed.", plane);
    }   
    tuplerow = pnm_allocpamrow(inpamP);

    jasperMaxval = pm_bitstomaxval(pm_maxvaltobits(inpamP->maxval));
    oddMaxval = jasperMaxval != inpamP->maxval;

    for (row = 0; row < inpamP->height; ++row) {
        unsigned int col;

        pnm_readpamrow(inpamP, tuplerow);

        for (col = 0; col < inpamP->width; ++col) {
            unsigned int plane;
            for (plane = 0; plane < inpamP->depth; ++plane) {
                unsigned int jasperSample;

                if (oddMaxval)
                    jasperSample = tuplerow[col][plane] * 
                        jasperMaxval / inpamP->maxval;
                else
                    jasperSample = tuplerow[col][plane];

                jas_matrix_set(matrix[plane], 0, col, jasperSample);
            }
        }
        { 
            unsigned int plane;

            for (plane = 0; plane < inpamP->depth; ++plane) {
                int rc;
                rc = jas_image_writecmpt(jasperP, plane, 0, row, 
                                         inpamP->width, 1,
                                         matrix[plane]);
                if (rc != 0)
                    pm_error("jas_image_writecmpt() of plane %u failed.", 
                             plane);
            }
        }
    }

    pnm_freepamrow(tuplerow);
    for (plane = 0; plane < inpamP->depth; ++plane)
        jas_matrix_destroy(matrix[plane]);
    
    free(matrix);
}