示例#1
0
OPJ_BOOL opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
{
	/* the number of bytes written on the media. */
	OPJ_SIZE_T l_current_write_nb_bytes = 0;

	p_stream->m_current_data = p_stream->m_stored_data;

	while (p_stream->m_bytes_in_buffer) {
		/* we should do an actual write on the media */
		l_current_write_nb_bytes = p_stream->m_write_fn(p_stream->m_current_data,
														p_stream->m_bytes_in_buffer,
														p_stream->m_user_data);

		if (l_current_write_nb_bytes == (OPJ_SIZE_T)-1) {
			p_stream->m_status |= opj_stream_e_error;
			opj_event_msg(p_event_mgr, EVT_INFO, "Error on writing stream!\n");

			return OPJ_FALSE;
		}

		p_stream->m_current_data += l_current_write_nb_bytes;
		p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
	}

	p_stream->m_current_data = p_stream->m_stored_data;

	return OPJ_TRUE;
}
示例#2
0
opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
	if (index != NULL)
		opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
		"To extract the index, use the opj_encode_with_info() function.\n"
		"No index will be generated during this encoding\n");
	return opj_encode_with_info(cinfo, cio, image, NULL);
}
示例#3
0
/*
 * Read a byte.
 */
unsigned char cio_bytein(opj_cio_t *cio) {
	if (cio->bp >= cio->end) {
		opj_event_msg(cio->cinfo, EVT_ERROR, "read error: passed the end of the codestream (start = %d, current = %d, end = %d\n", cio->start, cio->bp, cio->end);
		return 0;
	}
	return *cio->bp++;
}
示例#4
0
static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
	unsigned int j2k_codestream_offset, j2k_codestream_length;
	opj_jp2_box_t box;

	opj_j2k_t *j2k = jp2->j2k;

	box.init_pos = cio_tell(cio);
	cio_skip(cio, 4);
	cio_write(cio, JP2_JP2C, 4);	/* JP2C */

	/* J2K encoding */
	j2k_codestream_offset = cio_tell(cio);
	if(!j2k_encode(j2k, cio, image, cstr_info)) {
		opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
		return 0;
	}
	j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;

	jp2->j2k_codestream_offset = j2k_codestream_offset;
	jp2->j2k_codestream_length = j2k_codestream_length;

	box.length = 8 + jp2->j2k_codestream_length;
	cio_seek(cio, box.init_pos);
	cio_write(cio, box.length, 4);	/* L */
	cio_seek(cio, box.init_pos + box.length);

	return box.length;
}
示例#5
0
文件: jp2.c 项目: Jshauk/sumatrapdf
static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
	opj_jp2_box_t box;

	opj_common_ptr cinfo = jp2->cinfo;

  if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
    opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
    return OPJ_FALSE;
  }
	do {
		if(JP2_JP2C != box.type) {
			/* cf. http://code.google.com/p/openjpeg/issues/detail?id=155 */
			if (box.length < 8) return OPJ_FALSE;
			cio_skip(cio, box.length - 8);
			if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
		}
	} while(JP2_JP2C != box.type);

	*j2k_codestream_offset = cio_tell(cio);
	/* cf. http://code.google.com/p/openjpeg/issues/detail?id=155 */
	if (box.length < 8) return OPJ_FALSE;
	*j2k_codestream_length = box.length - 8;

	return OPJ_TRUE;
}
/**
 * Writes the content of the stream buffer to the stream.
 * @param    p_stream  the stream to write data to.
 * @param    p_event_mgr  the user event manager to be notified of special events.
 * @return    the number of bytes written, or -1 if an error occured.
 */
bool opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
{
  // the number of bytes written on the media.
  // must be signed becuse "-1" is used to indicate an error in return values.
  OPJ_INT32 l_current_write_nb_bytes = 0;
  p_stream->m_current_data = p_stream->m_stored_data;

  while
    (p_stream->m_bytes_in_buffer)
  {
    // we should do an actual write on the media
    l_current_write_nb_bytes = p_stream->m_write_fn(p_stream->m_current_data,p_stream->m_bytes_in_buffer,p_stream->m_user_data);
    if
      (l_current_write_nb_bytes == -1)
    {
      p_stream->m_status |= opj_stream_e_error;
      opj_event_msg(p_event_mgr, EVT_INFO, "Error on writting stream!\n");
      return false;
    }
    p_stream->m_current_data += l_current_write_nb_bytes;
    p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
  }
  p_stream->m_current_data = p_stream->m_stored_data;
  return true;
}
示例#7
0
/*
 * Read a byte.
 */
