Beispiel #1
0
/**
 * Updates the components of the image from the coding parameters.
 * 
 * @param p_image		the image to update.
 * @param p_cp			the coding parameters from which to update the image.
 */
void opj_image_comp_update(opj_image_t * p_image,const opj_cp_t * p_cp)
{
	OPJ_UINT32 i, l_width, l_height;
	OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
	OPJ_INT32 l_comp_x0,l_comp_y0,l_comp_x1,l_comp_y1;
	opj_image_comp_t * l_img_comp = 00;

	l_x0 = int_max(p_cp->tx0 , p_image->x0);
	l_y0 = int_max(p_cp->ty0 , p_image->y0);
	l_x1 = int_min(p_cp->tx0 + p_cp->tw * p_cp->tdx, p_image->x1);
	l_y1 = int_min(p_cp->ty0 + p_cp->th * p_cp->tdy, p_image->y1);
	
	l_img_comp = p_image->comps;
	for 
		(i = 0; i < p_image->numcomps; ++i) 
	{
		l_comp_x0 = int_ceildiv(l_x0, l_img_comp->dx);
		l_comp_y0 = int_ceildiv(l_y0, l_img_comp->dy);
		l_comp_x1 = int_ceildiv(l_x1, l_img_comp->dx);
		l_comp_y1 = int_ceildiv(l_y1, l_img_comp->dy);
		l_width = int_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor);
		l_height = int_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor);
		l_img_comp->w = l_width;
		l_img_comp->h = l_height;
		l_img_comp->x0 = l_x0;
		l_img_comp->y0 = l_y0;
		++l_img_comp;
	}
}
Beispiel #2
0
/// <summary>
/// Get next packet in resolution-precinct-component-layer order.  
/// </summary>
int pi_next_rpcl(pi_iterator_t *pi) {
    pi_comp_t *comp;
    pi_resolution_t *res;
    if (!pi->first) {
        goto skip;
    } else {
        int compno, resno;
        pi->first=0;
        pi->dx=0;
        pi->dy=0;
        for (compno=0; compno<pi->numcomps; compno++) {
            comp=&pi->comps[compno];
            for (resno=0; resno<comp->numresolutions; resno++) {
                int dx, dy;
                res=&comp->resolutions[resno];
                dx=comp->dx*(1<<(res->pdx+comp->numresolutions-1-resno));
                dy=comp->dy*(1<<(res->pdy+comp->numresolutions-1-resno));
                pi->dx=!pi->dx?dx:int_min(pi->dx, dx);
                pi->dy=!pi->dy?dy:int_min(pi->dy, dy);
            }
        }
    }
    for (pi->resno=pi->poc.resno0; pi->resno<pi->poc.resno1; pi->resno++) {
        for (pi->y=pi->ty0; pi->y<pi->ty1; pi->y+=pi->dy-(pi->y%pi->dy)) {
            for (pi->x=pi->tx0; pi->x<pi->tx1; pi->x+=pi->dx-(pi->x%pi->dx)) {
                for (pi->compno=pi->poc.compno0; pi->compno<pi->poc.compno1; pi->compno++) {
                    int levelno;
                    int trx0, try0;
                    int rpx, rpy;
                    int prci, prcj;
                    comp=&pi->comps[pi->compno];
                    if (pi->resno>=comp->numresolutions) {
                        continue;
                    }
                    res=&comp->resolutions[pi->resno];
                    levelno=comp->numresolutions-1-pi->resno;
                    trx0=int_ceildiv(pi->tx0, comp->dx<<levelno);
                    try0=int_ceildiv(pi->ty0, comp->dy<<levelno);
                    rpx=res->pdx+levelno;
                    rpy=res->pdy+levelno;
                    if (!(pi->x%(comp->dx<<rpx)==0||(pi->x==pi->tx0&&(trx0<<levelno)%(1<<rpx)))) {
                        continue;
                    }
                    if (!(pi->y%(comp->dy<<rpy)==0||(pi->y==pi->ty0&&(try0<<levelno)%(1<<rpx)))) {
                        continue;
                    }
                    prci=int_floordivpow2(int_ceildiv(pi->x, comp->dx<<levelno), res->pdx)-int_floordivpow2(trx0, res->pdx);
                    prcj=int_floordivpow2(int_ceildiv(pi->y, comp->dy<<levelno), res->pdy)-int_floordivpow2(try0, res->pdy);
                    pi->precno=prci+prcj*res->pw;
                    for (pi->layno=0; pi->layno<pi->poc.layno1; pi->layno++) {
                        return 1;
skip:					;
                    }
                }
            }
        }
    }
    return 0;
}
Beispiel #3
0
/*
void j2k_write_siz() {
    int i;
    int lenp, len;
    log_print(LOG_TRACE, "%.8x: SIZ\n", cio_tell());
    cio_write(J2K_MS_SIZ, 2);
    lenp=cio_tell();
    cio_skip(2);
    cio_write(0, 2);
    cio_write(j2k_img->x1, 4);
    cio_write(j2k_img->y1, 4);
    cio_write(j2k_img->x0, 4);
    cio_write(j2k_img->y0, 4);
    cio_write(j2k_cp->tdx, 4);
    cio_write(j2k_cp->tdy, 4);
    cio_write(j2k_cp->tx0, 4);
    cio_write(j2k_cp->ty0, 4);
    cio_write(j2k_img->numcomps, 2);
    for (i=0; i<j2k_img->numcomps; i++) {
        cio_write(j2k_img->comps[i].prec-1+(j2k_img->comps[i].sgnd<<7), 1);
        cio_write(j2k_img->comps[i].dx, 1);
        cio_write(j2k_img->comps[i].dy, 1);
    }
    len=cio_tell()-lenp;
    cio_seek(lenp);
    cio_write(len, 2);
    cio_seek(lenp+len);
}
*/
void j2k_read_siz() {
    int len, i;
//    log_print(LOG_TRACE, "%.8x: SIZ\n", cio_tell()-2);
    len=cio_read(2);
    cio_read(2);
    j2k_img->x1=cio_read(4);
    j2k_img->y1=cio_read(4);
    j2k_img->x0=cio_read(4);
    j2k_img->y0=cio_read(4);
    j2k_cp->tdx=cio_read(4);
    j2k_cp->tdy=cio_read(4);
    j2k_cp->tx0=cio_read(4);
    j2k_cp->ty0=cio_read(4);
    j2k_img->numcomps=cio_read(2);
    j2k_img->comps=(j2k_comp_t*)malloc(j2k_img->numcomps*sizeof(j2k_comp_t));
    for (i=0; i<j2k_img->numcomps; i++) {
        int tmp, w, h;
        tmp=cio_read(1);
        j2k_img->comps[i].prec=(tmp&0x7f)+1;
        j2k_img->comps[i].sgnd=tmp>>7;
        j2k_img->comps[i].dx=cio_read(1);
        j2k_img->comps[i].dy=cio_read(1);
        w=int_ceildiv(j2k_img->x1-j2k_img->x0, j2k_img->comps[i].dx);
        h=int_ceildiv(j2k_img->y1-j2k_img->y0, j2k_img->comps[i].dy);
        j2k_img->comps[i].data=(int*)malloc(sizeof(int)*w*h);
    }
    j2k_cp->tw=int_ceildiv(j2k_img->x1-j2k_img->x0, j2k_cp->tdx);
    j2k_cp->th=int_ceildiv(j2k_img->y1-j2k_img->y0, j2k_cp->tdy);
    j2k_cp->tcps=(j2k_tcp_t*)calloc(sizeof(j2k_tcp_t), j2k_cp->tw*j2k_cp->th);
    
    j2k_default_tcp.tccps=(j2k_tccp_t*)calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);
 
    for (i=0; i<j2k_cp->tw*j2k_cp->th; i++) {
        j2k_cp->tcps[i].tccps=(j2k_tccp_t*)calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);
    }
    j2k_tile_data=(unsigned char**)calloc(j2k_cp->tw*j2k_cp->th, sizeof(char*));
    j2k_tile_len=(int*)calloc(j2k_cp->tw*j2k_cp->th, sizeof(int));
    j2k_state=J2K_STATE_MH;
}
Beispiel #4
0
int main(int argc, char **argv)
{
  int len;
  int NumResolution, numD_min;	/*   NumResolution : number of resolution                     */
  int Tile_arg;			/*   Tile_arg = 0 (not in argument) ou = 1 (in argument)      */
  int CSty;			/*   CSty : coding style                                      */
  int Prog_order;		/*   progression order (default LRCP)                         */
  char progression[4];
  int numpocs, numpocs_tile;	/*   Number of progression order change (POC) default 0       */
  int prcw_init[J2K_MAXRLVLS];	/*   Initialisation Precinct width                            */
  int prch_init[J2K_MAXRLVLS];	/*   Initialisation Precinct height                           */
  //int prcw_init, prch_init;                     /*   Initialisation precincts' size                           */
  int cblockw_init, cblockh_init;	/*   Initialisation codeblocks' size                          */
  int mode, value;		/*   Mode switch (cblk_style)                                 */
  int subsampling_dx, subsampling_dy;	/* subsampling value for dx and dy                    */
  int ROI_compno, ROI_shift;	/*   region of interrest                                      */
  int Dim[2];			/*   portion of the image coded                               */
  int TX0, TY0;			/*   tile off-set                                             */
  j2k_image_t img;
  j2k_cp_t cp, cp_init;		/*   cp_init is used to initialise in multiple tiles          */
  j2k_tcp_t *tcp, *tcp_init;	/*   tcp_init is used to initialise in multiple tile          */
  j2k_poc_t POC[32];		/*   POC : used in case of Progression order change           */
  j2k_poc_t *tcp_poc;
  j2k_tccp_t *tccp;
  
  int i, tileno, j;
  char *infile = 0;
  char *outfile = 0;
  char *index = 0;
  char *s, S1, S2, S3;
  int ir = 0;
  int res_spec = 0;		/*   For various precinct sizes specification                 */
  char sep;
  char *outbuf, *out;
  FILE *f;


  /* default value */
  /* ------------- */
  NumResolution = 6;
  CSty = 0;
  cblockw_init = 64;
  cblockh_init = 64;
  cp.tw = 1;
  cp.th = 1;
  cp.index_on = 0;
  Prog_order = 0;
  numpocs = 0;
  mode = 0;
  subsampling_dx = 1;
  subsampling_dy = 1;
  ROI_compno = -1;		/* no ROI */
  ROI_shift = 0;
  Dim[0] = 0;
  Dim[1] = 0;
  TX0 = 0;
  TY0 = 0;
  cp.comment = "Created by OpenJPEG version 0.9";
  cp.disto_alloc = 0;
  cp.fixed_alloc = 0;
  cp.fixed_quality = 0;		//add fixed_quality
  /* img.PPT=0; */

  Tile_arg = 0;
  cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t));	/* initialisation if only one tile */
  tcp_init = &cp_init.tcps[0];
  tcp_init->numlayers = 0;
  jpwl_cp_init(&jpwl_cp);

  cp.intermed_file=0;
  use_index=0;

  while (1) {
    int c = getopt(argc, argv,
		"i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I:W,F");
    if (c == -1)
      break;
    switch (c) {
    case 'i':			/* IN fill */
      infile = optarg;
      s = optarg;
      while (*s) {
	s++;
      }
      s--;
      S3 = *s;
      s--;
      S2 = *s;
      s--;
      S1 = *s;

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

      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')) {
	cp.image_type = 1;
	break;
      }

      if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
	  || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
	cp.image_type = 2;
	break;
      }
      fprintf(stderr,
	      "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",
	      S1, S2, S3);
      return 1;
      break;
      /* ----------------------------------------------------- */
    case 'o':			/* OUT fill */
      outfile = optarg;
      while (*outfile) {
	outfile++;
      }
      outfile--;
      S3 = *outfile;
      outfile--;
      S2 = *outfile;
      outfile--;
      S1 = *outfile;
      
      outfile = optarg;
      
      if ((S1 == 'j' && S2 == '2' && S3 == 'k') || (S1 == 'J' && S2 == '2' && S3 == 'K'))
	cp.JPEG2000_format=0;
      else if ((S1 == 'j' && S2 == 'p' && S3 == '2') || (S1 == 'J' && S2 == 'P' && S3 == '2'))
	cp.JPEG2000_format=1;
      else    {
	fprintf(stderr,"Unknown output format image *.%c%c%c [only *.j2k, *.jp2]!! \n",S1,S2,S3);
	return 1;
      }
      
      
      
      break;
      /* ----------------------------------------------------- */
    case 'r':			/* rates rates/distorsion */
      s = optarg;
      while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers])
	     == 1) {
	tcp_init->numlayers++;
	while (*s && *s != ',') {
	  s++;
	}
	if (!*s)
	  break;
	s++;
      }
      cp.disto_alloc = 1;
      cp.matrice = NULL;
      break;
      /* ----------------------------------------------------- */
    case 'q':			/* add fixed_quality */
      s = optarg;
      while (sscanf(s, "%f", &tcp_init->distoratio[tcp_init->numlayers]) ==
	     1) {
	tcp_init->numlayers++;
	while (*s && *s != ',') {
	  s++;
	}
	if (!*s)
	  break;
	s++;
      }
      cp.fixed_quality = 1;
      cp.matrice = NULL;
      break;
      /* dda */
      /* ----------------------------------------------------- */
    case 'f':			/* mod fixed_quality (before : -q) */
      s = optarg;
      sscanf(s, "%d", &tcp_init->numlayers);
      s++;
      if (tcp_init->numlayers > 9)
	s++;
      cp.matrice =
	(int *) malloc(tcp_init->numlayers * NumResolution * 3 *
		       sizeof(int));
      s = s + 2;
      for (i = 0; i < tcp_init->numlayers; i++) {
	tcp_init->rates[i] = 1;
	sscanf(s, "%d,", &cp.matrice[i * NumResolution * 3]);
	s += 2;
	if (cp.matrice[i * NumResolution * 3] > 9)
	  s++;
	cp.matrice[i * NumResolution * 3 + 1] = 0;
	cp.matrice[i * NumResolution * 3 + 2] = 0;
	for (j = 1; j < NumResolution; j++) {
	  sscanf(s, "%d,%d,%d",
		 &cp.matrice[i * NumResolution * 3 + j * 3 + 0],
		 &cp.matrice[i * NumResolution * 3 + j * 3 + 1],
		 &cp.matrice[i * NumResolution * 3 + j * 3 + 2]);
	  s += 6;
	  if (cp.matrice[i * NumResolution * 3 + j * 3] > 9)
	    s++;
	  if (cp.matrice[i * NumResolution * 3 + j * 3 + 1] > 9)
	    s++;
	  if (cp.matrice[i * NumResolution * 3 + j * 3 + 2] > 9)
	    s++;
	}
	if (i < tcp_init->numlayers - 1)
	  s++;
      }
      cp.fixed_alloc = 1;
      break;
      /* ----------------------------------------------------- */
    case 't':			/* tiles */
      sscanf(optarg, "%d,%d", &cp.tdx, &cp.tdy);
      Tile_arg = 1;
      break;
      /* ----------------------------------------------------- */
    case 'n':			/* resolution */
      sscanf(optarg, "%d", &NumResolution);
      break;
      /* ----------------------------------------------------- */
    case 'c':			/* precinct dimension */
      s = optarg;
      do {
	sep = 0;
	sscanf(s, "[%d,%d]%c", &prcw_init[res_spec],
	       &prch_init[res_spec], &sep);
	CSty |= 0x01;
	res_spec++;
	s = strpbrk(s, "]") + 2;
      } while (sep == ',');
      break;
      /* ----------------------------------------------------- */
    case 'b':			/* code-block dimension */
      sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
      if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
	  || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
	fprintf(stderr,
		"!! Size of code_block error (option -b) !!\n\nRestriction :\n    * width*height<=4096\n    * 4<=width,height<= 1024\n\n");
	return 1;
      }
      break;
      /* ----------------------------------------------------- */
    case 'x':			/* creation of index file */
      index = optarg;
      cp.index_on = 1;
	  use_index = 1;
      break;
      /* ----------------------------------------------------- */
    case 'p':			/* progression order */
      s = optarg;
      for (i = 0; i < 4; i++) {
	progression[i] = *s;
	s++;
      }
      Prog_order = give_progression(progression);

      if (Prog_order == -1) {
	fprintf(stderr,
		"Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
	return 1;
      }
      break;
      /* ----------------------------------------------------- */
    case 's':			/* subsampling factor */
      if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy)
	  != 2) {
	fprintf(stderr,
		"'-s' sub-sampling argument error !  [-s dx,dy]\n");
	return 1;
      }
      break;
      /* ----------------------------------------------------- */
    case 'd':			/* coordonnate of the reference grid */
      if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) {
	fprintf(stderr,
		"-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
	return 1;
      }
      break;
      /* ----------------------------------------------------- */
    case 'h':			/* Display an help description */
      help_display();
      return 0;
      break;
      /* ----------------------------------------------------- */
    case 'P':			/* POC */
      fprintf(stderr, "/----------------------------------\\\n");
      fprintf(stderr, "|  POC option not fully tested !!  |\n");
      fprintf(stderr, "\\----------------------------------/\n");

      s = optarg;
      while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
		    &POC[numpocs].resno0, &POC[numpocs].compno0,
		    &POC[numpocs].layno1, &POC[numpocs].resno1,
		    &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
	POC[numpocs].prg = give_progression(POC[numpocs].progorder);
	/* POC[numpocs].tile; */
	numpocs++;
	while (*s && *s != '/') {
	  s++;
	}
	if (!*s)
	  break;
	s++;
      }
      break;
      /* ------------------------------------------------------ */
    case 'S':			/* SOP marker */
      CSty |= 0x02;
      break;
      /* ------------------------------------------------------ */
    case 'E':			/* EPH marker */
      CSty |= 0x04;
      break;
      /* ------------------------------------------------------ */
    case 'M':			/* Mode switch pas tous au point !! */
      if (sscanf(optarg, "%d", &value) == 1) {
	for (i = 0; i <= 5; i++) {
	  int cache = value & (1 << i);
	  if (cache)
	    mode |= (1 << i);
	}
      }
      break;
      /* ------------------------------------------------------ */
    case 'R':			/* ROI */
      if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
	fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
	return 1;
      }
      break;
      /* ------------------------------------------------------ */
    case 'T':			/* Tile offset */
      if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
	fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
	return 1;
      }
      break;
      /* ------------------------------------------------------ */
    case 'C':			/* Add a comment */
      cp.comment = optarg;
      break;
      /* ------------------------------------------------------ */
    case 'I':			/* reversible or not */
      ir = 1;
      break;
	   /* ------------------------------------------------------ */
	case 'W':			/* version 0.2 enables only EPC on main header and epb in fixed way*/
	  jpwl_cp.JPWL_on = 1;
	  break;
	   /* ------------------------------------------------------ */
	case 'F':			/* use intermed files*/
	  cp.intermed_file=1;
	  break;
	   /* ------------------------------------------------------ */
    default:
      return 1;
    }
  }

  cp.tx0 = TX0;
  cp.ty0 = TY0;

  // inserici i parametri jpwl
  
  if(jpwl_cp.JPWL_on)
	get_jpwl_cp(& cp);


  


  /* Error messages */
  /* -------------- */
  if (!infile || !outfile) {
    fprintf(stderr,
	    "usage: image_to_j2k -i image-file -o j2k/jp2-file (+ options)\n");
    return 1;
  }

  if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)&&(!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) {
    fprintf(stderr,
	    "Error: options -r -q and -f can not be used together !!\n");
    return 1;
  } // mod fixed_quality

  /* if no rate entered, lossless by default */
  if (tcp_init->numlayers == 0) {
    tcp_init->rates[tcp_init->numlayers] = 1;
    tcp_init->numlayers++;
    cp.disto_alloc = 1;
  }

  if (TX0 > Dim[0] || TY0 > Dim[1]) {
    fprintf(stderr,
	    "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
	    TX0, Dim[0], TY0, Dim[1]);
    return 1;
  }

  for (i = 0; i < numpocs; i++) {
    if (POC[i].prg == -1) {
      fprintf(stderr,
	      "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
	      i + 1);
    }
  }

  switch (cp.image_type) {
  case 0:
    if (Tile_arg) {
      if (!pgxtoimage
	  (infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim,
	   cp)) {
	fprintf(stderr, "not a pgx file\n");
	return 1;
      }
    } else {
      if (!pgxtoimage
	  (infile, &img, -1, subsampling_dx, subsampling_dy, Dim, cp)) {
	fprintf(stderr, " not a pgx file\n");
	return 1;
      }
    }
    break;

  case 1:
    if (!pnmtoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
      fprintf(stderr, " not a pnm file\n");
      return 1;
    }
    break;

  case 2:
    if (!bmptoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
      fprintf(stderr, " not a bmp file\n");
      return 1;
    }
    break;
  }
  /* to respect profile - 0 */
  /* ---------------------- */
  numD_min = 0;
  /*   while (int_ceildiv(img.x1,(1<<numD_min))-int_ceildiv(img.x0,(1<<numD_min))>120 || int_ceildiv(img.y1,(1<<numD_min))-int_ceildiv(img.y0,(1<<numD_min))>160) numD_min++;
     if ((numD_min+1)>NumResolution)
     {
     fprintf(stderr,"\n********************************************************************************\n\n");
     fprintf(stderr,  "In view to respect Profile-0, the number of resolution used is %d in place of %d\n\n",numD_min+1,NumResolution);
     fprintf(stderr,  "********************************************************************************\n\n");
     NumResolution=numD_min+1;
     } */

  if (Tile_arg == 1) {
    cp.tw = int_ceildiv(img.x1 - cp.tx0, cp.tdx);
    cp.th = int_ceildiv(img.y1 - cp.ty0, cp.tdy);
  } else {
    cp.tdx = img.x1 - cp.tx0;
    cp.tdy = img.y1 - cp.ty0;
  }
 
  /* Initialization for PPM marker */
  cp.ppm = 0;
  cp.ppm_data = NULL;
  cp.ppm_previous = 0;
  cp.ppm_store = 0;

  /* Init the mutiple tiles */
  /* ---------------------- */
  cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));

  for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
    tcp = &cp.tcps[tileno];
    tcp->numlayers = tcp_init->numlayers;
    for (j = 0; j < tcp->numlayers; j++) {
      if (cp.fixed_quality)   // add fixed_quality
	tcp->distoratio[j] = tcp_init->distoratio[j];
      else
	tcp->rates[j] = tcp_init->rates[j];
    }
    tcp->csty = CSty;
    tcp->prg = Prog_order;
    tcp->mct = img.numcomps == 3 ? 1 : 0;
    tcp->ppt = 0;
    tcp->ppt_data = NULL;
    tcp->ppt_store = 0;

    numpocs_tile = 0;
    tcp->POC = 0;
    if (numpocs) {
      /* intialisation of POC */
      tcp->POC = 1;
      for (i = 0; i < numpocs; i++) {
	if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
	  tcp_poc = &tcp->pocs[numpocs_tile];
	  tcp_poc->resno0 = POC[numpocs_tile].resno0;
	  tcp_poc->compno0 = POC[numpocs_tile].compno0;
	  tcp_poc->layno1 = POC[numpocs_tile].layno1;
	  tcp_poc->resno1 = POC[numpocs_tile].resno1;
	  tcp_poc->compno1 = POC[numpocs_tile].compno1;
	  tcp_poc->prg = POC[numpocs_tile].prg;
	  tcp_poc->tile = POC[numpocs_tile].tile;
	  numpocs_tile++;
	}
      }
    }
    tcp->numpocs = numpocs_tile;
    tcp->tccps = (j2k_tccp_t *) malloc(img.numcomps * sizeof(j2k_tccp_t));

    for (i = 0; i < img.numcomps; i++) {
      tccp = &tcp->tccps[i];
      tccp->csty = CSty & 0x01;	/* 0 => one precinct || 1 => custom precinct  */
      tccp->numresolutions = NumResolution;
      tccp->cblkw = int_floorlog2(cblockw_init);
      tccp->cblkh = int_floorlog2(cblockh_init);
      tccp->cblksty = mode;
      tccp->qmfbid = ir ? 0 : 1;
      tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
      tccp->numgbits = 2;
      if (i == ROI_compno)
	tccp->roishift = ROI_shift;
      else
	tccp->roishift = 0;
      if (CSty & J2K_CCP_CSTY_PRT) {
	int p = 0;
	for (j = tccp->numresolutions - 1; j >= 0; j--) {
	  if (p < res_spec) {
	    if (prcw_init[p] < 1)
	      tccp->prcw[j] = 1;
	    else
	      tccp->prcw[j] = int_floorlog2(prcw_init[p]);

	    if (prch_init[p] < 1)
	      tccp->prch[j] = 1;
	    else
	      tccp->prch[j] = int_floorlog2(prch_init[p]);
	  } else {
	    int size_prcw, size_prch;
	    size_prcw = prcw_init[res_spec - 1] >> (p - (res_spec - 1));
	    size_prch = prch_init[res_spec - 1] >> (p - (res_spec - 1));
	    if (size_prcw < 1)
	      tccp->prcw[j] = 1;
	    else
	      tccp->prcw[j] = int_floorlog2(size_prcw);
	    if (size_prch < 1)
	      tccp->prch[j] = 1;
	    else
	      tccp->prch[j] = int_floorlog2(size_prch);
	  }
	  p++;
	  /*printf("\nsize precinct pour level %d : %d,%d\n", j,
		 tccp->prcw[j], tccp->prch[j]);*/
	}
      } else {
	for (j = 0; j < tccp->numresolutions; j++) {
	  tccp->prcw[j] = 15;
	  tccp->prch[j] = 15;
	}
      }
      calc_explicit_stepsizes(tccp, img.comps[i].prec);
    }
