Alien-FreeImage

 view release on metacpan or  search on metacpan

src/Source/LibOpenJPEG/j2k.c  view on Meta::CPAN

                l_current_data+=2;

                l_remaining_size -= 6;
        }

        opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);

        *p_data_written = l_qcc_size;
}
#endif

OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)
{
        return opj_j2k_get_max_coc_size(p_j2k);
}

/**
 * Reads a QCC marker (Quantization component)
 * @param       p_header_data   the data contained in the QCC box.
 * @param       p_j2k                   the jpeg2000 codec.
 * @param       p_header_size   the size of the data contained in the QCC marker.
 * @param       p_manager               the user event manager.
*/
static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
                                    OPJ_BYTE * p_header_data,
                                    OPJ_UINT32 p_header_size,
                                    opj_event_mgr_t * p_manager
                                    )
{
        OPJ_UINT32 l_num_comp,l_comp_no;

        /* preconditions */
        assert(p_header_data != 00);
        assert(p_j2k != 00);
        assert(p_manager != 00);

        l_num_comp = p_j2k->m_private_image->numcomps;

        if (l_num_comp <= 256) {
                if (p_header_size < 1) {
                        opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
                        return OPJ_FALSE;
                }
                opj_read_bytes(p_header_data,&l_comp_no,1);
                ++p_header_data;
                --p_header_size;
        }
        else {
                if (p_header_size < 2) {
                        opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
                        return OPJ_FALSE;
                }
                opj_read_bytes(p_header_data,&l_comp_no,2);
                p_header_data+=2;
                p_header_size-=2;
        }

#ifdef USE_JPWL
        if (p_j2k->m_cp.correct) {

                static OPJ_UINT32 backup_compno = 0;

                /* compno is negative or larger than the number of components!!! */
                if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
                        opj_event_msg(p_manager, EVT_ERROR,
                                "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
                                l_comp_no, l_num_comp);
                        if (!JPWL_ASSUME) {
                                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
                                return OPJ_FALSE;
                        }
                        /* we try to correct */
                        l_comp_no = backup_compno % l_num_comp;
                        opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
                                "- setting component number to %d\n",
                                l_comp_no);
                }

                /* keep your private count of tiles */
                backup_compno++;
        };
#endif /* USE_JPWL */

        if (l_comp_no >= p_j2k->m_private_image->numcomps) {
                opj_event_msg(p_manager, EVT_ERROR,
                              "Invalid component number: %d, regarding the number of components %d\n",
                              l_comp_no, p_j2k->m_private_image->numcomps);
                return OPJ_FALSE;
        }

        if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
                opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
                return OPJ_FALSE;
        }

        if (p_header_size != 0) {
                opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
                return OPJ_FALSE;
        }

        return OPJ_TRUE;
}

OPJ_BOOL opj_j2k_write_poc(     opj_j2k_t *p_j2k,
                                                        opj_stream_private_t *p_stream,
                                                        opj_event_mgr_t * p_manager
                            )
{
        OPJ_UINT32 l_nb_comp;
        OPJ_UINT32 l_nb_poc;
        OPJ_UINT32 l_poc_size;
        OPJ_UINT32 l_written_size = 0;
        opj_tcp_t *l_tcp = 00;
        OPJ_UINT32 l_poc_room;

        /* preconditions */
        assert(p_j2k != 00);
        assert(p_manager != 00);
        assert(p_stream != 00);

        l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
        l_nb_comp = p_j2k->m_private_image->numcomps;
        l_nb_poc = 1 + l_tcp->numpocs;

        if (l_nb_comp <= 256) {
                l_poc_room = 1;
        }
        else {
                l_poc_room = 2;
        }
        l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;

        if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
                OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
                if (! new_header_tile_data) {
                        opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
                        p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
                        p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
                        opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
                        return OPJ_FALSE;

src/Source/LibOpenJPEG/j2k.c  view on Meta::CPAN

        opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1);                        /* TPsot */
        ++p_data;

        opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1);                      /* TNsot */
        ++p_data;

        /* UniPG>> */