unsigned char cio_bytein(opj_cio_t *cio) {
	if (cio->bp >= cio->end) {
		opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
		return 0;
	}
	return *cio->bp++;
}
示例#8
0
static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio,
                              unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset)
{
    opj_jp2_box_t box;

    opj_common_ptr cinfo = jp2->cinfo;

    if (jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE) {
        opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
        return OPJ_FALSE;
    }
    do {
        if (JP2_JP2C != box.type) {
            if (box.length <= 8) {
                return OPJ_FALSE;
            }
            cio_skip(cio, box.length - 8);
            if (jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE) {
                return OPJ_FALSE;
            }
        }
    } while (JP2_JP2C != box.type);

    *j2k_codestream_offset = cio_tell(cio);
    if (box.length <= 8) {
        return OPJ_FALSE;
    }
    *j2k_codestream_length = box.length - 8;

    return OPJ_TRUE;
}
示例#9
0
/*
 * Write a byte.
 */
bool cio_byteout(opj_cio_t *cio, unsigned char v) {
	if (cio->bp >= cio->end) {
		opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
		return false;
	}
	*cio->bp++ = v;
	return true;
}
示例#10
0
/*
 * Write a byte.
 */
opj_bool cio_byteout(opj_cio_t *cio, unsigned char v) {
	if (cio->bp >= cio->end) {
		opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
		return OPJ_FALSE;
	}
	*cio->bp++ = v;
	return OPJ_TRUE;
}
示例#11
0
/**
 * Skips a number of bytes from the stream.
 * @param		p_stream	the stream to skip data from.
 * @param		p_size		the number of bytes to skip.
 * @param		p_event_mgr	the user event manager to be notified of special events.
 * @return		the number of bytes skipped, or -1 if an error occured.
 */