Beispiel #5
0
/// <summary>
/// Create a packet iterator.  
/// </summary>
pi_iterator_t *pi_create(j2k_image_t *img, j2k_cp_t *cp, int tileno) {
    int p, q;
    int compno, resno;
    int maxres=0;
    pi_iterator_t *pi;
    j2k_tcp_t *tcp;
    j2k_tccp_t *tccp;
    tcp=&cp->tcps[tileno];
    pi=(pi_iterator_t*)malloc(sizeof(pi_iterator_t));
    p=tileno%cp->tw;
    q=tileno/cp->tw;
    pi->tx0=int_max(cp->tx0+p*cp->tdx, img->x0);
    pi->ty0=int_max(cp->ty0+q*cp->tdy, img->y0);
    pi->tx1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1);
    pi->ty1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1);
    pi->numcomps=img->numcomps;
    pi->comps=(pi_comp_t*)malloc(img->numcomps*sizeof(pi_comp_t));
    for (compno=0; compno<pi->numcomps; compno++) {
        int tcx0, tcy0, tcx1, tcy1;
        pi_comp_t *comp=&pi->comps[compno];
        tccp=&tcp->tccps[compno];
        comp->dx=img->comps[compno].dx;
        comp->dy=img->comps[compno].dy;
        comp->numresolutions=tccp->numresolutions;
        comp->resolutions=(pi_resolution_t*)malloc(comp->numresolutions*sizeof(pi_resolution_t));
        tcx0=int_ceildiv(pi->tx0, comp->dx);
        tcy0=int_ceildiv(pi->ty0, comp->dy);
        tcx1=int_ceildiv(pi->tx1, comp->dx);
        tcy1=int_ceildiv(pi->ty1, comp->dy);
        if (comp->numresolutions>maxres) {
            maxres=comp->numresolutions;
        }
        for (resno=0; resno<comp->numresolutions; resno++) {
            int levelno;
            int rx0, ry0, rx1, ry1;
            int px0, py0, px1, py1;
            pi_resolution_t *res=&comp->resolutions[resno];
            if (tccp->csty&J2K_CCP_CSTY_PRT) {
                res->pdx=tccp->prcw[resno];
                res->pdy=tccp->prch[resno];
            } else {
                res->pdx=15;
                res->pdy=15;
            }
            levelno=comp->numresolutions-1-resno;
            rx0=int_ceildivpow2(tcx0, levelno);
            ry0=int_ceildivpow2(tcy0, levelno);
            rx1=int_ceildivpow2(tcx1, levelno);
            ry1=int_ceildivpow2(tcy1, levelno);
            px0=int_floordivpow2(rx0, res->pdx)<<res->pdx;
            py0=int_floordivpow2(ry0, res->pdy)<<res->pdy;
            px1=int_ceildivpow2(rx1, res->pdx)<<res->pdx;
            py1=int_ceildivpow2(ry1, res->pdy)<<res->pdy;
            res->pw=(px1-px0)>>res->pdx;
            res->ph=(py1-py0)>>res->pdy;
        }
    }
    pi->first=1;
    pi->poc.resno0=0;
    pi->poc.compno0=0;
    pi->poc.layno1=tcp->numlayers;
    pi->poc.resno1=maxres;
    pi->poc.compno1=img->numcomps;
    pi->poc.prg=tcp->prg;
    return pi;
}
Beispiel #6
0
int main(int argc, char **argv)
{
  int NumResolution, numD_min;	/*   NumResolution : number of resolution                     */
  int Tile_arg;			/*   Tile_arg = 0 (not in argument) ou = 1 (in argument)      */
  int CSty;			/*   CSty : coding style                                      */
  int Prog_order;		/*   progression order (default LRCP)                         */
  char progression[4];
  int numpocs, numpocs_tile;	/*   Number of progression order change (POC) default 0       */
  int prcw_init[J2K_MAXRLVLS];	/*   Initialisation Precinct width                            */
  int prch_init[J2K_MAXRLVLS];	/*   Initialisation Precinct height                           */
  //int prcw_init, prch_init;                     /*   Initialisation precincts' size                           */
  int cblockw_init, cblockh_init;	/*   Initialisation codeblocks' size                          */
  int mode, value;		/*   Mode switch (cblk_style)                                 */
  int subsampling_dx, subsampling_dy;	/* subsampling value for dx and dy                    */
  int ROI_compno, ROI_shift;	/*   region of interrest                                      */
  int Dim[2];			/*   portion of the image coded                               */
  int TX0, TY0;			/*   tile off-set                                             */
  mj2_movie_t movie;
  j2k_cp_t cp, cp_init;		/*   cp_init is used to initialise in multiple tiles          */
  j2k_tcp_t *tcp, *tcp_init;	/*   tcp_init is used to initialise in multiple tile          */
  j2k_poc_t POC[32];		/*   POC : used in case of Progression order change           */
  j2k_poc_t *tcp_poc;
  j2k_tccp_t *tccp;
  int i, tileno, l, j;
  char *infile = 0;
  char *outfile = 0;
  char *index = 0;
  char *s, S1, S2, S3;
  char *buf;
  int ir = 0;
  int res_spec = 0;		/*   For various precinct sizes specification                 */
  char sep;
  int w;			/*   Width of YUV file                                        */
  int h;			/*   Height of YUV file                                       */
  int CbCr_subsampling_dx;	/*   Sample rate of YUV 4:4:4 4:2:2 or 4:2:0                  */
  int CbCr_subsampling_dy;	/*   Sample rate of YUV 4:4:4 4:2:2 or 4:2:0                  */
  int frame_rate;		/*   Video Frame Rate                                         */
  int numcomps;			/*   In YUV files, numcomps always considered as 3            */
  int prec;			/*   In YUV files, precision always considered as 8           */
  int x1, y1,  len, jp2c_initpos, m, k, pos;
  long mdat_initpos, offset;
  FILE *mj2file, *yuvfile;
  unsigned int sampleno;
  j2k_image_t img;
  
  /* default value */
  /* ------------- */
  NumResolution = 6;
  CSty = 0;
  cblockw_init = 64;
  cblockh_init = 64;
  cp.tw = 1;
  cp.th = 1;
  cp.index_on = 0;
  Prog_order = 0;
  numpocs = 0;
  mode = 0;
  subsampling_dx = 1;
  subsampling_dy = 1;
  ROI_compno = -1;		/* no ROI */
  ROI_shift = 0;
  Dim[0] = 0;
  Dim[1] = 0;
  TX0 = 0;
  TY0 = 0;
  cp.comment = NULL;
  cp.disto_alloc = 0;
  cp.fixed_alloc = 0;
  cp.fixed_quality = 0;		//add fixed_quality
  w = 352;			// CIF default value
  h = 288;			// CIF default value
  CbCr_subsampling_dx = 2;	// CIF default value
  CbCr_subsampling_dy = 2;	// CIF default value
  frame_rate = 25;
  
  
  Tile_arg = 0;
  cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t));	/* initialisation if only one tile */
  tcp_init = &cp_init.tcps[0];
  tcp_init->numlayers = 0;
  
  cp.intermed_file = 0;		// Don't store each tile in a file during processing 
  
  while (1) {
    int c = getopt(argc, argv,
      "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I:W:F:");
    if (c == -1)
      break;
    switch (c) {
    case 'i':			/* IN fill */
      infile = optarg;
      s = optarg;
      while (*s) {
	s++;
      }
      s--;
      S3 = *s;
      s--;
      S2 = *s;
      s--;
      S1 = *s;
      
      if ((S1 == 'y' && S2 == 'u' && S3 == 'v')
	|| (S1 == 'Y' && S2 == 'U' && S3 == 'V')) {
	cp.decod_format = YUV_DFMT;
	break;
      }
      fprintf(stderr,
	"!! Unrecognized format for infile : %c%c%c [accept only *.yuv] !!\n\n",
	S1, S2, S3);
      return 1;
      break;
      /* ----------------------------------------------------- */
    case 'o':			/* OUT fill */
      outfile = optarg;
      while (*outfile) {
	outfile++;
      }
      outfile--;
      S3 = *outfile;
      outfile--;
      S2 = *outfile;
      outfile--;
      S1 = *outfile;
      
      outfile = optarg;
      
      if ((S1 == 'm' && S2 == 'j' && S3 == '2')
	|| (S1 == 'M' && S2 == 'J' && S3 == '2'))
	cp.cod_format = MJ2_CFMT;
      else {
	fprintf(stderr,
	  "Unknown output format image *.%c%c%c [only *.mj2]!! \n",
	  S1, S2, S3);
	return 1;
      }
      
      
      
      break;
      /* ----------------------------------------------------- */
    case 'r':			/* rates rates/distorsion */
      s = optarg;
      while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers])
	== 1) {
	tcp_init->numlayers++;
	while (*s && *s != ',') {
	  s++;
	}
	if (!*s)
	  break;
	s++;
      }
      cp.disto_alloc = 1;
      cp.matrice = NULL;
      break;
      /* ----------------------------------------------------- */
    case 'q':			/* add fixed_quality */
      s = optarg;
      while (sscanf
	(s, "%f", &tcp_init->distoratio[tcp_init->numlayers]) == 1) {
	tcp_init->numlayers++;
	while (*s && *s != ',') {
	  s++;
	}
	if (!*s)
	  break;
	s++;
      }
      cp.fixed_quality = 1;
      cp.matrice = NULL;
      break;
      /* dda */
      /* ----------------------------------------------------- */
    case 'f':			/* mod fixed_quality (before : -q) */
      s = optarg;
      sscanf(s, "%d", &tcp_init->numlayers);
      s++;
      if (tcp_init->numlayers > 9)
	s++;
      cp.matrice =
	(int *) malloc(tcp_init->numlayers * NumResolution * 3 *
	sizeof(int));
      s = s + 2;
      for (i = 0; i < tcp_init->numlayers; i++) {
	tcp_init->rates[i] = 1;
	sscanf(s, "%d,", &cp.matrice[i * NumResolution * 3]);
	s += 2;
	if (cp.matrice[i * NumResolution * 3] > 9)
	  s++;
	cp.matrice[i * NumResolution * 3 + 1] = 0;
	cp.matrice[i * NumResolution * 3 + 2] = 0;
	for (j = 1; j < NumResolution; j++) {
	  sscanf(s, "%d,%d,%d",
	    &cp.matrice[i * NumResolution * 3 + j * 3 + 0],
	    &cp.matrice[i * NumResolution * 3 + j * 3 + 1],
	    &cp.matrice[i * NumResolution * 3 + j * 3 + 2]);
	  s += 6;
	  if (cp.matrice[i * NumResolution * 3 + j * 3] > 9)
	    s++;
	  if (cp.matrice[i * NumResolution * 3 + j * 3 + 1] > 9)
	    s++;
	  if (cp.matrice[i * NumResolution * 3 + j * 3 + 2] > 9)
	    s++;
	}
	if (i < tcp_init->numlayers - 1)
	  s++;
      }
      cp.fixed_alloc = 1;
      break;
      /* ----------------------------------------------------- */
    case 't':			/* tiles */
      sscanf(optarg, "%d,%d", &cp.tdx, &cp.tdy);
      Tile_arg = 1;
      break;
      /* ----------------------------------------------------- */
    case 'n':			/* resolution */
      sscanf(optarg, "%d", &NumResolution);
      break;
      /* ----------------------------------------------------- */
    case 'c':			/* precinct dimension */
      s = optarg;
      do {
	sep = 0;
	sscanf(s, "[%d,%d]%c", &prcw_init[res_spec],
	  &prch_init[res_spec], &sep);
	CSty |= 0x01;
	res_spec++;
	s = strpbrk(s, "]") + 2;
      } while (sep == ',');
      break;
      /* ----------------------------------------------------- */
    case 'b':			/* code-block dimension */
      sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
      if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
	|| cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
	fprintf(stderr,
	  "!! Size of code_block error (option -b) !!\n\nRestriction :\n    * width*height<=4096\n    * 4<=width,height<= 1024\n\n");
	return 1;
      }
      break;
      /* ----------------------------------------------------- */
    case 'x':			/* creation of index file */
      index = optarg;
      cp.index_on = 1;
      break;
      /* ----------------------------------------------------- */
    case 'p':			/* progression order */
      s = optarg;
      for (i = 0; i < 4; i++) {
	progression[i] = *s;
	s++;
      }
      Prog_order = give_progression(progression);
      
      if (Prog_order == -1) {
	fprintf(stderr,
	  "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
	return 1;
      }
      break;
      /* ----------------------------------------------------- */
    case 's':			/* subsampling factor */
      if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy)
	!= 2) {
	fprintf(stderr,
	  "'-s' sub-sampling argument error !  [-s dx,dy]\n");
	return 1;
      }
      break;
      /* ----------------------------------------------------- */
    case 'd':			/* coordonnate of the reference grid */
      if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) {
	fprintf(stderr,
	  "-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
	return 1;
      }
      break;
      /* ----------------------------------------------------- */
    case 'h':			/* Display an help description */
      help_display();
      return 0;
      break;
      /* ----------------------------------------------------- */
    case 'P':			/* POC */
      fprintf(stderr, "/----------------------------------\\\n");
      fprintf(stderr, "|  POC option not fully tested !!  |\n");
      fprintf(stderr, "\\----------------------------------/\n");
      
      s = optarg;
      while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
	&POC[numpocs].resno0, &POC[numpocs].compno0,
	&POC[numpocs].layno1, &POC[numpocs].resno1,
	&POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
	POC[numpocs].prg = give_progression(POC[numpocs].progorder);
	/* POC[numpocs].tile; */
	numpocs++;
	while (*s && *s != '/') {
	  s++;
	}
	if (!*s)
	  break;
	s++;
      }
      break;
      /* ------------------------------------------------------ */
    case 'S':			/* SOP marker */
      CSty |= 0x02;
      break;
      /* ------------------------------------------------------ */
    case 'E':			/* EPH marker */
      CSty |= 0x04;
      break;
      /* ------------------------------------------------------ */
    case 'M':			/* Mode switch pas tous au point !! */
      if (sscanf(optarg, "%d", &value) == 1) {
	for (i = 0; i <= 5; i++) {
	  int cache = value & (1 << i);
	  if (cache)
	    mode |= (1 << i);
	}
      }
      break;
      /* ------------------------------------------------------ */
    case 'R':			/* ROI */
      if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
	fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
	return 1;
      }
      break;
      /* ------------------------------------------------------ */
    case 'T':			/* Tile offset */
      if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
	fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
	return 1;
      }
      break;
      /* ------------------------------------------------------ */
    case 'C':			/* Add a comment */
      cp.comment = optarg;
      break;
      /* ------------------------------------------------------ */
    case 'I':			/* reversible or not */
      ir = 1;
      break;
      /* ------------------------------------------------------ */
    case 'W':			/* Width and Height and Cb and Cr subsampling in case of YUV format files */
      if (sscanf
	(optarg, "%d,%d,%d,%d", &w, &h, &CbCr_subsampling_dx,
	&CbCr_subsampling_dy) != 4) {
	fprintf(stderr, "-W argument error");
	return 1;
      }
      break;
      /* ------------------------------------------------------ */
    case 'F':			/* Video frame rate */
      if (sscanf(optarg, "%d", &frame_rate) != 1) {
	fprintf(stderr, "-F argument error");
	return 1;
      }
      break;
      /* ------------------------------------------------------ */
    default:
      return 1;
    }
  }
  
  cp.tx0 = TX0;
  cp.ty0 = TY0;
  
  /* Error messages */
  /* -------------- */
  if (!infile || !outfile) {
    fprintf(stderr,
      "Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n");
    return 1;
  }
  
  if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)
    && (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) {
    fprintf(stderr,
      "Error: options -r -q and -f can not be used together !!\n");
    return 1;
  }				// mod fixed_quality
  
  /* if no rate entered, lossless by default */
  if (tcp_init->numlayers == 0) {
    tcp_init->rates[tcp_init->numlayers] = 0;   //MOD antonin : losslessbug
    tcp_init->numlayers++;
    cp.disto_alloc = 1;
  }
  
  if (TX0 > Dim[0] || TY0 > Dim[1]) {
    fprintf(stderr,
      "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
      TX0, Dim[0], TY0, Dim[1]);
    return 1;
  }
  
  for (i = 0; i < numpocs; i++) {
    if (POC[i].prg == -1) {
      fprintf(stderr,
	"Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
	i + 1);
    }
  }
  
  /* to respect profile - 0 */
  /* ---------------------- */
  numD_min = 0;
  
  x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
    1) *
    subsampling_dx + 1;
  y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
    1) *
    subsampling_dy + 1;
  
  if (Tile_arg == 1) {
    cp.tw = int_ceildiv(x1 - cp.tx0, cp.tdx);
    cp.th = int_ceildiv(y1 - cp.ty0, cp.tdy);
  } else {
    cp.tdx = x1 - cp.tx0;
    cp.tdy = y1 - cp.ty0;
  }
  
  
  /* Initialization for PPM marker */
  cp.ppm = 0;
  cp.ppm_data = NULL;
  cp.ppm_previous = 0;
  cp.ppm_store = 0;
  
  numcomps = 3;			/* Because YUV files only have 3 components */
  
  tcp_init->mct = 0;		/* No component transform needed */
  
  prec = 8;			/* Because in YUV files, components have 8-bit depth */
  
  /* Init the mutiple tiles */
  /* ---------------------- */
  cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
  
  for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
    tcp = &cp.tcps[tileno];
    tcp->numlayers = tcp_init->numlayers;
    for (j = 0; j < tcp->numlayers; j++) {
      if (cp.fixed_quality)	// add fixed_quality
	tcp->distoratio[j] = tcp_init->distoratio[j];
      else
	tcp->rates[j] = tcp_init->rates[j];
    }
    tcp->csty = CSty;
    tcp->prg = Prog_order;
    tcp->mct = tcp_init->mct;
    tcp->ppt = 0;
    tcp->ppt_data = NULL;
    tcp->ppt_store = 0;
    
    numpocs_tile = 0;
    tcp->POC = 0;
    if (numpocs) {
      /* intialisation of POC */
      tcp->POC = 1;
      for (i = 0; i < numpocs; i++) {
	if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
	  tcp_poc = &tcp->pocs[numpocs_tile];
	  tcp_poc->resno0 = POC[numpocs_tile].resno0;
	  tcp_poc->compno0 = POC[numpocs_tile].compno0;
	  tcp_poc->layno1 = POC[numpocs_tile].layno1;
	  tcp_poc->resno1 = POC[numpocs_tile].resno1;
	  tcp_poc->compno1 = POC[numpocs_tile].compno1;
	  tcp_poc->prg = POC[numpocs_tile].prg;
	  tcp_poc->tile = POC[numpocs_tile].tile;
	  numpocs_tile++;
	}
      }
    }
    tcp->numpocs = numpocs_tile;
    tcp->tccps = (j2k_tccp_t *) malloc(numcomps * sizeof(j2k_tccp_t));
    
    for (i = 0; i < numcomps; i++) {
      tccp = &tcp->tccps[i];
      tccp->csty = CSty & 0x01;	/* 0 => one precinct || 1 => custom precinct  */
      tccp->numresolutions = NumResolution;
      tccp->cblkw = int_floorlog2(cblockw_init);
      tccp->cblkh = int_floorlog2(cblockh_init);
      tccp->cblksty = mode;
      tccp->qmfbid = ir ? 0 : 1;
      tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
      tccp->numgbits = 2;
      if (i == ROI_compno)
	tccp->roishift = ROI_shift;
      else
	tccp->roishift = 0;
      if (CSty & J2K_CCP_CSTY_PRT) {
	int p = 0;
	for (j = tccp->numresolutions - 1; j >= 0; j--) {
	  if (p < res_spec) {
	    if (prcw_init[p] < 1)
	      tccp->prcw[j] = 1;
	    else
	      tccp->prcw[j] = int_floorlog2(prcw_init[p]);
	    
	    if (prch_init[p] < 1)
	      tccp->prch[j] = 1;
	    else
	      tccp->prch[j] = int_floorlog2(prch_init[p]);
	  } else {
	    int size_prcw, size_prch;
	    size_prcw = prcw_init[res_spec - 1] >> (p - (res_spec - 1));
	    size_prch = prch_init[res_spec - 1] >> (p - (res_spec - 1));
	    if (size_prcw < 1)
	      tccp->prcw[j] = 1;
	    else
	      tccp->prcw[j] = int_floorlog2(size_prcw);
	    if (size_prch < 1)
	      tccp->prch[j] = 1;
	    else
	      tccp->prch[j] = int_floorlog2(size_prch);
	  }
	  p++;
	  /*printf("\nsize precinct pour level %d : %d,%d\n", j,
	  tccp->prcw[j], tccp->prch[j]); */
	}
      } else {
	for (j = 0; j < tccp->numresolutions; j++) {
	  tccp->prcw[j] = 15;
	  tccp->prch[j] = 15;
	}
      }
      calc_explicit_stepsizes(tccp, prec);
    }
