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; }
bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) { if(cinfo && cio && image) { switch(cinfo->codec_format) { case CODEC_J2K: return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info); case CODEC_JP2: return jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info); case CODEC_JPT: case CODEC_UNKNOWN: default: break; } } return false; }
int jp2_write_jp2c(j2k_image_t * img, j2k_cp_t * cp, char *jp2_buffer, char *index) { int len; jp2_box_t box; box.init_pos = cio_tell(); cio_skip(4); cio_write(JP2_JP2C, 4); // JP2C len = j2k_encode(img, cp, jp2_buffer, cp->tdx * cp->tdy * cp->th * cp->tw * 2, index); box.length = cio_tell() - box.init_pos; cio_seek(box.init_pos); cio_write(box.length, 4); /* L */ cio_seek(box.init_pos + box.length); return box.length; }
bool CxImageJ2K::Encode(CxFile * hFile) { if (EncodeSafeCheck(hFile)) return false; if (head.biClrUsed!=0 && !IsGrayScale()){ strcpy(info.szLastError,"J2K can save only RGB or GrayScale images"); return false; } int i,x,y; j2k_image_t *img; j2k_cp_t *cp; j2k_tcp_t *tcp; j2k_tccp_t *tccp; img = (j2k_image_t *)calloc(sizeof(j2k_image_t),1); cp = (j2k_cp_t *)calloc(sizeof(j2k_cp_t),1); cp->tx0=0; cp->ty0=0; cp->tw=1; cp->th=1; cp->tcps=(j2k_tcp_t*)calloc(sizeof(j2k_tcp_t),1); tcp=&cp->tcps[0]; long w=head.biWidth; long h=head.biHeight; tcp->numlayers=1; for (i=0;i<tcp->numlayers;i++) tcp->rates[i]=(w*h*GetJpegQuality())/600; if (IsGrayScale()) { img->x0=0; img->y0=0; img->x1=w; img->y1=h; img->numcomps=1; img->comps=(j2k_comp_t*)calloc(sizeof(j2k_comp_t),1); img->comps[0].data=(int*)calloc(w*h*sizeof(int),1); img->comps[0].prec=8; img->comps[0].sgnd=0; img->comps[0].dx=1; img->comps[0].dy=1; for (i=0,y=0; y<h; y++) { for (x=0; x<w; x++,i++){ img->comps[0].data[i]=GetPixelIndex(x,h-1-y); } } } else if (!IsIndexed()) { img->x0=0; img->y0=0; img->x1=w; img->y1=h; img->numcomps=3; img->comps=(j2k_comp_t*)calloc(img->numcomps*sizeof(j2k_comp_t),1); for (i=0; i<img->numcomps; i++) { img->comps[i].data=(int*)calloc(w*h*sizeof(int),1); img->comps[i].prec=8; img->comps[i].sgnd=0; img->comps[i].dx=1; img->comps[i].dy=1; } RGBQUAD c; for (i=0,y=0; y<h; y++) { for (x=0; x<w; x++,i++){ c=GetPixelColor(x,h-1-y); img->comps[0].data[i]=c.rgbRed; img->comps[1].data[i]=c.rgbGreen; img->comps[2].data[i]=c.rgbBlue; } } } else { return 0; } cp->tdx=img->x1-img->x0; cp->tdy=img->y1-img->y0; tcp->csty=0; tcp->prg=0; tcp->mct=img->numcomps==3?1:0; tcp->tccps=(j2k_tccp_t*)calloc(img->numcomps*sizeof(j2k_tccp_t),1); int ir=0; /* or 1 ???*/ for (i=0; i<img->numcomps; i++) { tccp=&tcp->tccps[i]; tccp->csty=0; tccp->numresolutions=6; tccp->cblkw=6; tccp->cblkh=6; tccp->cblksty=0; tccp->qmfbid=ir?0:1; tccp->qntsty=ir?J2K_CCP_QNTSTY_SEQNT:J2K_CCP_QNTSTY_NOQNT; tccp->numgbits=2; tccp->roishift=0; j2k_calc_explicit_stepsizes(tccp, img->comps[i].prec); } BYTE* dest=(BYTE*)calloc(tcp->rates[tcp->numlayers-1]+2,1); long len = j2k_encode(img, cp, dest, tcp->rates[tcp->numlayers-1]+2); if (len==0) { strcpy(info.szLastError,"J2K failed to encode image"); } else { hFile->Write(dest, len, 1); } free(dest); j2k_destroy(&img,&cp); return (len!=0); }
int jp2_write_jp2c(j2k_image_t * img, j2k_cp_t * cp, char *jp2_buffer, char *index) { int len, i, set; char *cstr, *out; jp2_box_t box; box.init_pos = cio_tell(); cio_skip(4); cio_write(JP2_JP2C, 4); // JP2C if (jpwl_cp.JPWL_on){ set=cio_tell(); cstr = (char *) malloc( cp->tdx * cp->tdy * cp->tw * cp->th * 10*sizeof(char)); /* Allocate memory for all tiles */ cio_init(cstr, cp->tdx * cp->tdy * cp->tw * cp->th * 10); len = j2k_encode(img, cp, cstr, cp->tdx * cp->tdy * cp->tw * cp->th * 10, index); if (len == 0) { fprintf(stderr, "failed to encode image\n"); return 1; } out= (char *) malloc(len*6*sizeof(char)); len=jpwl_encode(cstr,out,len); if (len == 0) { fprintf(stderr, "failed to encode image\n"); return 1; } //ricopio la codestream, sono nel buffer out cio_seek(0); for(i=0;i<len;i++) jp2_buffer[set+i]=cio_read(1); //mi rimetto nel buffer jp2_buffer cio_init(jp2_buffer, cp->tdx * cp->tdy * cp->tw * cp->th * 10); //aggiorno la lunghezza della codestream len += set; cio_seek(len); //correggo l'index file e lo scrivo if (info_IM.index_on){ info_IM.Main_head_end += set; //printf("fine_mh: %d\n",info_IM.Main_head_end); //printf("%d\n", info_IM.tile[0].start_pos); info_IM.tile[0].start_pos += set; for (i=0; i < cp->th * cp->tw; i++){ info_IM.tile[i].end_header += set; info_IM.tile[i].end_pos += set; if (i < cp->tw * cp->th) info_IM.tile[i+1].start_pos += set; pack_corr(set, i); } } if (cp->index_on && use_index){ write_index(index, len); } free(cstr); free(out); } else{ len = j2k_encode(img, cp, jp2_buffer, cp->tdx * cp->tdy * cp->th * cp->tw * 2, index); } box.length = cio_tell() - box.init_pos; cio_seek(box.init_pos); cio_write(box.length, 4); /* L */ cio_seek(box.init_pos + box.length); return box.length; }