OPJ_SIZE_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
{
	OPJ_SIZE_T l_skip_nb_bytes = 0;
	OPJ_SIZE_T l_current_skip_nb_bytes = 0;
	
	if
		(p_stream->m_bytes_in_buffer >= p_size)
	{
		p_stream->m_current_data += p_size;
		p_stream->m_bytes_in_buffer -= p_size;
		l_skip_nb_bytes += p_size;
		p_stream->m_byte_offset += l_skip_nb_bytes;
		return l_skip_nb_bytes;
	}

	// we are now in the case when the remaining data if not sufficient
	if
		(p_stream->m_status & opj_stream_e_end)
	{
		l_skip_nb_bytes += p_stream->m_bytes_in_buffer;
		p_stream->m_current_data += p_stream->m_bytes_in_buffer;
		p_stream->m_bytes_in_buffer = 0;
		p_stream->m_byte_offset += l_skip_nb_bytes;
		return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_SIZE_T) -1;
	}
	
	// the flag is not set, we copy data and then do an actual skip on the stream
	if
		(p_stream->m_bytes_in_buffer)
	{
		l_skip_nb_bytes += p_stream->m_bytes_in_buffer;
		p_stream->m_current_data = p_stream->m_stored_data;
		p_size -= p_stream->m_bytes_in_buffer;
		p_stream->m_bytes_in_buffer = 0;
	}
	
	while
		(p_size > 0)
	{
		// we should do an actual skip on the media
		l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
		if
			(l_current_skip_nb_bytes == (OPJ_SIZE_T) -1)
		{
			opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
			p_stream->m_status |= opj_stream_e_end;
			p_stream->m_byte_offset += l_skip_nb_bytes;
			// end if stream
			return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_SIZE_T) -1;
		}
		p_size -= l_current_skip_nb_bytes;
		l_skip_nb_bytes += l_current_skip_nb_bytes;
	}
	p_stream->m_byte_offset += l_skip_nb_bytes;
	return l_skip_nb_bytes;
}
示例#12
0
static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
	opj_jp2_box_t box;

	opj_common_ptr cinfo = jp2->cinfo;

	jp2_read_boxhdr(cinfo, cio, &box);
	if (JP2_JP != box.type) {
		opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
		return false;
	}
	if (0x0d0a870a != cio_read(cio, 4)) {
		opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
		return false;
	}
	if (cio_tell(cio) - box.init_pos != box.length) {
		opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
		return false;
	}

	return true;
}
示例#13
0
OPJ_OFF_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
{
	OPJ_OFF_T l_skip_nb_bytes = 0;
	OPJ_OFF_T l_current_skip_nb_bytes = 0;
	
	assert( p_size >= 0 );
	
	if (p_stream->m_bytes_in_buffer >= (OPJ_SIZE_T)p_size) {
		p_stream->m_current_data += p_size;
		/* it is safe to cast p_size to OPJ_SIZE_T since it is <= m_bytes_in_buffer
		which is of type OPJ_SIZE_T */
		p_stream->m_bytes_in_buffer -= (OPJ_SIZE_T)p_size;
		l_skip_nb_bytes += p_size;
		p_stream->m_byte_offset += l_skip_nb_bytes;
		return l_skip_nb_bytes;
	}

	/* we are now in the case when the remaining data if not sufficient */
	if (p_stream->m_status & opj_stream_e_end) {
		l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
		p_stream->m_current_data += p_stream->m_bytes_in_buffer;
		p_stream->m_bytes_in_buffer = 0;
		p_stream->m_byte_offset += l_skip_nb_bytes;
		return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
	}

	/* the flag is not set, we copy data and then do an actual skip on the stream */
	if (p_stream->m_bytes_in_buffer) {
		l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
		p_stream->m_current_data = p_stream->m_stored_data;
		p_size -= (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
		p_stream->m_bytes_in_buffer = 0;
	}

	while (p_size > 0) {
		/* we should do an actual skip on the media */
		l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
		if (l_current_skip_nb_bytes == (OPJ_OFF_T) -1) {
			opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");

			p_stream->m_status |= opj_stream_e_end;
			p_stream->m_byte_offset += l_skip_nb_bytes;
			/* end if stream */
			return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
		}
		p_size -= l_current_skip_nb_bytes;
		l_skip_nb_bytes += l_current_skip_nb_bytes;
	}

	p_stream->m_byte_offset += l_skip_nb_bytes;
	
	return l_skip_nb_bytes;
}
示例#14
0
/* skip PROFILE */
	skip_len = box->init_pos + box->length - cio_tell(cio);
	if (skip_len < 0) 
  {
	opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
	return OPJ_FALSE;
  }
	if(skip_len > 0)
  {
	unsigned char *start;

	start = cio_getbp(cio);
	color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
	color->icc_profile_len = skip_len;

	cio_skip(cio, box->init_pos + box->length - cio_tell(cio));

	memcpy(color->icc_profile_buf, start, skip_len);
  }
   }

	if (cio_tell(cio) - box->init_pos != box->length) 
   {
	opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
	return OPJ_FALSE;
   }
	color->jp2_has_colr = 1;

	return OPJ_TRUE;
}/* jp2_read_colr() */