Beispiel #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++) {
Beispiel #8
0
opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){ 
	int p, q, pino;
	int compno, resno;
	int maxres = 0;
	int maxprec = 0;
	opj_pi_iterator_t *pi = NULL;
	opj_tcp_t *tcp = NULL;
	opj_tccp_t *tccp = NULL;
	size_t array_size;
	
	tcp = &cp->tcps[tileno];

	array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
	pi = (opj_pi_iterator_t *) opj_malloc(array_size);
	if(!pi) {	return NULL;}
	pi->tp_on = cp->tp_on;
	
	for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
		p = tileno % cp->tw;
		q = tileno / cp->tw;

		pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
		pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
		pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
		pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
		pi[pino].numcomps = image->numcomps;
		
		array_size = image->numcomps * sizeof(opj_pi_comp_t);
		pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
		if(!pi[pino].comps) {
			pi_destroy(pi, cp, tileno);
			return NULL;
		}
		memset(pi[pino].comps, 0, array_size);
		
		for (compno = 0; compno < pi[pino].numcomps; compno++) {
			int tcx0, tcy0, tcx1, tcy1;
			opj_pi_comp_t *comp = &pi[pino].comps[compno];
			tccp = &tcp->tccps[compno];
			comp->dx = image->comps[compno].dx;
			comp->dy = image->comps[compno].dy;
			comp->numresolutions = tccp->numresolutions;

			array_size = comp->numresolutions * sizeof(opj_pi_resolution_t);
			comp->resolutions =	(opj_pi_resolution_t *) opj_malloc(array_size);
			if(!comp->resolutions) {
				pi_destroy(pi, cp, tileno);
				return NULL;
			}

			tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
			tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
			tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
			tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
			if (comp->numresolutions > maxres) {
				maxres = comp->numresolutions;
			}

			for (resno = 0; resno < comp->numresolutions; resno++) {
				int levelno;
				int rx0, ry0, rx1, ry1;
				int px0, py0, px1, py1;
				opj_pi_resolution_t *res = &comp->resolutions[resno];
				if (tccp->csty & J2K_CCP_CSTY_PRT) {
					res->pdx = tccp->prcw[resno];
					res->pdy = tccp->prch[resno];
				} else {
					res->pdx = 15;
					res->pdy = 15;
				}
				levelno = comp->numresolutions - 1 - resno;
				rx0 = int_ceildivpow2(tcx0, levelno);
				ry0 = int_ceildivpow2(tcy0, levelno);
				rx1 = int_ceildivpow2(tcx1, levelno);
				ry1 = int_ceildivpow2(tcy1, levelno);
				px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
				py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
				px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
				py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
				res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
				res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);

				if (res->pw*res->ph > maxprec) {
					maxprec = res->pw * res->ph;
				}
			}
		}
		
		tccp = &tcp->tccps[0];
		pi[pino].step_p = 1;
		pi[pino].step_c = maxprec * pi[pino].step_p;
		pi[pino].step_r = image->numcomps * pi[pino].step_c;
		pi[pino].step_l = maxres * pi[pino].step_r;
		
		for (compno = 0; compno < pi->numcomps; compno++) {
			opj_pi_comp_t *comp = &pi->comps[compno];
			for (resno = 0; resno < comp->numresolutions; resno++) {
				int dx, dy;
				opj_pi_resolution_t *res = &comp->resolutions[resno];
				dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
				dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
				pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
				pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
			}
		}

		if (pino == 0) {
			array_size = tcp->numlayers * pi[pino].step_l * sizeof(short int);
			pi[pino].include = (short int *) opj_malloc(array_size);
			if(!pi[pino].include) {
				pi_destroy(pi, cp, tileno);
				return NULL;
			}
		}
		else {
			pi[pino].include = pi[pino - 1].include;
		}
		
		/* Generation of boundaries for each prog flag*/
			if(tcp->POC & (t2_mode == FINAL_PASS)){
				tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
				tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
				tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
				tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
				tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
				tcp->pocs[pino].prg  = tcp->pocs[pino].prg1;
				if (pino > 0)
					tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
			}else {
				tcp->pocs[pino].compS= 0;
				tcp->pocs[pino].compE= image->numcomps;
				tcp->pocs[pino].resS = 0;
				tcp->pocs[pino].resE = maxres;
				tcp->pocs[pino].layS = 0;
				tcp->pocs[pino].layE = tcp->numlayers;
				tcp->pocs[pino].prg  = tcp->prg;
			}
			tcp->pocs[pino].prcS = 0;
			tcp->pocs[pino].prcE = maxprec;;
			tcp->pocs[pino].txS = pi[pino].tx0;
			tcp->pocs[pino].txE = pi[pino].tx1;
			tcp->pocs[pino].tyS = pi[pino].ty0;
			tcp->pocs[pino].tyE = pi[pino].ty1;
			tcp->pocs[pino].dx = pi[pino].dx;
			tcp->pocs[pino].dy = pi[pino].dy;
		}
			return pi;
	}