#ifdef USE_JPWL
        /* update markers struct */
/*
        OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
*/
  assert( 0 && "TODO" );
#endif /* USE_JPWL */

        * p_data_written = 12;

        return OPJ_TRUE;
}

OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k,
                            OPJ_BYTE * p_header_data,
                            OPJ_UINT32 p_header_size,
                            opj_event_mgr_t * p_manager )
{
        opj_cp_t *l_cp = 00;
        opj_tcp_t *l_tcp = 00;
        OPJ_UINT32 l_tot_len, l_num_parts = 0;
        OPJ_UINT32 l_current_part;
        OPJ_UINT32 l_tile_x,l_tile_y;

        /* preconditions */
        assert(p_header_data != 00);
        assert(p_j2k != 00);
        assert(p_manager != 00);

        /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
        if (p_header_size != 8) {
                opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
                return OPJ_FALSE;
        }

        l_cp = &(p_j2k->m_cp);
        opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */
        p_header_data+=2;

        /* testcase 2.pdf.SIGFPE.706.1112 */
        if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) {
                opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", p_j2k->m_current_tile_number);
                return OPJ_FALSE;
        }

        l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
        l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
        l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;

#ifdef USE_JPWL
        if (l_cp->correct) {

                OPJ_UINT32 tileno = p_j2k->m_current_tile_number;
                static OPJ_UINT32 backup_tileno = 0;

                /* tileno is negative or larger than the number of tiles!!! */
                if (tileno > (l_cp->tw * l_cp->th)) {
                        opj_event_msg(p_manager, EVT_ERROR,
                                        "JPWL: bad tile number (%d out of a maximum of %d)\n",
                                        tileno, (l_cp->tw * l_cp->th));
                        if (!JPWL_ASSUME) {
                                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
                                return OPJ_FALSE;
                        }
                        /* we try to correct */
                        tileno = backup_tileno;
                        opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
                                        "- setting tile number to %d\n",
                                        tileno);
                }

                /* keep your private count of tiles */
                backup_tileno++;
        };
#endif /* USE_JPWL */

        /* look for the tile in the list of already processed tile (in parts). */
        /* Optimization possible here with a more complex data structure and with the removing of tiles */
        /* since the time taken by this function can only grow at the time */

        opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
        p_header_data+=4;

        /* PSot should be equal to zero or >=14 or <= 2^32-1 */
        if ((l_tot_len !=0 ) && (l_tot_len < 14) )
        {
            if (l_tot_len == 12 ) /* MSD: Special case for the PHR data which are read by kakadu*/
            {
                opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", l_tot_len);
            }
            else
            {
                opj_event_msg(p_manager, EVT_ERROR, "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len);
                return OPJ_FALSE;
            }
        }

#ifdef USE_JPWL
        if (l_cp->correct) {

                /* totlen is negative or larger than the bytes left!!! */
                if (/*(l_tot_len < 0) ||*/ (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
                        opj_event_msg(p_manager, EVT_ERROR,
                                        "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
                                        l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
                        if (!JPWL_ASSUME) {
                                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
                                return OPJ_FALSE;
                        }
                        /* we try to correct */
                        l_tot_len = 0;
                        opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
                                        "- setting Psot to %d => assuming it is the last tile\n",
                                        l_tot_len);
                }
                };
#endif /* USE_JPWL */

                /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
                if (!l_tot_len) {
                        opj_event_msg(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
                                        "we assuming it is the last tile-part of the codestream.\n");
                        p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
                }

                opj_read_bytes(p_header_data,&l_current_part ,1);       /* TPsot */
                ++p_header_data;

                opj_read_bytes(p_header_data,&l_num_parts ,1);          /* TNsot */
                ++p_header_data;

                if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
                        /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of



( run in 0.951 second using v1.01-cache-2.11-cpan-0bd6704ced7 )