//from openjpeg mailing list https://groups.google.com/forum/#!msg/openjpeg/7RZRPmzdE_M/eQGojBtOAawJ
#define JP2_XML 0x786D6C20 /*0x75756964*/
static opj_bool jp2_read_xml(opj_jp2_t *jp2, opj_cio_t *cio,
    opj_jp2_box_t *box,unsigned char ** xmp)
{
    int len;
    opj_common_ptr cinfo;

    cinfo = jp2->cinfo;

    len = box->init_pos + box->length - cio_tell(cio);
    if (len < 0)
   {
    opj_event_msg(cinfo, EVT_ERROR, "Error with XML box size\n");
    return OPJ_FALSE;
   }
    if(len > 0)
   {
    unsigned char *start;

    start = cio_getbp(cio);
    *xmp = (unsigned char*)opj_malloc(len+1);

    cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
    memcpy(*xmp, start, len);
	(*xmp)[len] = 0;
   }
    else
   {
    (*xmp) = (unsigned char*)opj_malloc(1);
    (*xmp)[0] = 0;
   }
    if (cio_tell(cio) - box->init_pos != box->length)
   {
    opj_event_msg(cinfo, EVT_ERROR, "Error with XML Box\n");
    opj_free(*xmp);
    return OPJ_FALSE;
   }
   return OPJ_TRUE;
}
示例#15
0
static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
	box->init_pos = cio_tell(cio);
	box->length = cio_read(cio, 4);
	box->type = cio_read(cio, 4);
	if (box->length == 1) {
		if (cio_read(cio, 4) != 0) {
			opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
			return OPJ_FALSE;
		}
		box->length = cio_read(cio, 4);
		if (box->length == 0) 
			box->length = cio_numbytesleft(cio) + 12;
	}
	else if (box->length == 0) {
		box->length = cio_numbytesleft(cio) + 8;
	}
	if (box->length < 0) {
		opj_event_msg(cinfo, EVT_ERROR, "Integer overflow in box->length\n");
		return OPJ_FALSE; /* TODO: actually check jp2_read_boxhdr's return value */
	}
	
	return OPJ_TRUE;
}
示例#16
0
static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
	unsigned int i;
	opj_jp2_box_t box;

	opj_common_ptr cinfo = jp2->cinfo;

	jp2_read_boxhdr(cinfo, cio, &box);
	if (JP2_BPCC != box.type) {
		opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
		return false;
	}

	for (i = 0; i < jp2->numcomps; i++) {
		jp2->comps[i].bpcc = cio_read(cio, 1);
	}

	if (cio_tell(cio) - box.init_pos != box.length) {
		opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
		return false;
	}

	return true;
}
示例#17
0
bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
	opj_jp2_box_t box;
	int skip_len;

	opj_common_ptr cinfo = jp2->cinfo;

	jp2_read_boxhdr(cinfo, cio, &box);
	do {
		if (JP2_JP2H != box.type) {
			if (box.type == JP2_JP2C) {
				opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
				return false;
			}
			cio_skip(cio, box.length - 8);
			jp2_read_boxhdr(cinfo, cio, &box);
		}
	} while(JP2_JP2H != box.type);

	if (!jp2_read_ihdr(jp2, cio))
		return false;

	if (jp2->bpc == 255) {
		if (!jp2_read_bpcc(jp2, cio))
			return false;
	}
	if (!jp2_read_colr(jp2, cio))
		return false;

	skip_len = box.init_pos + box.length - cio_tell(cio);
	if (skip_len < 0) {
		opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
		return false;
	}
	cio_skip(cio, box.init_pos + box.length - cio_tell(cio));

	return true;
}
示例#18
0
文件: jp2.c 项目: luciferlu/openjpeg
opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {

	int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
	pos_jp2c = pos_iptr = -1; /* remove a warning */

	/* JP2 encoding */

	/* JPEG 2000 Signature box */
	jp2_write_jp(cio);
	/* File Type box */
	jp2_write_ftyp(jp2, cio);
	/* JP2 Header box */
	jp2_write_jp2h(jp2, cio);

#if 0
	if( jp2->jpip_on){
	  pos_iptr = cio_tell( cio);
	  cio_skip( cio, 24); /* IPTR further ! */
	  
	  pos_jp2c = cio_tell( cio);
	}
#endif

	/* J2K encoding */
	if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
	    opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
	    return OPJ_FALSE;
	}

#if 0
	if( jp2->jpip_on){
	  pos_cidx = cio_tell( cio);
	  
	  len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
	  
	  pos_fidx = cio_tell( cio);
	  len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
	  
	  end_pos = cio_tell( cio);
	  
	  cio_seek( cio, pos_iptr);
	  write_iptr( pos_fidx, len_fidx, cio);
	  
	  cio_seek( cio, end_pos);
	}