Beispiel #9
0
static void setparams(opj_mj2_t *movie, opj_image_t *image) {
  int i, depth_0, depth, sign;
  
  movie->tk[0].sample_rate = 25;
  movie->tk[0].w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
  movie->tk[0].h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
  mj2_init_stdmovie(movie);
  
  movie->tk[0].depth = image->comps[0].prec;
	
  if (image->numcomps==3) {
    if ((image->comps[0].dx == 1) 
	&& (image->comps[1].dx == 1) 
	&& (image->comps[2].dx == 1)) 
      movie->tk[0].CbCr_subsampling_dx = 1;
    else 
	if ((image->comps[0].dx == 1) 
	&& (image->comps[1].dx == 2) 
	&& (image->comps[2].dx == 2))
      movie->tk[0].CbCr_subsampling_dx = 2;
    else
      fprintf(stderr,"Image component sizes are incoherent\n");
    
    if ((image->comps[0].dy == 1) 
	&& (image->comps[1].dy == 1) 
	&& (image->comps[2].dy == 1)) 
      movie->tk[0].CbCr_subsampling_dy = 1;
    else 
	if ((image->comps[0].dy == 1) 
	&& (image->comps[1].dy == 2) 
	&& (image->comps[2].dy == 2))
      movie->tk[0].CbCr_subsampling_dy = 2;
    else
      fprintf(stderr,"Image component sizes are incoherent\n");
  }
  
  movie->tk[0].sample_rate = 25;
  
  movie->tk[0].jp2_struct.numcomps = image->numcomps;	// NC  
	
	/* Init Standard jp2 structure */
	
	movie->tk[0].jp2_struct.comps =
    (opj_jp2_comps_t *) malloc(movie->tk[0].jp2_struct.numcomps * sizeof(opj_jp2_comps_t));
  movie->tk[0].jp2_struct.precedence = 0;   /* PRECEDENCE*/
  movie->tk[0].jp2_struct.approx = 0;   /* APPROX*/
  movie->tk[0].jp2_struct.brand = JP2_JP2;	/* BR         */
  movie->tk[0].jp2_struct.minversion = 0;	/* MinV       */
  movie->tk[0].jp2_struct.numcl = 1;
  movie->tk[0].jp2_struct.cl = (unsigned int *) malloc(movie->tk[0].jp2_struct.numcl * sizeof(int));
  movie->tk[0].jp2_struct.cl[0] = JP2_JP2;	/* CL0 : JP2  */
  movie->tk[0].jp2_struct.C = 7;      /* C : Always 7*/
  movie->tk[0].jp2_struct.UnkC = 0;      /* UnkC, colorspace specified in colr box*/
  movie->tk[0].jp2_struct.IPR = 0;      /* IPR, no intellectual property*/
  movie->tk[0].jp2_struct.w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
  movie->tk[0].jp2_struct.h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
  
  depth_0 = image->comps[0].prec - 1;
  sign = image->comps[0].sgnd;
  movie->tk[0].jp2_struct.bpc = depth_0 + (sign << 7);
  
  for (i = 1; i < image->numcomps; i++) {
    depth = image->comps[i].prec - 1;
    sign = image->comps[i].sgnd;
    if (depth_0 != depth)
      movie->tk[0].jp2_struct.bpc = 255;
  }
  
  for (i = 0; i < image->numcomps; i++)
    movie->tk[0].jp2_struct.comps[i].bpcc =
    image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
  
  if ((image->numcomps == 1 || image->numcomps == 3)
    && (movie->tk[0].jp2_struct.bpc != 255))
    movie->tk[0].jp2_struct.meth = 1;
  else
    movie->tk[0].jp2_struct.meth = 2;
	
    if (image->numcomps == 1)
     movie->tk[0].jp2_struct.enumcs = 17;  // Grayscale
  
    else   
	if ((image->comps[0].dx == 1) 
	&& (image->comps[1].dx == 1) 
	&& (image->comps[2].dx == 1) 
    && (image->comps[0].dy == 1) 
	&& (image->comps[1].dy == 1) 
	&& (image->comps[2].dy == 1)) 
     movie->tk[0].jp2_struct.enumcs = 16;    // RGB
  
    else   
	if ((image->comps[0].dx == 1) 
	&& (image->comps[1].dx == 2) 
	&& (image->comps[2].dx == 2) 
    && (image->comps[0].dy == 1) 
	&& (image->comps[1].dy == 2) 
	&& (image->comps[2].dy == 2)) 
     movie->tk[0].jp2_struct.enumcs = 18;  // YUV
  
  else
    movie->tk[0].jp2_struct.enumcs = 0;	// Unkown profile */
}
Beispiel #10
0
void j2k_read_siz() {
    int len, i;
    info_tile_t *tile;

    len = cio_read(2);

    /* <INDEX> [MHIX BOX] */
    img.marker[img.num_marker].type = J2K_MS_SIZ;
    img.marker[img.num_marker].start_pos = cio_tell()-2;
    img.marker[img.num_marker].len = len;
    img.num_marker++;
    /* </INDEX> [MHIX BOX] */
    
    cio_read(2);                      /* Rsiz (capabilities) */
    j2k_img->x1 = cio_read(4);        /* Xsiz                */
    j2k_img->y1 = cio_read(4);        /* Ysiz                */
    j2k_img->x0 = cio_read(4);        /* X0siz               */
    j2k_img->y0 = cio_read(4);        /* Y0siz               */
    j2k_cp->tdx = cio_read(4);        /* XTsiz               */
    j2k_cp->tdy = cio_read(4);        /* YTsiz               */
    j2k_cp->tx0 = cio_read(4);        /* XT0siz              */
    j2k_cp->ty0 = cio_read(4);        /* YTOsiz              */

    j2k_img->numcomps = cio_read(2);  /* Csiz                */
    j2k_img->comps = (j2k_comp_t*)malloc(j2k_img->numcomps * sizeof(j2k_comp_t));
    for (i = 0; i < j2k_img->numcomps; i++) {
        int tmp, w, h;
        tmp = cio_read(1);
        j2k_img->comps[i].prec = (tmp & 0x7f) + 1;
        j2k_img->comps[i].sgnd = tmp >> 7;
        j2k_img->comps[i].dx = cio_read(1);
        j2k_img->comps[i].dy = cio_read(1);
        w = int_ceildiv(j2k_img->x1-j2k_img->x0, j2k_img->comps[i].dx);
        h = int_ceildiv(j2k_img->y1-j2k_img->y0, j2k_img->comps[i].dy);
        j2k_img->comps[i].data = (int*)malloc(sizeof(int) * w * h);
    }
    j2k_cp->tw = int_ceildiv(j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx);
    j2k_cp->th = int_ceildiv(j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy);

    j2k_cp->tcps = (j2k_tcp_t*)calloc((j2k_cp->tw * j2k_cp->th), sizeof(j2k_tcp_t));

    for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++)
      {
	j2k_cp->tcps[i].POC = 0;
	j2k_cp->tcps[i].numpocs = 0;
	// j2k_cp->tcps[i].first=1;
      }

    /* Initialization for PPM marker */
    j2k_cp->ppm = 0;
    j2k_cp->ppm_data = NULL;
    j2k_cp->ppm_previous = 0;
    j2k_cp->ppm_store = 0;

    j2k_default_tcp.tccps = (j2k_tccp_t*)malloc(j2k_img->numcomps * sizeof(j2k_tccp_t));
    for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) {
	j2k_cp->tcps[i].tccps = (j2k_tccp_t*)malloc(j2k_img->numcomps * sizeof(j2k_tccp_t));
    }
    j2k_tile_data = (unsigned char**)calloc(j2k_cp->tw * j2k_cp->th, sizeof(char*));
    j2k_tile_len = (int*)calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));
    j2k_state = J2K_STATE_MH;

    /* <INDEX> */
    img.Im_w = j2k_img->x1 - j2k_img->x0;
    img.Im_h = j2k_img->y1 - j2k_img->y0;
    img.Tile_x = j2k_cp->tdx;
    img.Tile_y = j2k_cp->tdy;
    img.Comp = j2k_img->numcomps;
    img.tw = j2k_cp->tw;
    img.th = j2k_cp->th;
    img.tile = (info_tile_t*)malloc(img.tw * img.th * sizeof(info_tile_t));

    for (i = 0; i < img.tw * img.th; i++)
      {
	tile = &img.tile[i];
	tile->marker = (info_marker_t*)malloc(32 * sizeof(info_marker_t));
	tile->num_marker = 0;
	tile->marker_mul.num_COC = 0;
	tile->marker_mul.CzCOC = START_NB;
	tile->marker_mul.num_RGN = 0;
	tile->marker_mul.CzRGN = START_NB;
	tile->marker_mul.num_QCC = 0;
	tile->marker_mul.CzQCC = START_NB;
	tile->marker_mul.num_PLT = 0;
	tile->marker_mul.CzPLT = START_NB;
	tile->marker_mul.num_PPT = 0;
	tile->marker_mul.CzPPT = START_NB;
	tile->marker_mul.num_COM = 0;
	tile->marker_mul.CzCOM = START_NB;
      }
    /* </INDEX> */


 }
