Exemple #1
0
opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
	opj_common_ptr cinfo;
	opj_image_t *image = NULL;

	if(!jp2 || !cio) {
		return NULL;
	}

	cinfo = jp2->cinfo;

	/* JP2 decoding */
	if(!jp2_read_struct(jp2, cio)) {
		opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
		return NULL;
	}

	/* J2K decoding */
	image = j2k_decode(jp2->j2k, cio, cstr_info);
	if(!image) {
		opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
		return NULL;
	}

	/* Set Image Color Space */
	if (jp2->enumcs == 16)
		image->color_space = CLRSPC_SRGB;
	else if (jp2->enumcs == 17)
		image->color_space = CLRSPC_GRAY;
	else if (jp2->enumcs == 18)
		image->color_space = CLRSPC_SYCC;
	else
		image->color_space = CLRSPC_UNKNOWN;

	return image;
}
Exemple #2
0
opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
	if(dinfo && cio) {
		switch(dinfo->codec_format) {
			case CODEC_J2K:
				return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
			case CODEC_JPT:
				return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
			case CODEC_JP2:
				return jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
			case CODEC_UNKNOWN:
			default:
				break;
		}
	}
	return NULL;
}
Exemple #3
0
int jp2_read_jp2c(unsigned char *src, int len, jp2_struct_t * jp2_struct,
		  j2k_cp_t * cp)
{
  jp2_box_t box;

  jp2_read_boxhdr(&box);
  if (JP2_JP2C != box.type) {
    fprintf(stderr, "Error: Expected JP2C Marker\n");
    return 1;
  }

  src += cio_tell();

  if (j2k_decode(src, len, jp2_struct->image, cp) == 0) {
    fprintf(stderr, "JP2F box: failed to decode J2K bitstream image!\n");
    return 1;
  }

  return 0;
}
Exemple #4
0
opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
	opj_common_ptr cinfo;
	opj_image_t *image = NULL;

	if(!jp2 || !cio) {
		return NULL;
	}

	cinfo = jp2->cinfo;

	/* JP2 decoding */
	if(!jp2_read_struct(jp2, cio)) {
		opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
		return NULL;
	}

	/* J2K decoding */
	image = j2k_decode(jp2->j2k, cio);
	if(!image) {
		opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
	}

	return image;
}
Exemple #5
0
opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, 
	opj_codestream_info_t *cstr_info) 
{
	opj_common_ptr cinfo;
	opj_image_t *image = NULL;
	opj_jp2_color_t color;

	if(!jp2 || !cio) 
   {
	return NULL;
   }
	memset(&color, 0, sizeof(opj_jp2_color_t));
	cinfo = jp2->cinfo;

/* JP2 decoding */
	if(!jp2_read_struct(jp2, cio, &color)) 
   {
	free_color_data(&color);
	opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
	return NULL;
   }

/* J2K decoding */
	image = j2k_decode(jp2->j2k, cio, cstr_info);

	if(!image) 
   {
	free_color_data(&color);
	opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
	return NULL;
   }
   
    if (!jp2->ignore_pclr_cmap_cdef){

    /* Set Image Color Space */
	if (jp2->enumcs == 16)
		image->color_space = CLRSPC_SRGB;
	else if (jp2->enumcs == 17)
		image->color_space = CLRSPC_GRAY;
	else if (jp2->enumcs == 18)
		image->color_space = CLRSPC_SYCC;
	else
		image->color_space = CLRSPC_UNKNOWN;

	if(color.jp2_cdef)
   {
	jp2_apply_cdef(image, &color);
   }
	if(color.jp2_pclr)
   {
/* Part 1, I.5.3.4: Either both or none : */
	if( !color.jp2_pclr->cmap) 
	 jp2_free_pclr(&color);
	else
	 jp2_apply_pclr(&color, image, cinfo);
   }
	if(color.icc_profile_buf)
   {
	image->icc_profile_buf = color.icc_profile_buf;
	color.icc_profile_buf = NULL;
	image->icc_profile_len = color.icc_profile_len;
   }
   }
   
	return image;

}/* opj_jp2_decode() */
Exemple #6
0
bool CxImageJ2K::Decode(CxFile *hFile)
{
	if (hFile == NULL) return false;

  try
  {
	BYTE* src;
	long len;
	j2k_image_t *img=NULL;
	j2k_cp_t *cp=NULL;
	long i,x,y,w,h,max;

	len=hFile->Size();
	src=(BYTE*)malloc(len);
	hFile->Read(src, len, 1);

	if (!j2k_decode(src, len, &img, &cp)) {
		free(src);
		throw "failed to decode J2K image!";
	}

	free(src);

    if (img->numcomps==3 &&
		img->comps[0].dx==img->comps[1].dx &&
		img->comps[1].dx==img->comps[2].dx &&
		img->comps[0].dy==img->comps[1].dy &&
		img->comps[1].dy==img->comps[2].dy &&
		img->comps[0].prec==img->comps[1].prec &&
		img->comps[1].prec==img->comps[2].prec)
	{
        w=CEILDIV(img->x1-img->x0, img->comps[0].dx);
        h=CEILDIV(img->y1-img->y0, img->comps[0].dy);
        max=(1<<img->comps[0].prec)-1;

		Create(w,h,24,CXIMAGE_FORMAT_J2K);

		RGBQUAD c;
        for (i=0,y=0; y<h; y++) {
			for (x=0; x<w; x++,i++){
				c.rgbRed   = img->comps[0].data[i];
				c.rgbGreen = img->comps[1].data[i];
				c.rgbBlue  = img->comps[2].data[i];
				SetPixelColor(x,h-1-y,c);
			}
		}
	} else {
		int compno;
		info.nNumFrames = img->numcomps;
		if ((info.nFrame<0)||(info.nFrame>=info.nNumFrames)){
			j2k_destroy(&img,&cp);
			throw "wrong frame!";
		}
		for (compno=0; compno<=info.nFrame; compno++) {
			w=CEILDIV(img->x1-img->x0, img->comps[compno].dx);
			h=CEILDIV(img->y1-img->y0, img->comps[compno].dy);
			max=(1<<img->comps[compno].prec)-1;
			Create(w,h,8,CXIMAGE_FORMAT_J2K);
			SetGrayPalette();
			for (i=0,y=0; y<h; y++) {
				for (x=0; x<w; x++,i++){
					SetPixelIndex(x,h-1-y,img->comps[compno].data[i]);
				}
			}
		}
	}

	j2k_destroy(&img,&cp);

  } catch (char *message) {
	strncpy(info.szLastError,message,255);
	return FALSE;
  }
	return true;
}
Exemple #7
0
int main(int argc, char **argv)
{
  FILE *f;
  char *src, *src_name;
  char *dest, S1, S2, S3;
  int len;

  j2k_image_t img;

  j2k_cp_t cp;
  int w, wr, wrr, h, hr, hrr, max;
  int i, image_type = -1, compno, pad, j;
  int adjust;
  jp2_struct_t *jp2_struct;

  if (argc < 3) {
    fprintf(stderr,
	    "usage: %s j2k-file image-file [-reduce n]\n", argv[0]);
    return 1;
  }

  f = fopen(argv[1], "rb");
  if (!f) {
    fprintf(stderr, "failed to open %s for reading\n", argv[1]);
    return 1;
  }

  dest = argv[2];

  cp.reduce_on = 0;
  cp.reduce_value = 0;

  /* OPTION REDUCE IS ACTIVE */
  if (argc == 5) {
    if (strcmp(argv[3], "-reduce")) {
      fprintf(stderr,
	      "usage: options " "-reduce n"
	      " where n is the factor of reduction [%s]\n", argv[3]);
      return 1;
    }
    cp.reduce_on = 1;
    sscanf(argv[4], "%d", &cp.reduce_value);
  }

  while (*dest) {
    dest++;
  }
  dest--;
  S3 = *dest;
  dest--;
  S2 = *dest;
  dest--;
  S1 = *dest;

  if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
      || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
    image_type = 0;

    dest--;

    *dest = '\0';
  }

  if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
      || (S1 == 'P' && S2 == 'N' && S3 == 'M') || (S1 == 'p' && S2 == 'g'
						   && S3 == 'm')
      || (S1 == 'P' && S2 == 'G' && S3 == 'M') || (S1 == 'P' && S2 == 'P'
						   && S3 == 'M')
      || (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
    image_type = 1;
  }

  if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
      || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
    image_type = 2;
  }

  if (image_type == -1) {
    fprintf(stderr,
	    "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",
	    S1, S2, S3);
    return 1;
  }

  fseek(f, 0, SEEK_END);
  len = ftell(f);
  fseek(f, 0, SEEK_SET);
  src = (char *) malloc(len);
  fread(src, 1, len, f);
  fclose(f);

  src_name = argv[1];
  while (*src_name) {
    src_name++;
  }
  src_name--;
  S3 = *src_name;
  src_name--;
  S2 = *src_name;
  src_name--;
  S1 = *src_name;

  /* J2K format */
  if ((S1 == 'j' && S2 == '2' && S3 == 'k')
      || (S1 == 'J' && S2 == '2' && S3 == 'K') || (S1 == 'j' && S2 == '2'
						   && S3 == 'c')
      || (S1 == 'J' && S2 == '2' && S3 == 'C')) {
    if (!j2k_decode(src, len, &img, &cp)) {
      fprintf(stderr, "j2k_to_image: failed to decode image!\n");
      return 1;
    }
  }

  /* JP2 format */
  else if ((S1 == 'j' && S2 == 'p' && S3 == '2')
	   || (S1 == 'J' && S2 == 'P' && S3 == '2')) {
    jp2_struct = (jp2_struct_t *) malloc(sizeof(jp2_struct_t));

    jp2_struct->image = &img;

    if (jp2_decode(src, len, jp2_struct, &cp)) {
      fprintf(stderr, "j2k_to_image: failed to decode image!\n");
      return 1;
    }
    /* Insert code here if you want to create actions on jp2_struct before deleting it */
    free(jp2_struct);
  }

  /* JPT format */
  else if ((S1 == 'j' && S2 == 'p' && S3 == 't')
	   || (S1 == 'J' && S2 == 'P' && S3 == 'T')) {
    if (!j2k_decode_jpt_stream(src, len, &img, &cp)) {
      fprintf(stderr, "j2k_to_image: failed to decode image!\n");
      return 1;
    }
  }

  /* otherwise : error */
  else {
    fprintf(stderr,
	    "j2k_to_image : Unknown format image *.%c%c%c [only *.j2k, *.jp2, *.jpc or *.jpt]!! \n",
	    S1, S2, S3);
    return 1;
  }

  free(src);
  /* ------------------  CREATE OUT IMAGE WITH THE RIGHT FORMAT ----------------------- */

  /* ---------------------------- / */
  /* /                            / */
  /* /  FORMAT : PNM, PGM or PPM  / */
  /* /                            / */
  /* ---------------------------- / */

  switch (image_type) {
  case 1:			/* PNM PGM PPM */
    if (img.numcomps == 3 && img.comps[0].dx == img.comps[1].dx
	&& img.comps[1].dx == img.comps[2].dx
	&& img.comps[0].dy == img.comps[1].dy
	&& img.comps[1].dy == img.comps[2].dy
	&& img.comps[0].prec == img.comps[1].prec
	&& img.comps[1].prec == img.comps[2].prec) {
      f = fopen(argv[2], "wb");
      w = ceildiv(img.x1 - img.x0, img.comps[0].dx);
      // wr = ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor),img.comps[0].dx);
      wr = img.comps[0].w;
      wrr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);

      h = ceildiv(img.y1 - img.y0, img.comps[0].dy);
      // hr = ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
      hr = img.comps[0].h;
      hrr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);

      max = img.comps[0].prec > 8 ? 255 : (1 << img.comps[0].prec) - 1;

      img.comps[0].x0 =
	int_ceildivpow2(img.comps[0].x0 -
			int_ceildiv(img.x0, img.comps[0].dx),
			img.comps[0].factor);
      img.comps[0].y0 =
	int_ceildivpow2(img.comps[0].y0 -
			int_ceildiv(img.y0, img.comps[0].dy),
			img.comps[0].factor);


      fprintf(f, "P6\n# %d %d %d %d %d\n%d %d\n%d\n",
	      cp.tcps[cp.tileno[0]].tccps[0].numresolutions, w, h,
	      img.comps[0].x0, img.comps[0].y0, wrr, hrr, max);
      adjust = img.comps[0].prec > 8 ? img.comps[0].prec - 8 : 0;
      for (i = 0; i < wrr * hrr; i++) {
	char r, g, b;
	r = img.comps[0].data[i / wrr * wr + i % wrr];
	r += (img.comps[0].sgnd ? 1 << (img.comps[0].prec - 1) : 0);
	r = r >> adjust;

	g = img.comps[1].data[i / wrr * wr + i % wrr];
	g += (img.comps[1].sgnd ? 1 << (img.comps[1].prec - 1) : 0);
	g = g >> adjust;

	b = img.comps[2].data[i / wrr * wr + i % wrr];
	b += (img.comps[2].sgnd ? 1 << (img.comps[2].prec - 1) : 0);
	b = b >> adjust;

	fprintf(f, "%c%c%c", r, g, b);
      }
      free(img.comps[0].data);
      free(img.comps[1].data);
      free(img.comps[2].data);
      fclose(f);
    } else {
      for (compno = 0; compno < img.numcomps; compno++) {