#endif

	return OPJ_TRUE;
}
示例#19
0
static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
	opj_jp2_box_t box;
	int skip_len;

	opj_common_ptr cinfo = jp2->cinfo;

	jp2_read_boxhdr(cinfo, cio, &box);
	do {
		if (JP2_COLR != box.type) {
			cio_skip(cio, box.length - 8);
			jp2_read_boxhdr(cinfo, cio, &box);
		}
	} while(JP2_COLR != box.type);

	jp2->meth = cio_read(cio, 1);		/* METH */
	jp2->precedence = cio_read(cio, 1);	/* PRECEDENCE */
	jp2->approx = cio_read(cio, 1);		/* APPROX */

	if (jp2->meth == 1) {
		jp2->enumcs = cio_read(cio, 4);	/* EnumCS */
	} else {
		/* skip PROFILE */
		skip_len = box.init_pos + box.length - cio_tell(cio);
		if (skip_len < 0) {
			opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
			return false;
		}
		cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
	}

	if (cio_tell(cio) - box.init_pos != box.length) {
		opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
		return false;
	}
	return true;
}
示例#20
0
opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
	opj_common_ptr cinfo;
	opj_image_t *image = NULL;

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

	cinfo = jp2->cinfo;

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

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

	return image;
}
示例#21
0
static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
	opj_jp2_box_t box;

	opj_common_ptr cinfo = jp2->cinfo;

  if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
    opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
    return OPJ_FALSE;
  }
	if (JP2_JP != box.type) {
		opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
		return OPJ_FALSE;
	}
	if (0x0d0a870a != cio_read(cio, 4)) {
		opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
		return OPJ_FALSE;
	}
	if (cio_tell(cio) - box.init_pos != box.length) {
		opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
		return OPJ_FALSE;
	}

	return OPJ_TRUE;
}
示例#22
0
文件: jp2.c 项目: Jshauk/sumatrapdf
static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
	box->init_pos = cio_tell(cio);
	box->length = cio_read(cio, 4);
	box->type = cio_read(cio, 4);
	if (box->length == 1) {
		if (cio_read(cio, 4) != 0) {
			opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
			return OPJ_FALSE;
		}
		box->length = cio_read(cio, 4);
		if (box->length == 0) 
			box->length = cio_numbytesleft(cio) + 12;
	}
	else if (box->length == 0) {
		box->length = cio_numbytesleft(cio) + 8;
	}
	/* cf. http://code.google.com/p/openjpeg/issues/detail?id=155 */
	if (box->length < 0) {
		opj_event_msg(cinfo, EVT_ERROR, "Integer overflow in box->length\n");
		return OPJ_FALSE; // TODO: actually check jp2_read_boxhdr's return value
	}
	
	return OPJ_TRUE;
}
示例#23
0
int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
	unsigned char *c = src;
	opj_pi_iterator_t *pi;
	int pino, e = 0;
	int n = 0,i;

	opj_volume_t *volume = t2->volume;
	opj_cp_t *cp = t2->cp;
	
	/* create a packet iterator */
	pi = pi_create(volume, cp, tileno);
	if(!pi) {
		/* TODO: throw an error */
		return -999;
	}
	
	for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
		while (pi_next(&pi[pino])) {
			if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
				e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
			} else {
				e = 0;
			}
			
			/* progression in resolution */
			for (i = 0; i < 3; i++){
                volume->comps[pi[pino].compno].resno_decoded[i] = (e > 0) ? int_max(pi[pino].resno, volume->comps[pi[pino].compno].resno_decoded[i]) : volume->comps[pi[pino].compno].resno_decoded[i];
			}
			n++;
			
			if (e == -999) {		/* ADD */
				break;
			} else {
				opj_event_msg(t2->cinfo, EVT_INFO, "  t2_decode_packet: %d bytes decoded\n",e);
				c += e;
			}
		}
	}

	/* don't forget to release pi */
	pi_destroy(pi, cp, tileno);
	
	if (e == -999) {
		return e;
	}
	
    return (c - src);
}
示例#24
0
opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
	opj_cp_t *cp = NULL;
	opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
	if(!cio) return NULL;
	cio->cinfo = cinfo;
	if(buffer && length) {
		/* wrap a user buffer containing the encoded image */
		cio->openmode = OPJ_STREAM_READ;
		cio->buffer = buffer;
		cio->length = length;
	}
	else if(!buffer && !length && cinfo) {
		/* allocate a buffer for the encoded image */
		cio->openmode = OPJ_STREAM_WRITE;
		switch(cinfo->codec_format) {
			case CODEC_J2K:
				cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
				break;
			case CODEC_JP2:
				cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
				break;
			default:
				opj_free(cio);
				return NULL;
		}
		cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
		cio->buffer = (unsigned char *)opj_malloc(cio->length);
		if(!cio->buffer) {
			opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
			opj_free(cio);
			return NULL;
		}
	}
	else {
		opj_free(cio);
		return NULL;
	}

	/* Initialize byte IO */
	cio->start = cio->buffer;
	cio->end = cio->buffer + cio->length;
	cio->bp = cio->buffer;

	return cio;
}
示例#25
0
/**
 * Skips a number of bytes from the stream.
 * @param		p_stream	the stream to skip data from.
 * @param		p_size		the number of bytes to skip.
 * @param		p_event_mgr	the user event manager to be notified of special events.
 * @return		the number of bytes skipped, or -1 if an error occured.
 */