Beispiel #11
0
void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
	int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;
	unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;

	tcd->image = image;
	tcd->cp = cp;
	tcd->tcd_image->tw = cp->tw;
	tcd->tcd_image->th = cp->th;
	tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
	
	for (i = 0; i < cp->tileno_size; i++) {
		opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]);
		opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[cp->tileno[i]]);
	
		/* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
		tileno = cp->tileno[i];
		p = tileno % cp->tw;	/* si numerotation matricielle .. */
		q = tileno / cp->tw;	/* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */

		/* 4 borders of the tile rescale on the image if necessary */
		tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
		tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
		tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
		tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
		
		tile->numcomps = image->numcomps;
		tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
		for (compno = 0; compno < tile->numcomps; compno++) {
			opj_tccp_t *tccp = &tcp->tccps[compno];
			opj_tcd_tilecomp_t *tilec = &tile->comps[compno];

			/* border of each tile component (global) */
			tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
			tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
			tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
			tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
			
			tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
			tilec->numresolutions = tccp->numresolutions;
			tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));

			for (resno = 0; resno < tilec->numresolutions; resno++) {
				int pdx, pdy;
				int levelno = tilec->numresolutions - 1 - resno;
				int tlprcxstart, tlprcystart, brprcxend, brprcyend;
				int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
				int cbgwidthexpn, cbgheightexpn;
				int cblkwidthexpn, cblkheightexpn;

				opj_tcd_resolution_t *res = &tilec->resolutions[resno];
				
				/* border for each resolution level (global) */
				res->x0 = int_ceildivpow2(tilec->x0, levelno);
				res->y0 = int_ceildivpow2(tilec->y0, levelno);
				res->x1 = int_ceildivpow2(tilec->x1, levelno);
				res->y1 = int_ceildivpow2(tilec->y1, levelno);
				res->numbands = resno == 0 ? 1 : 3;
				
				/* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
				if (tccp->csty & J2K_CCP_CSTY_PRT) {
					pdx = tccp->prcw[resno];
					pdy = tccp->prch[resno];
				} else {
					pdx = 15;
					pdy = 15;
				}
				
				/* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
				tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
				tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
				brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
				brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;

				res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
				res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
				
				if (resno == 0) {
					tlcbgxstart = tlprcxstart;
					tlcbgystart = tlprcystart;
					brcbgxend = brprcxend;
					brcbgyend = brprcyend;
					cbgwidthexpn = pdx;
					cbgheightexpn = pdy;
				} else {
					tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
					tlcbgystart = int_ceildivpow2(tlprcystart, 1);
					brcbgxend = int_ceildivpow2(brprcxend, 1);
					brcbgyend = int_ceildivpow2(brprcyend, 1);
					cbgwidthexpn = pdx - 1;
					cbgheightexpn = pdy - 1;
				}
				
				cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
				cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
				
				for (bandno = 0; bandno < res->numbands; bandno++) {
					int x0b, y0b;
					int gain, numbps;
					opj_stepsize_t *ss = NULL;

					opj_tcd_band_t *band = &res->bands[bandno];
					band->bandno = resno == 0 ? 0 : bandno + 1;
					x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
					y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
					
					if (band->bandno == 0) {
						/* band border (global) */
						band->x0 = int_ceildivpow2(tilec->x0, levelno);
						band->y0 = int_ceildivpow2(tilec->y0, levelno);
						band->x1 = int_ceildivpow2(tilec->x1, levelno);
						band->y1 = int_ceildivpow2(tilec->y1, levelno);
					} else {
						/* band border (global) */
						band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
						band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
						band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
						band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
					}
					
					ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
					gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
					numbps = image->comps[compno].prec + gain;
					band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
					band->numbps = ss->expn + tccp->numgbits - 1;	/* WHY -1 ? */
					
					band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
					
					for (precno = 0; precno < res->pw * res->ph; precno++) {
						int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
						int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
						int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
						int cbgxend = cbgxstart + (1 << cbgwidthexpn);
						int cbgyend = cbgystart + (1 << cbgheightexpn);

						opj_tcd_precinct_t *prc = &band->precincts[precno];
						/* precinct size (global) */
						prc->x0 = int_max(cbgxstart, band->x0);
						prc->y0 = int_max(cbgystart, band->y0);
						prc->x1 = int_min(cbgxend, band->x1);
						prc->y1 = int_min(cbgyend, band->y1);
						
						tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
						tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
						brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
						brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
						prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
						prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
						
						prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
						
						prc->incltree = tgt_create(prc->cw, prc->ch);
						prc->imsbtree = tgt_create(prc->cw, prc->ch);
						
						for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
							int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
							int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
							int cblkxend = cblkxstart + (1 << cblkwidthexpn);
							int cblkyend = cblkystart + (1 << cblkheightexpn);					
							
							/* code-block size (global) */
							opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
							cblk->x0 = int_max(cblkxstart, prc->x0);
							cblk->y0 = int_max(cblkystart, prc->y0);
							cblk->x1 = int_min(cblkxend, prc->x1);
							cblk->y1 = int_min(cblkyend, prc->y1);
						}
					} /* precno */
				} /* bandno */
			} /* resno */
		} /* compno */
	} /* i = 0..cp->tileno_size */

	/* tcd_dump(stdout, tcd, &tcd->tcd_image); */

	/* 
	Allocate place to store the decoded data = final image
	Place limited by the tile really present in the codestream 
	*/
	
	for (i = 0; i < image->numcomps; i++) {
		for (j = 0; j < cp->tileno_size; j++) {
			tileno = cp->tileno[j];
			x0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x0 : int_min(x0,
				(unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x0);
			y0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y0 : int_min(y0,
				(unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y0);
			x1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x1 : int_max(x1,
				(unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x1);
			y1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y1 : int_max(y1, 
				(unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y1);
		}
		
		w = x1 - x0;
		h = y1 - y0;
		
		image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
		image->comps[i].w = w;
		image->comps[i].h = h;
		image->comps[i].x0 = x0;
		image->comps[i].y0 = y0;
	}
}
Beispiel #12
0
void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
	int tileno, compno, resno, bandno, precno, cblkno;

	for (tileno = 0; tileno < 1; tileno++) {
		opj_tcp_t *tcp = &cp->tcps[curtileno];
		int j;
		/* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
		int p = curtileno % cp->tw;
		int q = curtileno / cp->tw;

		opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
		
		/* 4 borders of the tile rescale on the image if necessary */
		tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
		tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
		tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
		tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
		
		tile->numcomps = image->numcomps;
		/* tile->PPT=image->PPT; */

		/* Modification of the RATE >> */
		for (j = 0; j < tcp->numlayers; j++) {
			tcp->rates[j] = tcp->rates[j] ? 
				int_ceildiv(tile->numcomps 
				* (tile->x1 - tile->x0) 
				* (tile->y1 - tile->y0) 
				* image->comps[0].prec, 
				(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) 
				: 0;

			if (tcp->rates[j]) {
				if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
					tcp->rates[j] = tcp->rates[j - 1] + 20;
				} else {
					if (!j && tcp->rates[j] < 30)
						tcp->rates[j] = 30;
				}
			}
		}
		/* << Modification of the RATE */

		/* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
		for (compno = 0; compno < tile->numcomps; compno++) {
			opj_tccp_t *tccp = &tcp->tccps[compno];
			
			opj_tcd_tilecomp_t *tilec = &tile->comps[compno];

			/* border of each tile component (global) */
			tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
			tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
			tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
			tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
			
			tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
			tilec->numresolutions = tccp->numresolutions;
			/* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
			for (resno = 0; resno < tilec->numresolutions; resno++) {
				int pdx, pdy;

				int levelno = tilec->numresolutions - 1 - resno;
				int tlprcxstart, tlprcystart, brprcxend, brprcyend;
				int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
				int cbgwidthexpn, cbgheightexpn;
				int cblkwidthexpn, cblkheightexpn;
				
				opj_tcd_resolution_t *res = &tilec->resolutions[resno];

				/* border for each resolution level (global) */
				res->x0 = int_ceildivpow2(tilec->x0, levelno);
				res->y0 = int_ceildivpow2(tilec->y0, levelno);
				res->x1 = int_ceildivpow2(tilec->x1, levelno);
				res->y1 = int_ceildivpow2(tilec->y1, levelno);	
				res->numbands = resno == 0 ? 1 : 3;

				/* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
				if (tccp->csty & J2K_CCP_CSTY_PRT) {
					pdx = tccp->prcw[resno];
					pdy = tccp->prch[resno];
				} else {
					pdx = 15;
					pdy = 15;
				}
				/* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
				tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
				tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
				brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
				brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
				
				res->pw = (brprcxend - tlprcxstart) >> pdx;
				res->ph = (brprcyend - tlprcystart) >> pdy;
				
				if (resno == 0) {
					tlcbgxstart = tlprcxstart;
					tlcbgystart = tlprcystart;
					brcbgxend = brprcxend;
					brcbgyend = brprcyend;
					cbgwidthexpn = pdx;
					cbgheightexpn = pdy;
				} else {
					tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
					tlcbgystart = int_ceildivpow2(tlprcystart, 1);
					brcbgxend = int_ceildivpow2(brprcxend, 1);
					brcbgyend = int_ceildivpow2(brprcyend, 1);
					cbgwidthexpn = pdx - 1;
					cbgheightexpn = pdy - 1;
				}
				
				cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
				cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
				
				for (bandno = 0; bandno < res->numbands; bandno++) {
					int x0b, y0b;
					int gain, numbps;
					opj_stepsize_t *ss = NULL;

					opj_tcd_band_t *band = &res->bands[bandno];

					band->bandno = resno == 0 ? 0 : bandno + 1;
					x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
					y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
					
					if (band->bandno == 0) {
						/* band border */
						band->x0 = int_ceildivpow2(tilec->x0, levelno);
						band->y0 = int_ceildivpow2(tilec->y0, levelno);
						band->x1 = int_ceildivpow2(tilec->x1, levelno);
						band->y1 = int_ceildivpow2(tilec->y1, levelno);
					} else {
						band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
						band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
						band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
						band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
					}
					
					ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
					gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
					numbps = image->comps[compno].prec + gain;
					band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
					band->numbps = ss->expn + tccp->numgbits - 1;	/* WHY -1 ? */
					
					for (precno = 0; precno < res->pw * res->ph; precno++) {
						int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;

						int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
						int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
						int cbgxend = cbgxstart + (1 << cbgwidthexpn);
						int cbgyend = cbgystart + (1 << cbgheightexpn);
						
						opj_tcd_precinct_t *prc = &band->precincts[precno];

						/* precinct size (global) */
						prc->x0 = int_max(cbgxstart, band->x0);
						prc->y0 = int_max(cbgystart, band->y0);
						prc->x1 = int_min(cbgxend, band->x1);
						prc->y1 = int_min(cbgyend, band->y1);

						tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
						tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
						brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
						brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
						prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
						prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;

						opj_free(prc->cblks);
						prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));

						if (prc->incltree != NULL) {
							tgt_destroy(prc->incltree);
						}
						if (prc->imsbtree != NULL) {
							tgt_destroy(prc->imsbtree);
						}
						
						prc->incltree = tgt_create(prc->cw, prc->ch);
						prc->imsbtree = tgt_create(prc->cw, prc->ch);

						for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
							int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
							int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
							int cblkxend = cblkxstart + (1 << cblkwidthexpn);
							int cblkyend = cblkystart + (1 << cblkheightexpn);

							opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
							
							/* code-block size (global) */
							cblk->x0 = int_max(cblkxstart, prc->x0);
							cblk->y0 = int_max(cblkystart, prc->y0);
							cblk->x1 = int_min(cblkxend, prc->x1);
							cblk->y1 = int_min(cblkyend, prc->y1);
						}
					} /* precno */
				} /* bandno */
			} /* resno */
		} /* compno */
	} /* tileno */

	/* tcd_dump(stdout, tcd, &tcd->tcd_image); */
}
Beispiel #13
0
void tcd_init(j2k_image_t *img, j2k_cp_t *cp, info_image_t *imgg) {
    int tileno, compno, resno, bandno, precno, cblkno;
    tcd_img=img;
    tcd_cp=cp;
    tcd_image.tw=cp->tw;
    tcd_image.th=cp->th;
    tcd_image.tiles=(tcd_tile_t*)malloc(cp->tw*cp->th*sizeof(tcd_tile_t));
    for (tileno=0; tileno<cp->tw*cp->th; tileno++) {
        j2k_tcp_t *tcp=&cp->tcps[tileno];
        tcd_tile_t *tile=&tcd_image.tiles[tileno];
        // cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000)
        int p=tileno%cp->tw;  // si numerotation matricielle ..
        int q=tileno/cp->tw;  // .. coordonnees de la tile (q,p) q pour ligne et p pour colonne
	info_tile_t *tile_Idx=&imgg->tile[tileno]; // INDEX

	// 4 borders of the tile rescale on the image if necessary
        tile->x0=int_max(cp->tx0+p*cp->tdx, img->x0);
        tile->y0=int_max(cp->ty0+q*cp->tdy, img->y0);
        tile->x1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1);
        tile->y1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1);
	
        tile->numcomps=img->numcomps;
        tile->comps=(tcd_tilecomp_t*)malloc(img->numcomps*sizeof(tcd_tilecomp_t));
        tile_Idx->compo=(info_compo_t*)malloc(img->numcomps*sizeof(info_compo_t)); // INDEX
	for (compno=0; compno<tile->numcomps; compno++) {
            j2k_tccp_t *tccp=&tcp->tccps[compno];
            tcd_tilecomp_t *tilec=&tile->comps[compno];
	    info_compo_t *compo_Idx=&tile_Idx->compo[compno]; // INDEX

	    // border of each tile component (global)
            tilec->x0=int_ceildiv(tile->x0, img->comps[compno].dx);
            tilec->y0=int_ceildiv(tile->y0, img->comps[compno].dy);
	    tilec->x1=int_ceildiv(tile->x1, img->comps[compno].dx);
            tilec->y1=int_ceildiv(tile->y1, img->comps[compno].dy);
	    
            tilec->data=(int*)malloc(sizeof(int)*(tilec->x1-tilec->x0)*(tilec->y1-tilec->y0));
            tilec->numresolutions=tccp->numresolutions;
            tilec->resolutions=(tcd_resolution_t*)malloc(tilec->numresolutions*sizeof(tcd_resolution_t));
	    compo_Idx->reso=(info_reso_t*)malloc(tilec->numresolutions*sizeof(info_reso_t)); // INDEX
            for (resno=0; resno<tilec->numresolutions; resno++) {
                int pdx, pdy;
                int levelno=tilec->numresolutions-1-resno;
                int tlprcxstart, tlprcystart, brprcxend, brprcyend;
                int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
                int cbgwidthexpn, cbgheightexpn;
                int cblkwidthexpn, cblkheightexpn;
                tcd_resolution_t *res=&tilec->resolutions[resno];
		info_reso_t *res_Idx=&compo_Idx->reso[resno]; // INDEX
		int precno_Idx; // INDEX

		// border for each resolution level (global)
                res->x0=int_ceildivpow2(tilec->x0, levelno);
                res->y0=int_ceildivpow2(tilec->y0, levelno);
                res->x1=int_ceildivpow2(tilec->x1, levelno);
                res->y1=int_ceildivpow2(tilec->y1, levelno);
		
		res->numbands=resno==0?1:3;
		// p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000)
                if (tccp->csty&J2K_CCP_CSTY_PRT) {
                    pdx=tccp->prcw[resno];
                    pdy=tccp->prch[resno];
		} else {
                    pdx=15;
                    pdy=15;
                }
		// p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) 
                tlprcxstart=int_floordivpow2(res->x0, pdx)<<pdx;
                tlprcystart=int_floordivpow2(res->y0, pdy)<<pdy;
                brprcxend=int_ceildivpow2(res->x1, pdx)<<pdx;
                brprcyend=int_ceildivpow2(res->y1, pdy)<<pdy;
                res->pw=(brprcxend-tlprcxstart)>>pdx;
                res->ph=(brprcyend-tlprcystart)>>pdy;

		// <INDEX>
		imgg->tile[tileno].pw=res->pw;
		imgg->tile[tileno].ph=res->ph;
		
		res_Idx->prec=(info_prec_t*)malloc(res->pw*res->ph*sizeof(info_prec_t));
		for (precno_Idx=0;precno_Idx<res->pw*res->ph;precno_Idx++)
		  {
		    info_prec_t *prec_Idx = &res_Idx->prec[precno_Idx];
		    prec_Idx->layer=(info_layer_t*)malloc(imgg->Layer*sizeof(info_layer_t));
		  }
		
		imgg->pw=res->pw;  // old parser version
		imgg->ph=res->ph;  // old parser version
		imgg->pdx=1<<pdx;
		imgg->pdy=1<<pdy;
		// </INDEX>

                if (resno==0) {
                    tlcbgxstart=tlprcxstart;
                    tlcbgystart=tlprcystart;
                    brcbgxend=brprcxend;
                    brcbgyend=brprcyend;
                    cbgwidthexpn=pdx;
                    cbgheightexpn=pdy;
                } else {
                    tlcbgxstart=int_ceildivpow2(tlprcxstart, 1);
                    tlcbgystart=int_ceildivpow2(tlprcystart, 1);
                    brcbgxend=int_ceildivpow2(brprcxend, 1);
                    brcbgyend=int_ceildivpow2(brprcyend, 1);
                    cbgwidthexpn=pdx-1;
                    cbgheightexpn=pdy-1;
                }

                cblkwidthexpn=int_min(tccp->cblkw, cbgwidthexpn);
                cblkheightexpn=int_min(tccp->cblkh, cbgheightexpn);

                for (bandno=0; bandno<res->numbands; bandno++) {
                    int x0b, y0b;
                    tcd_band_t *band=&res->bands[bandno];
                    band->bandno=resno==0?0:bandno+1;
                    x0b=(band->bandno==1)||(band->bandno==3)?1:0;
                    y0b=(band->bandno==2)||(band->bandno==3)?1:0;

                    if (band->bandno==0) {
		      // band border (global)
		      band->x0=int_ceildivpow2(tilec->x0, levelno);
		      band->y0=int_ceildivpow2(tilec->y0, levelno);
		      band->x1=int_ceildivpow2(tilec->x1, levelno);
		      band->y1=int_ceildivpow2(tilec->y1, levelno);
                    } else {
		      // band border (global)
		      band->x0=int_ceildivpow2(tilec->x0-(1<<levelno)*x0b, levelno+1);
		      band->y0=int_ceildivpow2(tilec->y0-(1<<levelno)*y0b, levelno+1);
		      band->x1=int_ceildivpow2(tilec->x1-(1<<levelno)*x0b, levelno+1);
		      band->y1=int_ceildivpow2(tilec->y1-(1<<levelno)*y0b, levelno+1);
                    }

                    band->precincts=(tcd_precinct_t*)malloc(res->pw*res->ph*sizeof(tcd_precinct_t));

                    for (precno=0; precno<res->pw*res->ph; precno++) {
                        int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
                        int cbgxstart=tlcbgxstart+(precno%res->pw)*(1<<cbgwidthexpn);
                        int cbgystart=tlcbgystart+(precno/res->pw)*(1<<cbgheightexpn);
                        int cbgxend=cbgxstart+(1<<cbgwidthexpn);
                        int cbgyend=cbgystart+(1<<cbgheightexpn);
                        tcd_precinct_t *prc=&band->precincts[precno];
			// precinct size (global)
                        prc->x0=int_max(cbgxstart, band->x0);
                        prc->y0=int_max(cbgystart, band->y0);
                        prc->x1=int_min(cbgxend, band->x1);
                        prc->y1=int_min(cbgyend, band->y1);

                        tlcblkxstart=int_floordivpow2(prc->x0, cblkwidthexpn)<<cblkwidthexpn;
                        tlcblkystart=int_floordivpow2(prc->y0, cblkheightexpn)<<cblkheightexpn;
                        brcblkxend=int_ceildivpow2(prc->x1, cblkwidthexpn)<<cblkwidthexpn;
                        brcblkyend=int_ceildivpow2(prc->y1, cblkheightexpn)<<cblkheightexpn;
                        prc->cw=(brcblkxend-tlcblkxstart)>>cblkwidthexpn;
                        prc->ch=(brcblkyend-tlcblkystart)>>cblkheightexpn;

                        prc->cblks=(tcd_cblk_t*)malloc(prc->cw*prc->ch*sizeof(tcd_cblk_t));

                        prc->incltree=tgt_create(prc->cw, prc->ch);
                        prc->imsbtree=tgt_create(prc->cw, prc->ch);

                        for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) {
                            int cblkxstart=tlcblkxstart+(cblkno%prc->cw)*(1<<cblkwidthexpn);
                            int cblkystart=tlcblkystart+(cblkno/prc->cw)*(1<<cblkheightexpn);
                            int cblkxend=cblkxstart+(1<<cblkwidthexpn);
                            int cblkyend=cblkystart+(1<<cblkheightexpn);
                            tcd_cblk_t *cblk=&prc->cblks[cblkno];
			    // code-block size (global)
                            cblk->x0=int_max(cblkxstart, prc->x0);
                            cblk->y0=int_max(cblkystart, prc->y0);
                            cblk->x1=int_min(cblkxend, prc->x1);
                            cblk->y1=int_min(cblkyend, prc->y1);
                        }
                    }
                }
            }
        }
    }
}
Beispiel #14
0
/* <summary>
 * Get next packet in component-precinct-resolution-layer order.
 *
 * pi: packet iterator to modify
 * </summary> */
int pi_next_cprl(pi_iterator_t * pi)
{
  pi_comp_t *comp;
  pi_resolution_t *res;
  if (!pi->first) {
    comp = &pi->comps[pi->compno];
    goto skip;
  } else {
    pi->first = 0;
  }
  for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
       pi->compno++) {
    int resno;
    comp = &pi->comps[pi->compno];
    pi->dx = 0;
    pi->dy = 0;
    for (resno = 0; resno < comp->numresolutions; resno++) {
      int dx, dy;
      res = &comp->resolutions[resno];
      dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
      dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
      pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
      pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
    }
    for (pi->y = pi->ty0; pi->y < pi->ty1;
	 pi->y += pi->dy - (pi->y % pi->dy)) {
      for (pi->x = pi->tx0; pi->x < pi->tx1;
	   pi->x += pi->dx - (pi->x % pi->dx)) {
	for (pi->resno = pi->poc.resno0;
	     pi->resno < int_min(pi->poc.resno1,
				 comp->numresolutions); pi->resno++) {
	  int levelno;
	  int trx0, try0;
   int trx1, try1;/* Add antonin pcrl*/
	  int rpx, rpy;
	  int prci, prcj;
	  res = &comp->resolutions[pi->resno];
	  levelno = comp->numresolutions - 1 - pi->resno;
	  trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
	  try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
   trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/
   try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/
	  rpx = res->pdx + levelno;
	  rpy = res->pdy + levelno;
	  if (!
	      (pi->x % (comp->dx << rpx) == 0
	       || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
	    continue;
	  }
	  if (!
	      (pi->y % (comp->dy << rpy) == 0
	       || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
	    continue;
	  }

   /*Add Antonin : sizebug1*/
	  if ((res->pw==0)||(res->pw==0)) continue;
   /*ddA*/

   /*Add Antonin : pcrl*/
	  if ((trx0==trx1)||(try0==try1)) continue;
   /*ddA*/

	  prci =
	    int_floordivpow2(int_ceildiv
			     (pi->x, comp->dx << levelno),
			     res->pdx) - int_floordivpow2(trx0, res->pdx);
	  prcj =
	    int_floordivpow2(int_ceildiv
			     (pi->y, comp->dy << levelno),
			     res->pdy) - int_floordivpow2(try0, res->pdy);
	  pi->precno = prci + prcj * res->pw;
	  for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
	    if (!pi->
		include[pi->layno * pi->step_l +
			pi->resno * pi->step_r +
			pi->compno * pi->step_c +
			pi->precno * pi->step_p]) {
	      pi->include[pi->layno * pi->step_l +
			  pi->resno * pi->step_r +
			  pi->compno * pi->step_c +
			  pi->precno * pi->step_p] = 1;
	      return 1;
	    }
	  skip:;
	  }
	}
      }
    }
  }
  return 0;
}
Beispiel #15
0
/* <summary>
 * Create a packet iterator.
 * </summary> */
pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
{
  int p, q, i;
  int compno, resno, pino;
  int maxres = 0;
  pi_iterator_t *pi;
  j2k_tcp_t *tcp;
  j2k_tccp_t *tccp;

  tcp = &cp->tcps[tileno];
  pi = (pi_iterator_t *) malloc((tcp->numpocs + 1) *
				sizeof(pi_iterator_t));

  for (pino = 0; pino < tcp->numpocs + 1; pino++) {	/* change */
    p = tileno % cp->tw;
    q = tileno / cp->tw;

    pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
    pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
    pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
    pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
    pi[pino].numcomps = img->numcomps;
    pi[pino].comps =
      (pi_comp_t *) malloc(img->numcomps * sizeof(pi_comp_t));

    for (compno = 0; compno < pi->numcomps; compno++) {
      int tcx0, tcy0, tcx1, tcy1;
      pi_comp_t *comp = &pi[pino].comps[compno];
      tccp = &tcp->tccps[compno];
      comp->dx = img->comps[compno].dx;
      comp->dy = img->comps[compno].dy;
      comp->numresolutions = tccp->numresolutions;
      comp->resolutions =
	(pi_resolution_t *) malloc(comp->numresolutions *
				   sizeof(pi_resolution_t));
      tcx0 = int_ceildiv(pi->tx0, comp->dx);
      tcy0 = int_ceildiv(pi->ty0, comp->dy);
      tcx1 = int_ceildiv(pi->tx1, comp->dx);
      tcy1 = int_ceildiv(pi->ty1, comp->dy);
      if (comp->numresolutions > maxres) {
	maxres = comp->numresolutions;
      }
      for (resno = 0; resno < comp->numresolutions; resno++) {
	int levelno;
	int rx0, ry0, rx1, ry1;
	int px0, py0, px1, py1;
	pi_resolution_t *res = &comp->resolutions[resno];
	if (tccp->csty & J2K_CCP_CSTY_PRT) {
	  res->pdx = tccp->prcw[resno];
	  res->pdy = tccp->prch[resno];
	} else {
	  res->pdx = 15;
	  res->pdy = 15;
	}
	levelno = comp->numresolutions - 1 - resno;
	rx0 = int_ceildivpow2(tcx0, levelno);
	ry0 = int_ceildivpow2(tcy0, levelno);
	rx1 = int_ceildivpow2(tcx1, levelno);
	ry1 = int_ceildivpow2(tcy1, levelno);
	px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
	py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
	px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
	py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); /*Mod Antonin : sizebug1*/
 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); /*Mod Antonin : sizebug1*/
      }
    }

    tccp = &tcp->tccps[0];
    pi[pino].step_p = 1;
    pi[pino].step_c = 100 * pi[pino].step_p;
    pi[pino].step_r = img->numcomps * pi[pino].step_c;
    pi[pino].step_l = maxres * pi[pino].step_r;

    if (pino == 0) {
      pi[pino].include =
	(short int *) malloc(img->numcomps * maxres *
			     tcp->numlayers * 100 * sizeof(short int));
      for (i = 0; i < img->numcomps * maxres * tcp->numlayers * 100; i++)
	pi[pino].include[i] = 0;
    }
    /* pi[pino].include=(short int*)calloc(img->numcomps*maxres*tcp->numlayers*1000,sizeof(short int)); */
    else
      pi[pino].include = pi[pino - 1].include;

    if (tcp->POC == 0) {
      pi[pino].first = 1;
      pi[pino].poc.resno0 = 0;
      pi[pino].poc.compno0 = 0;
      pi[pino].poc.layno1 = tcp->numlayers;
      pi[pino].poc.resno1 = maxres;
      pi[pino].poc.compno1 = img->numcomps;
      pi[pino].poc.prg = tcp->prg;
    } else {
      pi[pino].first = 1;
      pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
      pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
      pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
      pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
      pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
      pi[pino].poc.prg = tcp->pocs[pino].prg;
    }
  }
  return pi;
}
Beispiel #16
0
static bool pi_next_cprl(opj_pi_iterator_t * pi) {
	opj_pi_comp_t *comp = NULL;
	opj_pi_resolution_t *res = NULL;
	long index = 0;

	if (!pi->first) {
		comp = &pi->comps[pi->compno];
		goto LABEL_SKIP;
	} else {
		pi->first = 0;
	}

	for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
		int resno;
		comp = &pi->comps[pi->compno];
		pi->dx = 0;
		pi->dy = 0;
		for (resno = 0; resno < comp->numresolutions; resno++) {
			int dx, dy;
			res = &comp->resolutions[resno];
			dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
			dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
			pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
			pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
		}
		if (!pi->tp_on){
			pi->poc.ty0 = pi->ty0;
			pi->poc.tx0 = pi->tx0;
			pi->poc.ty1 = pi->ty1;
			pi->poc.tx1 = pi->tx1;
		}
		for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
			for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
				for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
					int levelno;
					int trx0, try0;
					int trx1, try1;
					int rpx, rpy;
					int prci, prcj;
					res = &comp->resolutions[pi->resno];
					levelno = comp->numresolutions - 1 - pi->resno;
					trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
					try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
					trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
					try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
					rpx = res->pdx + levelno;
					rpy = res->pdy + levelno;
					if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){
						continue;	
					}
					if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
						continue; 
					}
					
					if ((res->pw==0)||(res->pw==0)) continue;
					
					if ((trx0==trx1)||(try0==try1)) continue;
					
					prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
						 - int_floordivpow2(trx0, res->pdx);
					prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
						 - int_floordivpow2(try0, res->pdy);
					pi->precno = prci + prcj * res->pw;
					for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
						index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
						if (!pi->include[index]) {
							pi->include[index] = 1;
							return true;
						}