OPJ_SIZE_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
{
	OPJ_BOOL l_is_written = 0;
	OPJ_SIZE_T l_current_skip_nb_bytes = 0;
	OPJ_SIZE_T l_skip_nb_bytes = 0;

	if
		(p_stream->m_status & opj_stream_e_error)
	{
		return (OPJ_SIZE_T) -1;
	}
	
	// we should flush data
	l_is_written = opj_stream_flush (p_stream, p_event_mgr);
	if
		(! l_is_written)
	{
		p_stream->m_status |= opj_stream_e_error;
		p_stream->m_bytes_in_buffer = 0;
		p_stream->m_current_data = p_stream->m_current_data;
		return (OPJ_SIZE_T) -1;
	}
	// then skip

	while
		(p_size > 0)
	{
		// we should do an actual skip on the media
		l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
		if
			(l_current_skip_nb_bytes == (OPJ_SIZE_T)-1)
		{
			opj_event_msg(p_event_mgr, EVT_INFO, "Stream error!\n");
			p_stream->m_status |= opj_stream_e_error;
			p_stream->m_byte_offset += l_skip_nb_bytes;
			// end if stream
			return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_SIZE_T)-1;
		}
		p_size -= l_current_skip_nb_bytes;
		l_skip_nb_bytes += l_current_skip_nb_bytes;
	}
	p_stream->m_byte_offset += l_skip_nb_bytes;
	return l_skip_nb_bytes;
}
示例#26
0
OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
                                        opj_dparameters_t *parameters 
										)
{
	if (p_codec && parameters) { 
		opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;

		if (! l_codec->is_decompressor) {
			opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, 
                "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
			return OPJ_FALSE;
		}

		l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
																parameters);
		return OPJ_TRUE;
	}
	return OPJ_FALSE;
}
示例#27
0
static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
	box->init_pos = cio_tell(cio);
	box->length = cio_read(cio, 4);
	box->type = cio_read(cio, 4);
	if (box->length == 1) {
		if (cio_read(cio, 4) != 0) {
			opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
			return false;
		}
		box->length = cio_read(cio, 4);
		if (box->length == 0) 
			box->length = cio_numbytesleft(cio) + 12;
	}
	else if (box->length == 0) {
		box->length = cio_numbytesleft(cio) + 8;
	}
	
	return true;
}
示例#28
0
bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {

	/* JP2 encoding */

	/* JPEG 2000 Signature box */
	jp2_write_jp(cio);
	/* File Type box */
	jp2_write_ftyp(jp2, cio);
	/* JP2 Header box */
	jp2_write_jp2h(jp2, cio);

	/* J2K encoding */

	if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) {
		opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
		return false;
	}

	return true;
}
示例#29
0
opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {

	int pos_iptr, pos_jp2c, len_jp2c;
	pos_jp2c = pos_iptr = -1; /* remove a warning */

	/* JP2 encoding */

	/* JPEG 2000 Signature box */
	jp2_write_jp(cio);
	/* File Type box */
	jp2_write_ftyp(jp2, cio);
	/* JP2 Header box */
	jp2_write_jp2h(jp2, cio);

	/* J2K encoding */
	if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
	    opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
	    return OPJ_FALSE;
	}

	return OPJ_TRUE;
}
示例#30
0
OPJ_BOOL OPJ_CALLCONV opj_read_header (	opj_stream_t *p_stream,
										opj_codec_t *p_codec,
										opj_image_t **p_image )
{
	if (p_codec && p_stream) {
		opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
		opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;

		if(! l_codec->is_decompressor) {
			opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, 
                "Codec provided to the opj_read_header function is not a decompressor handler.\n");
			return OPJ_FALSE;
		}

		return l_codec->m_codec_data.m_decompression.opj_read_header(	l_stream,
																		l_codec->m_codec,
																		p_image,
																		&(l_codec->m_event_mgr) );
	}

	return OPJ_FALSE;
}