LABEL_SKIP:;
					}
				}
			}
		}
	}
	
	return false;
}
Beispiel #17
0
opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
	int p, q;
	int compno, resno, pino;
	opj_pi_iterator_t *pi = NULL;
	opj_tcp_t *tcp = NULL;
	opj_tccp_t *tccp = NULL;
	size_t array_size;
	
	tcp = &cp->tcps[tileno];

	array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
	pi = (opj_pi_iterator_t *) opj_malloc(array_size);
	if(!pi) {
		/* TODO: throw an error */
		return NULL;
	}
	
	for (pino = 0; pino < tcp->numpocs + 1; pino++) {	/* change */
		int maxres = 0;
		int maxprec = 0;
		p = tileno % cp->tw;
		q = tileno / cp->tw;

		pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
		pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
		pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
		pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
		pi[pino].numcomps = image->numcomps;

		array_size = image->numcomps * sizeof(opj_pi_comp_t);
		pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
		if(!pi[pino].comps) {
			/* TODO: throw an error */
			pi_destroy(pi, cp, tileno);
			return NULL;
		}
		memset(pi[pino].comps, 0, array_size);
		
		for (compno = 0; compno < pi->numcomps; compno++) {
			int tcx0, tcy0, tcx1, tcy1;
			opj_pi_comp_t *comp = &pi[pino].comps[compno];
			tccp = &tcp->tccps[compno];
			comp->dx = image->comps[compno].dx;
			comp->dy = image->comps[compno].dy;
			comp->numresolutions = tccp->numresolutions;

			array_size = comp->numresolutions * sizeof(opj_pi_resolution_t);
			comp->resolutions =	(opj_pi_resolution_t *) opj_malloc(array_size);
			if(!comp->resolutions) {
				/* TODO: throw an error */
				pi_destroy(pi, cp, tileno);
				return NULL;
			}

			tcx0 = int_ceildiv(pi->tx0, comp->dx);
			tcy0 = int_ceildiv(pi->ty0, comp->dy);
			tcx1 = int_ceildiv(pi->tx1, comp->dx);
			tcy1 = int_ceildiv(pi->ty1, comp->dy);
			if (comp->numresolutions > maxres) {
				maxres = comp->numresolutions;
			}

			for (resno = 0; resno < comp->numresolutions; resno++) {
				int levelno;
				int rx0, ry0, rx1, ry1;
				int px0, py0, px1, py1;
				opj_pi_resolution_t *res = &comp->resolutions[resno];
				if (tccp->csty & J2K_CCP_CSTY_PRT) {
					res->pdx = tccp->prcw[resno];
					res->pdy = tccp->prch[resno];
				} else {
					res->pdx = 15;
					res->pdy = 15;
				}
				levelno = comp->numresolutions - 1 - resno;
				rx0 = int_ceildivpow2(tcx0, levelno);
				ry0 = int_ceildivpow2(tcy0, levelno);
				rx1 = int_ceildivpow2(tcx1, levelno);
				ry1 = int_ceildivpow2(tcy1, levelno);
				px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
				py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
				px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
				py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
				res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
				res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
				
				if (res->pw*res->ph > maxprec) {
					maxprec = res->pw*res->ph;
				}
				
			}
		}
		
		tccp = &tcp->tccps[0];
		pi[pino].step_p = 1;
		pi[pino].step_c = maxprec * pi[pino].step_p;
		pi[pino].step_r = image->numcomps * pi[pino].step_c;
		pi[pino].step_l = maxres * pi[pino].step_r;
		
		if (pino == 0) {
			array_size = image->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int);
			pi[pino].include = (short int *) opj_malloc(array_size);
			if(!pi[pino].include) {
				/* TODO: throw an error */
				pi_destroy(pi, cp, tileno);
				return NULL;
			}
		}
		else {
			pi[pino].include = pi[pino - 1].include;
		}
		
		if (tcp->POC == 0) {
			pi[pino].first = 1;
			pi[pino].poc.resno0 = 0;
			pi[pino].poc.compno0 = 0;
			pi[pino].poc.layno1 = tcp->numlayers;
			pi[pino].poc.resno1 = maxres;
			pi[pino].poc.compno1 = image->numcomps;
			pi[pino].poc.prg = tcp->prg;
		} else {
			pi[pino].first = 1;
			pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
			pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
			pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
			pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
			pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
			pi[pino].poc.prg = tcp->pocs[pino].prg;
		}
		pi[pino].poc.layno0  = 0;
		pi[pino].poc.precno0 = 0; 
		pi[pino].poc.precno1 = maxprec;
			
	}
	
	return pi;
}
Beispiel #18
0
static bool pi_next_cprl(opj_pi_iterator_t * pi) {
	opj_pi_comp_t *comp = NULL;
	opj_pi_resolution_t *res = NULL;
	long index = 0;

	if (!pi->first) {
		comp = &pi->comps[pi->compno];
		goto LABEL_SKIP;
	} else {
		pi->first = 0;
	}

	for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
		int resno;
		comp = &pi->comps[pi->compno];
		pi->dx = 0;
		pi->dy = 0;
		pi->dz = 0;
		for (resno = 0; resno < comp->numresolution[0]; resno++) {
			int dx, dy, dz;
			res = &comp->resolutions[resno];
			dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno));
			dy = comp->dy * (1 << (res->pdy + comp->numresolution[1] - 1 - resno));
			dz = comp->dz * (1 << (res->pdz + comp->numresolution[2] - 1 - resno));
			pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
			pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
			pi->dz = !pi->dz ? dz : int_min(pi->dz, dz);
		}
	for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) {
		for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
			for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
				for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolution[0]); pi->resno++) {
						int levelnox, levelnoy, levelnoz;
						int trx0, try0, trz0;
						int trx1, try1, trz1;
						int rpx, rpy, rpz;
						int prci, prcj, prck;
						comp = &pi->comps[pi->compno];
						if (pi->resno >= comp->numresolution[0]) {
							continue;
						}
						res = &comp->resolutions[pi->resno];
						levelnox = comp->numresolution[0] - 1 - pi->resno;
						levelnoy = comp->numresolution[1] - 1 - pi->resno;
						levelnoz = comp->numresolution[2] - 1 - pi->resno;
						trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox);
						try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy);
						trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz);
						trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox);
						try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy);
						trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz);
						rpx = res->pdx + levelnox;
						rpy = res->pdy + levelnoy;
						rpz = res->pdz + levelnoz;
						if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) {
							continue;
						}
						if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) {
							continue;
						}
						if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) {
							continue;
						}
						if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue;

						if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue;

						prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx)
							- int_floordivpow2(trx0, res->pdx);
						prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy)
							- int_floordivpow2(try0, res->pdy);
						prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz)
							- int_floordivpow2(trz0, res->pdz);
						pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1];
						for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
							index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
							if (!pi->include[index]) {
								pi->include[index] = 1;
								return true;
							}
	LABEL_SKIP:;
						}
					}
				}
			}
		}
	}

	return false;
}
Beispiel #19
0
void j2k_read_siz()
{
  int len, i;

  len = cio_read(2);		/* Lsiz                */
  cio_read(2);			/* Rsiz (capabilities) */
  j2k_img->x1 = cio_read(4);	/* Xsiz                */
  j2k_img->y1 = cio_read(4);	/* Ysiz                */
  j2k_img->x0 = cio_read(4);	/* X0siz               */
  j2k_img->y0 = cio_read(4);	/* Y0siz               */
  j2k_cp->tdx = cio_read(4);	/* XTsiz               */
  j2k_cp->tdy = cio_read(4);	/* YTsiz               */
  j2k_cp->tx0 = cio_read(4);	/* XT0siz              */
  j2k_cp->ty0 = cio_read(4);	/* YT0siz              */

  j2k_img->numcomps = cio_read(2);	/* Csiz                */
  j2k_img->comps =
    (j2k_comp_t *) malloc(j2k_img->numcomps * sizeof(j2k_comp_t));
  for (i = 0; i < j2k_img->numcomps; i++) {
    int tmp, w, h;
    tmp = cio_read(1);		/* Ssiz_i          */
    j2k_img->comps[i].prec = (tmp & 0x7f) + 1;
    j2k_img->comps[i].sgnd = tmp >> 7;
    j2k_img->comps[i].dx = cio_read(1);	/* XRsiz_i         */
    j2k_img->comps[i].dy = cio_read(1);	/* YRsiz_i         */
    w = int_ceildiv(j2k_img->x1 - j2k_img->x0, j2k_img->comps[i].dx);
    h = int_ceildiv(j2k_img->y1 - j2k_img->y0, j2k_img->comps[i].dy);
    j2k_img->comps[i].resno_decoded = 0;	/* number of resolution decoded */
    j2k_img->comps[i].factor = 0;	/* reducing factor by component */
  }

  j2k_cp->tw = int_ceildiv(j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx);
  j2k_cp->th = int_ceildiv(j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy);
  j2k_cp->tcps =
    (j2k_tcp_t *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(j2k_tcp_t));
  j2k_cp->tileno = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));
  j2k_cp->tileno_size = 0;

  for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) {
    j2k_cp->tcps[i].POC = 0;
    j2k_cp->tcps[i].numpocs = 0;
    j2k_cp->tcps[i].first = 1;
  }

  /* Initialization for PPM marker */
  j2k_cp->ppm = 0;
  j2k_cp->ppm_data = NULL;
  j2k_cp->ppm_previous = 0;
  j2k_cp->ppm_store = 0;

  j2k_default_tcp.tccps =
    (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);
  for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) {
    j2k_cp->tcps[i].tccps =
      (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);
  }
  j2k_tile_data =
    (unsigned char **) calloc(j2k_cp->tw * j2k_cp->th, sizeof(char *));
  j2k_tile_len = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));
  j2k_state = J2K_STATE_MH;


}
Beispiel #20
0
opj_pi_iterator_t *pi_create(opj_volume_t *volume, opj_cp_t *cp, int tileno) {
	int p, q, r;
	int compno, resno, pino;
	opj_pi_iterator_t *pi = NULL;
	opj_tcp_t *tcp = NULL;
	opj_tccp_t *tccp = NULL;
	size_t array_size;

	tcp = &cp->tcps[tileno];

	array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
	pi = (opj_pi_iterator_t *) opj_malloc(array_size);
	if(!pi) {
		fprintf(stdout,"[ERROR] Malloc of opj_pi_iterator failed \n");
		return NULL;
	}

	for (pino = 0; pino < tcp->numpocs + 1; pino++) {	/* change */
		int maxres = 0;
		int maxprec = 0;
		p = tileno % cp->tw;
		q = tileno / cp->tw;
		r = tileno / (cp->tw * cp->th);

		pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
		pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
		pi[pino].tz0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
		pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
		pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
		pi[pino].tz1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
		pi[pino].numcomps = volume->numcomps;

		array_size = volume->numcomps * sizeof(opj_pi_comp_t);
		pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
		if(!pi[pino].comps) {
			fprintf(stdout,"[ERROR] Malloc of opj_pi_comp failed \n");
			pi_destroy(pi, cp, tileno);
			return NULL;
		}
		memset(pi[pino].comps, 0, array_size);

		for (compno = 0; compno < pi->numcomps; compno++) {
			int tcx0, tcx1, tcy0, tcy1, tcz0, tcz1;
			int i;
			opj_pi_comp_t *comp = &pi[pino].comps[compno];
			tccp = &tcp->tccps[compno];

			comp->dx = volume->comps[compno].dx;
			comp->dy = volume->comps[compno].dy;
			comp->dz = volume->comps[compno].dz;
			for (i = 0; i < 3; i++) {
				comp->numresolution[i] = tccp->numresolution[i];
				if (comp->numresolution[i] > maxres) {
					maxres = comp->numresolution[i];
				}
			}
			array_size = comp->numresolution[0] * sizeof(opj_pi_resolution_t);
			comp->resolutions =	(opj_pi_resolution_t *) opj_malloc(array_size);
			if(!comp->resolutions) {
				fprintf(stdout,"[ERROR] Malloc of opj_pi_resolution failed \n");
				pi_destroy(pi, cp, tileno);
				return NULL;
			}

			tcx0 = int_ceildiv(pi->tx0, comp->dx);
			tcy0 = int_ceildiv(pi->ty0, comp->dy);
			tcz0 = int_ceildiv(pi->tz0, comp->dz);
			tcx1 = int_ceildiv(pi->tx1, comp->dx);
			tcy1 = int_ceildiv(pi->ty1, comp->dy);
			tcz1 = int_ceildiv(pi->tz1, comp->dz);

			for (resno = 0; resno < comp->numresolution[0]; resno++) {
				int levelnox, levelnoy, levelnoz, diff;
				int rx0, ry0, rz0, rx1, ry1, rz1;
				int px0, py0, pz0, px1, py1, pz1;
				opj_pi_resolution_t *res = &comp->resolutions[resno];
				if (tccp->csty & J3D_CCP_CSTY_PRT) {
					res->pdx = tccp->prctsiz[0][resno];
					res->pdy = tccp->prctsiz[1][resno];
					res->pdz = tccp->prctsiz[2][resno];
				} else {
					res->pdx = 15;
					res->pdy = 15;
					res->pdz = 15;
				}
				levelnox = comp->numresolution[0] - 1 - resno;
				levelnoy = comp->numresolution[1] - 1 - resno;
        levelnoz = comp->numresolution[2] - 1 - resno;
				if (levelnoz < 0) levelnoz = 0;
				diff = comp->numresolution[0] - comp->numresolution[2];

				rx0 = int_ceildivpow2(tcx0, levelnox);
				ry0 = int_ceildivpow2(tcy0, levelnoy);
				rz0 = int_ceildivpow2(tcz0, levelnoz);
				rx1 = int_ceildivpow2(tcx1, levelnox);
				ry1 = int_ceildivpow2(tcy1, levelnoy);
				rz1 = int_ceildivpow2(tcz1, levelnoz);
				px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
				py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
				pz0 = int_floordivpow2(rz0, res->pdz) << res->pdz;
				px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
				py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
				pz1 = int_ceildivpow2(rz1, res->pdz) << res->pdz;
				res->prctno[0] = (rx0==rx1)? 0 : ((px1 - px0) >> res->pdx);
				res->prctno[1] = (ry0==ry1)? 0 : ((py1 - py0) >> res->pdy);
				res->prctno[2] = (rz0==rz1)? 0 : ((pz1 - pz0) >> res->pdz);

				if (res->prctno[0]*res->prctno[1]*res->prctno[2] > maxprec) {
					maxprec = res->prctno[0]*res->prctno[1]*res->prctno[2];
				}
			}
		}

		tccp = &tcp->tccps[0];
		pi[pino].step_p = 1;
		pi[pino].step_c = maxprec * pi[pino].step_p;
		pi[pino].step_r = volume->numcomps * pi[pino].step_c;
		pi[pino].step_l = maxres * pi[pino].step_r;

		if (pino == 0) {
			array_size = volume->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int);
			pi[pino].include = (short int *) opj_malloc(array_size);
			if(!pi[pino].include) {
				fprintf(stdout,"[ERROR] Malloc of pi[pino].include failed \n");
				pi_destroy(pi, cp, tileno);
				return NULL;
			}
		}
		else {
			pi[pino].include = pi[pino - 1].include;
		}

		if (tcp->POC == 0) {
			pi[pino].first = 1;
			pi[pino].poc.resno0 = 0;
			pi[pino].poc.compno0 = 0;
			pi[pino].poc.layno1 = tcp->numlayers;
			pi[pino].poc.resno1 = maxres;
			pi[pino].poc.compno1 = volume->numcomps;
			pi[pino].poc.prg = tcp->prg;
		} else {
			pi[pino].first = 1;
			pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
			pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
			pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
			pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
			pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
			pi[pino].poc.prg = tcp->pocs[pino].prg;
		}
	}

	return pi;
}