void ocrword_free(OCRWORD *word) { static char *funcname="ocrword_free"; willus_mem_free((double **)&word->cpos,funcname); willus_mem_free((double **)&word->text,funcname); }
void strbuf_dsprintf_no_space(STRBUF *sbuf,STRBUF *sbuf2,char *fmt,...) { static char *funcname="strbuf_sprintf"; if (sbuf!=NULL || sbuf2!=NULL) { va_list args; char *buf; willus_mem_alloc_warn((void **)&buf,1024,funcname,10); va_start(args,fmt); vsprintf(buf,fmt,args); va_end(args); if (buf[0]!='\0') { if (sbuf!=NULL && sbuf2==NULL) { strbuf_ensure(sbuf,(sbuf->s==NULL?0:strlen(sbuf->s))+strlen(buf)+2); strcat(sbuf->s,buf); } if (sbuf2!=NULL) { strbuf_ensure(sbuf2,(sbuf2->s==NULL?0:strlen(sbuf2->s))+strlen(buf)+2); strcat(sbuf2->s,buf); } } willus_mem_free((double **)&buf,funcname); } }
void strbuf_free(STRBUF *sbuf) { willus_mem_free((double**)&sbuf->s,"strbuf_free"); sbuf->s=NULL; sbuf->na=0; }
void wrectmaps_free(WRECTMAPS *wrectmaps) { static char *funcname="wrectmaps_free"; willus_mem_free((double **)&wrectmaps->wrectmap,funcname); wrectmaps->na=wrectmaps->n=0; }
static void str0_free(char **list,int *na) { static char *funcname="str0_free"; willus_mem_free((double **)list,funcname); (*na)=0; }
void k2pdfopt_files_free(K2PDFOPT_FILES *k2files) { static char *funcname="k2pdfopt_files_free"; k2pdfopt_files_clear(k2files); willus_mem_free((double **)&k2files->file,funcname); k2files->na=0; }
void pageregions_free(PAGEREGIONS *regions) { static char *funcname="pageregions_free"; int i; for (i=regions->n-1;i>=0;i--) pageregion_free(®ions->pageregion[i]); willus_mem_free((double **)®ions->pageregion,funcname); }
void bmp8_win32_palette_free(WILLUSBITMAP *bmp) { if (bmp8_palette!=NULL) { DeleteObject(bmp8_palette); bmp8_palette=NULL; } willus_mem_free((double **)&bmp8_pal_data,"bmp8_win32_palette_free"); }
void k2pdfopt_files_clear(K2PDFOPT_FILES *k2files) { static char *funcname="k2pdfopt_files_clear"; int i; if (k2files->file!=NULL) for (i=k2files->n-1;i>=0;i--) willus_mem_free((double **)&k2files->file[i],funcname); k2files->n=0; }
void ocrwords_free(OCRWORDS *words) { int i; static char *funcname="ocrwords_free"; for (i=words->na-1;i>=0;i--) ocrword_free(&words->word[i]); willus_mem_free((double **)&words->word,funcname); words->n=0; words->na=0; }
/* ** Call with NULL filehandle in case of error etc., to clean up zlib state ** and allocations. */ void compress_done(FILE *f,compress_handle *hh) { static char *funcname="compress_done"; compress_handle_p h = (compress_handle_p)(*hh); if (h) { if (f) compress_out(f,h,Z_FINISH); deflateEnd(&h->strm); /* Fix memory leak, 2-2-14 */ willus_mem_free((double **)hh,funcname); } }
void k2pdfopt_files_remove_file(K2PDFOPT_FILES *k2files,char *filename) { static char *funcname="k2pdfopt_files_remove_file"; int i; for (i=0;i<k2files->n;i++) if (!strcmp(k2files->file[i],filename)) break; if (i>=k2files->n) return; willus_mem_free((double **)&k2files->file[i],funcname); for (i++;i<k2files->n;i++) k2files->file[i-1]=k2files->file[i]; k2files->n--; }
/* ** Truncate word to chars from text[i1..i2] */ void ocrword_truncate(OCRWORD *word,int i1,int i2) { int i,n; int *u; double w0,scale; static char *funcname="ocrword_truncate"; n=word->n; if (i1<0) i1=0; if (i2>n-1) i2=n-1; if (i2<i1) { word->n=0; word->w0=0.; word->w=0; return; } w0=word->w0; /* Fix text */ willus_mem_alloc_warn((void **)&u,n*sizeof(int),funcname,10); utf8_to_unicode(u,word->text,n); unicode_to_utf8(word->text,&u[i1],i2-i1+1); willus_mem_free((double **)&u,funcname); /* Fix length */ word->n=i2-i1+1; if (word->cpos==NULL) return; /* Fix width */ word->w0 = word->cpos[i2]-(i1==0?0.:word->cpos[i1-1]); scale = word->w/w0; word->w = (int)(scale*word->w0+.5); /* Fix left position and subsequent positions */ if (i1>0) { double x0; x0=word->cpos[i1-1]; word->x0 += x0; word->c += (int)(x0*scale+.5); for (i=i1;i<=i2;i++) word->cpos[i-i1]=word->cpos[i]-x0; } }
int k2gui_cbox_vprintf(FILE *f,char *fmt,va_list args) { static char *funcname="k2gui_cbox_vprintf"; char prbuf[1024]; char *buf; int i,j,status,nlines; if (k2gui_cbox==NULL || k2gui_cbox->control[0].handle==NULL || k2gui_cbox->ncontrols<1) return(-1); status=vsprintf(prbuf,fmt,args); willus_mem_alloc_warn((void **)&buf,strlen(prbuf)*2,funcname,10); for (i=j=0;prbuf[i]!='\0';i++) { /* Insert CRs before LFs */ if (prbuf[i]=='\r' && prbuf[i+1]=='\n') { buf[j++]=prbuf[i++]; buf[j++]=prbuf[i]; continue; } if (prbuf[i]=='\n') { buf[j++]='\r'; buf[j++]=prbuf[i]; continue; } /* Remove any ANSI color code */ if (prbuf[i]=='\x1b' && prbuf[i+1]=='[') { for (i+=2;prbuf[i]!='\0' && (tolower(prbuf[i])<'a' || tolower(prbuf[i])>'z');i++); if (prbuf[i]=='\0') break; continue; } buf[j++]=prbuf[i]; } buf[j]='\0'; strbuf_sprintf(&k2gui_cbox->buf,"%s",buf); nlines=willusgui_control_nlines(&k2gui_cbox->control[0]); willusgui_control_set_text(&k2gui_cbox->control[0],strbuf_lineno(&k2gui_cbox->buf,-nlines)); willusgui_control_scroll_to_bottom(&k2gui_cbox->control[0]); willus_mem_free((double **)&buf,funcname); return(status); }
void strbuf_sprintf(STRBUF *sbuf,char *fmt,...) { static char *funcname="strbuf_sprintf"; if (sbuf!=NULL) { va_list args; char *buf; willus_mem_alloc_warn((void **)&buf,1024,funcname,10); va_start(args,fmt); vsprintf(buf,fmt,args); va_end(args); strbuf_cat(sbuf,buf); willus_mem_free((double **)&buf,funcname); } }
double string_atof(char *s) { #ifdef WIN32 return(atof(s)); #else char *p; int i; double x; static char *funcname="string_atof"; willus_mem_alloc_warn((void **)&p,strlen(s)+1,funcname,10); if (p==NULL) return(atof(s)); strcpy(p,s); for (i=0; p[i]!='\0'; i++) if (tolower(p[i])=='d') p[i]=p[i]+1; x=atof(p); willus_mem_free((double **)&p,funcname); return(x); #endif }
static int wmupdf_pdfdoc_newpages(pdf_document *xref,fz_context *ctx,WPDFPAGEINFO *pageinfo, int use_forms,FILE *out) { static char *funcname="wmupdf_pdfdoc_newpages"; pdf_obj *root,*oldroot,*pages,*kids,*countobj,*parent,*olddests; pdf_obj *srcpageobj,*srcpagecontents; pdf_obj *destpageobj,*destpagecontents,*destpageresources; double srcx0,srcy0; int qref,i,i0,pagecount,srccount,destpageref,nbb; int *srcpageused; char *bigbuf; double srcpagerot; /* Avoid compiler warning */ destpageref = 0; destpageobj = NULL; srcx0=srcy0=0.; /* Keep only pages/type and (reduced) dest entries to avoid references to unretained pages */ pagecount = pdf_count_pages(xref); if (use_forms) { willus_mem_alloc_warn((void **)&srcpageused,sizeof(int)*(pagecount+1),funcname,10); /* Mark all source pages as "not done" */ for (i=0;i<=pagecount;i++) srcpageused[i]=0; nbb=4096; willus_mem_alloc_warn((void **)&bigbuf,nbb,funcname,10); bigbuf[0]='\0'; } oldroot = pdf_dict_gets(xref->trailer,"Root"); /* ** pages points to /Pages object in PDF file. ** Has: /Type /Pages, /Count <numpages>, /Kids [ obj obj obj obj ] */ pages = pdf_dict_gets(oldroot,"Pages"); olddests = pdf_load_name_tree(xref,"Dests"); /* ** Create new root object with only /Pages and /Type (and reduced dest entries) ** to avoid references to unretained pages. */ root = pdf_new_dict(ctx,4); pdf_dict_puts(root,"Type",pdf_dict_gets(oldroot,"Type")); pdf_dict_puts(root,"Pages",pages); pdf_update_object(xref,pdf_to_num(oldroot),root); pdf_drop_obj(root); /* Parent indirectly references the /Pages object in the file */ /* (Each new page we create has to point to this.) */ parent = pdf_new_indirect(ctx, pdf_to_num(pages), pdf_to_gen(pages), xref); /* Create a new kids array with only the pages we want to keep */ kids = pdf_new_array(ctx, 1); qref=0; /* Avoid compiler warnings */ destpageresources=NULL; destpagecontents=NULL; srcpagecontents=NULL; srcpagerot=0.; for (i=0;i<=pageinfo->boxes.n;i++) if (pageinfo->boxes.box[i].dstpage>0) break; if (i>0) { if (i<pageinfo->boxes.n) memmove(&pageinfo->boxes.box[0],&pageinfo->boxes.box[i],sizeof(WPDFBOX)*pageinfo->boxes.n-i); pageinfo->boxes.n -= i; } /* Walk through PFDBOXES array */ for (i=srccount=i0=0;i<=pageinfo->boxes.n;i++) { WPDFBOX *box; int j,k,newsrc; static char buf[512]; pdf_obj *s1indirect,*qindirect,*rotobj; static double cpm[3][3],m[3][3],m1[3][3]; static double xclip[4],yclip[4]; /* printf("box[%d]\n",i); if (i<pageinfo->boxes.n) { box=&pageinfo->boxes.box[i]; printf(" srcpage=%d, dstpage=%d\n",box->srcbox.pageno,box->dstpage); printf(" x0=%g, y0=%g\n",box->x0,box->y0); printf(" w=%g, h=%g\n",box->w,box->h); printf(" x1=%g, y1=%g\n",box->x1,box->y1); printf(" sr=%g, dr=%g\n",box->srcrot_deg,box->dstrot_deg); printf(" scale=%g\n",box->scale); } */ /* Check to see if we are done with an output page */ if (srccount>0 && (i==pageinfo->boxes.n || (i>0 && pageinfo->boxes.box[i].dstpage!=pageinfo->boxes.box[i-1].dstpage))) { pdf_obj *newpageref; /* ** Store destination page into document structure */ /* printf(" ADDING NEW PAGE. (srccount=%d)\n",srccount); */ if (use_forms) { pdf_obj *dest_stream; /* Create new object in document for destination page stream */ dest_stream = pdf_new_indirect(ctx,new_stream_object(xref,ctx,bigbuf), 0,(void *)xref); /* Store this into the destination page contents array */ pdf_array_push(destpagecontents,dest_stream); pdf_drop_obj(dest_stream); } newpageref=pdf_new_indirect(ctx,destpageref,0,(void *)xref); /* Reference parent list of pages */ pdf_dict_puts(destpageobj,"Parent",parent); pdf_dict_puts(destpageobj,"Contents",destpagecontents); pdf_dict_puts(destpageobj,"Resources",destpageresources); /* Store page object in document's kids array */ pdf_array_push(kids,newpageref); /* Update document with new page */ pdf_update_object(xref,destpageref,destpageobj); /* Clean up */ pdf_drop_obj(newpageref); pdf_drop_obj(destpageresources); pdf_drop_obj(destpagecontents); pdf_drop_obj(destpageobj); /* Reset source page and index to start of new destination page */ i0=i; srccount=0; } /* Quit loop if beyond last box */ if (i>=pageinfo->boxes.n) break; box=&pageinfo->boxes.box[i]; if (box->srcbox.pageno<1 || box->srcbox.pageno>pagecount) continue; /* Is this a source page we haven't processed yet (for this destination page)? */ for (newsrc=1,j=i0;j<i;j++) { if (pageinfo->boxes.box[j].srcbox.pageno==box->srcbox.pageno) { newsrc=0; break; } } if (newsrc) { double v[4]; srccount++; if (use_forms) srcpageused[box->srcbox.pageno]=1; /* printf(" NEW SOURCE PAGE (srccount=%d)\n",srccount); */ if (srccount==1) { /* ** Start a new destination page. ** ** Each new page object is a dict type with: ** /Type /Page ** /Contents (array of objects) ** /Resources (dict) ** /MediaBox [0 0 612 792] ** /Parent <PagesObj> ** [Can have /Rotate 90, for example.] ** */ /* printf(" (STARTING NEW DEST. PAGE)\n"); */ destpageobj=start_new_destpage(ctx,box->dst_width_pts,box->dst_height_pts); destpageresources=pdf_new_dict(ctx,1); if (use_forms) pdf_dict_puts(destpageresources,"XObject",pdf_new_dict(ctx,1)); destpageref=pdf_create_object(xref); destpagecontents=pdf_new_array(ctx,1); /* Init the destination page stream for forms */ if (use_forms) bigbuf[0]='\0'; } /* New source page, so get the source page objects */ srcpageobj = xref->page_objs[box->srcbox.pageno-1]; wmupdf_page_bbox(srcpageobj,v); srcx0=v[0]; srcy0=v[1]; /* printf("SRCX0=%g, SRCY0=%g\n",srcx0,srcy0); */ rotobj=pdf_dict_gets(srcpageobj,"Rotate"); srcpagerot = rotobj!=NULL ? pdf_to_real(rotobj) : 0.; /* printf("Page rotation = %g\n",srcpagerot); */ srcpagecontents=pdf_dict_gets(srcpageobj,"Contents"); /* if (pdf_is_array(srcpagecontents)) { int k; printf(" source page contents = array.\n"); for (k=0;k<pdf_array_len(srcpagecontents);k++) { pdf_obj *obj; obj=pdf_array_get(srcpagecontents,k); if (pdf_is_indirect(obj)) { printf(" contents[%d] = indirect (%d)\n",k,pdf_to_num(obj)); pdf_resolve_indirect(obj); } } } */ if (use_forms) { pdf_obj *xobjdict; int pageno; xobjdict=pdf_dict_gets(destpageresources,"XObject"); pageno=box->srcbox.pageno; pdf_dict_puts(xobjdict,xobject_name(pageno),xref->page_refs[pageno-1]); pdf_dict_puts(destpageresources,"XObject",xobjdict); } else { pdf_obj *srcpageresources; /* Merge source page resources into destination page resources */ srcpageresources=pdf_dict_gets(srcpageobj,"Resources"); /* printf("box->dstpage=%d, srcpage=%d (ind.#=%d)\n",box->dstpage,box->srcbox.pageno,pdf_to_num(xref->page_refs[box->srcbox.pageno-1])); */ wmupdf_dict_merge(ctx,"Resources",destpageresources,srcpageresources); } } /* ** Process this source box: ** ** Create a tranformation matrix and clipping path to only show the ** desired part of the source page at the appropriate place on the ** destination page. ** ** How the tranformation matrix works: ** - Translations shall be specified as [ 1 0 0 1 tx ty ], where tx and ty ** shall be the distances to translate the origin of the coordinate system ** in the horizontal and vertical dimensions, respectively. ** ** - Scaling shall be obtained by [ sx 0 0 sy 0 0 ]. This scales the coordinates ** so that 1 unit in the horizontal and vertical dimensions of the new coordinate ** system is the same size as sx and sy units, respectively, in the previous ** coordinate system. ** ** - Rotations shall be produced by [ cos q sin q -sin q cos q 0 0 ], which has the ** effect of rotating the coordinate system axes by an angle q counter-clockwise. ** ** - Skew shall be specified by [ 1 tan a tan b 1 0 0 ], which skews the x axis by ** an angle a and the y axis by an angle b. ** */ wpdfbox_determine_original_source_position(box); /* printf("Before unrotate.\n"); printf("box->srcrot=%g\n",box->srcrot_deg); printf("box->x0=%g, box->y0=%g\n",box->x0,box->y0); printf("box->w=%g, box->h=%g\n",box->w,box->h); printf("box->pw=%g, box->ph=%g\n",box->src_width_pts,box->src_height_pts); */ if (fabs(srcpagerot) > 1.0e-4) wpdfbox_unrotate(box,srcpagerot); /* printf("box->srcrot=%g\n",box->srcrot_deg); printf("box->x0=%g, box->y0=%g\n",box->x0,box->y0); printf("box->w=%g, box->h=%g\n",box->w,box->h); printf("box->pw=%g, box->ph=%g\n",box->src_width_pts,box->src_height_pts); */ matrix_unity(m,1.); /* printf("xfmatrix = [ %9.6f %9.6f %9.6f ]\n" " [ %9.6f %9.6f %9.6f ]\n" " [ %9.6f %9.6f %9.6f ]\n", m[0][0],m[0][1],m[0][2], m[1][0],m[1][1],m[1][2], m[2][0],m[2][1],m[2][2]); */ matrix_translate(m1,-box->x0-srcx0,-box->y0-srcy0); matrix_mul(m,m1); matrix_rotate(m1,-box->srcrot_deg+box->dstrot_deg); matrix_mul(m,m1); matrix_unity(m1,box->scale); matrix_mul(m,m1); matrix_translate(m1,box->x1,box->y1); matrix_mul(m,m1); matrix_zero_round(m); matrix_rotate(cpm,box->srcrot_deg); matrix_translate(m1,box->x0+srcx0,box->y0+srcy0); matrix_mul(cpm,m1); /* printf("Clip matrix:\n"); printf("xfmatrix = [ %9.6f %9.6f %9.6f ]\n" " [ %9.6f %9.6f %9.6f ]\n" " [ %9.6f %9.6f %9.6f ]\n", cpm[0][0],cpm[0][1],cpm[0][2], cpm[1][0],cpm[1][1],cpm[1][2], cpm[2][0],cpm[2][1],cpm[2][2]); */ set_clip_array(xclip,yclip,box->srcrot_deg,box->w,box->h); for (k=0;k<4;k++) matrix_xymul(cpm,&xclip[k],&yclip[k]); /* printf("Clip path:\n %7.2f %7.2f\n %7.2f,%7.2f\n %7.2f,%7.2f\n" " %7.2f %7.2f\n %7.2f,%7.2f\n", xclip[0],yclip[0],xclip[1],yclip[1],xclip[2],yclip[2], xclip[3],yclip[3],xclip[0],yclip[0]); */ strcpy(buf,"q"); for (k=0;k<=2;k++) { cat_pdf_double(buf,m[k][0]); cat_pdf_double(buf,m[k][1]); } strcat(buf," cm"); for (k=0;k<=4;k++) { cat_pdf_double(buf,xclip[k&3]); cat_pdf_double(buf,yclip[k&3]); strcat(buf,k==0 ? " m" : " l"); } strcat(buf," W n"); if (use_forms) { /* FORM METHOD */ sprintf(&buf[strlen(buf)]," /%s Do Q\n",xobject_name(box->srcbox.pageno)); if (strlen(bigbuf)+strlen(buf) > nbb) { int newsize; newsize=nbb*2; willus_mem_realloc_robust_warn((void **)&bigbuf,newsize,nbb,funcname,10); nbb=newsize; } strcat(bigbuf,buf); } else { /* NO-FORMS METHOD */ strcat(buf,"\n"); /* Create new objects in document for tx matrix and restore matrix */ s1indirect = pdf_new_indirect(ctx,new_stream_object(xref,ctx,buf),0,(void *)xref); if (qref==0) qref=new_stream_object(xref,ctx,"Q\n"); qindirect = pdf_new_indirect(ctx,qref,0,(void *)xref); /* Store this region into the destination page contents array */ pdf_array_push(destpagecontents,s1indirect); if (pdf_is_array(srcpagecontents)) { int k; for (k=0;k<pdf_array_len(srcpagecontents);k++) pdf_array_push(destpagecontents,pdf_array_get(srcpagecontents,k)); } else pdf_array_push(destpagecontents,srcpagecontents); pdf_array_push(destpagecontents,qindirect); pdf_drop_obj(s1indirect); pdf_drop_obj(qindirect); } } pdf_drop_obj(parent); /* For forms, convert all original source pages to XObject Forms */ if (use_forms) wmupdf_convert_pages_to_forms(xref,ctx,srcpageused); /* Update page count and kids array */ countobj = pdf_new_int(ctx, pdf_array_len(kids)); pdf_dict_puts(pages, "Count", countobj); pdf_drop_obj(countobj); pdf_dict_puts(pages, "Kids", kids); pdf_drop_obj(kids); /* Also preserve the (partial) Dests name tree */ if (olddests) wmupdf_preserve_old_dests(olddests,ctx,xref,pages); if (use_forms) { /* Free memory */ willus_mem_free((double **)&bigbuf,funcname); willus_mem_free((double **)&srcpageused,funcname); } return(0); }
int willus_mem_realloc_robust(double **ptr,long newsize,long oldsize,char *name) { #if (defined(WIN32) && !defined(__DMC__) && !defined(MINGW)) unsigned long memsize; void *newptr; #else size_t memsize; void *newptr; #endif #ifndef NOMEMDEBUG #ifdef DEBUG int ra=0; #endif #endif // NOMEMDEBUG #if (defined(WIN32) && !defined(__DMC__) && !defined(MINGW)) memsize=(unsigned long)newsize; #else memsize=(size_t)newsize; #endif if (memsize!=newsize) return(0); if ((*ptr)==NULL || oldsize<=0) return(willus_mem_alloc(ptr,newsize,name)); #if (defined(WIN32) && !defined(__DMC__) && !defined(MINGW)) #ifdef USEGLOBAL newptr = (void *)GlobalReAlloc((void *)(*ptr),memsize,GMEM_MOVEABLE); #else newptr = (void *)CoTaskMemRealloc((void *)(*ptr),memsize); #endif #else newptr = realloc((void *)(*ptr),memsize); #endif if (newptr==NULL && willus_mem_alloc((double **)&newptr,newsize,name)) { #ifndef NOMEMDEBUG #ifdef DEBUG ra=1; printf("Copying %ld bytes from old pointer to new pointer.\n",oldsize); #endif #endif // NOMEMDEBUG memcpy(newptr,(*ptr),oldsize); #ifndef NOMEMDEBUG #ifdef DEBUG printf("Done.\n"); #endif #endif // NOMEMDEBUG willus_mem_free(ptr,name); } if (newptr==NULL) return(0); #ifndef NOMEMDEBUG #ifdef DEBUG if (!willus_mem_inited) willus_mem_init(); if (ra==0) { int i; char label[80]; for (i=0;i<n && ptrs[i]!=(*ptr);i++); if (i>=n) { sprintf(label,"!!Bad RRA!! oldptr=%p,oldsize=%d ",(*ptr),(int)oldsize); totmem += memsize-oldsize; ptrs[n] = newptr; sizealloced[n] = memsize; strncpy(fname[n],name,31); fname[n][31]='\0'; n++; allocated_ptrs++; willus_mem_update(label,name,memsize,newptr); } // printf("*** !! realloc can't find pointer in list !! ***\n"); else { totmem += memsize-sizealloced[i]; sizealloced[i] = memsize; ptrs[i] = newptr; strncpy(fname[i],name,31); fname[i][31]='\0'; willus_mem_update(" RRA ",name,memsize,newptr); } } #endif #endif // NOMEMDEBUG (*ptr) = newptr; return(1); }
int willus_mem_realloc(double **ptr,long newsize,char *name) { #if (defined(WIN32) && !defined(__DMC__) && !defined(MINGW)) unsigned long memsize; void *newptr; #else size_t memsize; void *newptr; #endif #if (defined(WIN32) && !defined(__DMC__) && !defined(MINGW)) memsize=(unsigned long)newsize; #else memsize=(size_t)newsize; #endif if (memsize!=newsize) return(0); if ((*ptr)==NULL) return(willus_mem_alloc(ptr,newsize,name)); #if (defined(WIN32) && !defined(__DMC__) && !defined(MINGW)) #ifdef USEGLOBAL newptr = (void *)GlobalReAlloc((void *)(*ptr),memsize,GMEM_MOVEABLE); if (newptr==NULL) { printf("GlobalReAlloc fails:\n %s\n",win_lasterror()); printf(" Function: %s\n",name); printf(" Mem size requested: %ld\n",newsize); } #else newptr = (void *)CoTaskMemRealloc((void *)(*ptr),memsize); #endif #else newptr = realloc((void *)(*ptr),memsize); #endif if (newptr==NULL && willus_mem_alloc((double **)&newptr,newsize,name)) { printf("!! DIRTY REALLOC in willus_mem_realloc !!\n"); memcpy(newptr,(*ptr),newsize); willus_mem_free(ptr,name); } if (newptr==NULL) return(0); #ifndef NOMEMDEBUG #ifdef DEBUG if (!willus_mem_inited) willus_mem_init(); { int i; char label[64]; for (i=0;i<n && ptrs[i]!=(*ptr);i++); if (i>=n) { sprintf(label,"!!Bad SRA!! oldptr=%p ",(*ptr)); totmem += memsize; ptrs[n] = newptr; sizealloced[n] = memsize; strncpy(fname[n],name,31); fname[n][31]='\0'; n++; allocated_ptrs++; willus_mem_update(label,name,memsize,newptr); } else { totmem += memsize-sizealloced[i]; sizealloced[i] = memsize; ptrs[i] = newptr; strncpy(fname[i],name,31); fname[i][31]='\0'; willus_mem_update(" SRA ",name,memsize,newptr); } } #endif #endif // NOMEMDEBUG (*ptr) = newptr; return(1); }
/* ** Conversion thread. This is launched as a separate thread to do the ** conversions while the dialog box displays the progress. */ static void k2gui_cbox_start_conversion(void *data) { void **ptrs; int i; K2PDFOPT_CONVERSION *k2conv; K2PDFOPT_SETTINGS *k2settings; K2PDFOPT_FILELIST_PROCESS k2listproc; static char *funcname="k2gui_cbox_start_conversion"; ptrs=(void **)data; k2conv=(K2PDFOPT_CONVERSION *)ptrs[0]; k2settings=&k2conv->k2settings; /* ** Count files */ k2gui_cbox_set_num_files(1); k2gui_cbox_set_files_completed(0,"Counting files..."); overwrite_set(1); for (i=k2listproc.filecount=0;i<k2conv->k2files.n;i++) { k2listproc.bmp=NULL; k2listproc.outname=NULL; k2listproc.mode=K2PDFOPT_FILELIST_PROCESS_MODE_GET_FILECOUNT; k2pdfopt_proc_wildarg(k2settings,k2conv->k2files.file[i],&k2listproc); willus_mem_free((double **)&k2listproc.outname,funcname); } if (k2listproc.filecount==0) { k2gui_cbox_set_files_completed(0,"No files"); k2gui_alertbox(0,"No files","No files can be opened for conversion."); k2gui_cbox_conversion_thread_cleanup(); return; } k2gui_cbox_set_num_files(k2listproc.filecount); k2gui_cbox_set_files_completed(0,NULL); /* ** Process files */ k2gui_cbox_set_error_count(0); k2gui_cbox_freelist(); overwrite_set(0); for (i=k2listproc.filecount=0;i<k2conv->k2files.n;i++) { char *buf; K2PDFOPT_CONVERSION *k2c; STRBUF *cmdline,_cmdline; willus_mem_alloc_warn((void **)&buf,1024,funcname,10); k2gui_cbox_set_num_pages(1); sprintf(buf,"Starting conversion of %s...",k2gui_short_name(k2conv->k2files.file[i])); k2gui_cbox_set_pages_completed(0,buf); willus_mem_alloc_warn((void **)&k2c,sizeof(K2PDFOPT_CONVERSION),funcname,10); k2pdfopt_conversion_init(k2c); cmdline=&_cmdline; strbuf_init(cmdline); k2gui_settings_to_cmdline(cmdline,&k2conv->k2settings); parse_cmd_args(k2c,k2gui->env,cmdline,&k2gui->cmdxtra,1,1); k2listproc.outname=NULL; k2listproc.bmp=NULL; k2listproc.mode=K2PDFOPT_FILELIST_PROCESS_MODE_CONVERT_FILES; k2pdfopt_proc_wildarg(&k2c->k2settings,k2conv->k2files.file[i],&k2listproc); #if (WILLUSDEBUGX & 0x2000) printf("\n\nDone conversion...\n\n"); #endif str0_addstring(&k2gui_cbox->filelist,&k2gui_cbox->filelist_na,k2listproc.outname); willus_mem_free((double **)&k2listproc.outname,funcname); k2pdfopt_conversion_close(k2c); willus_mem_free((double **)&k2c,funcname); willus_mem_free((double **)&buf,funcname); } k2gui_cbox_set_files_completed(k2listproc.filecount,NULL); if (k2listproc.filecount==k2conv->k2files.n && k2gui_cbox->error_count==0) k2gui_cbox->successful=1; k2gui_cbox_conversion_thread_cleanup(); }
void wpdfboxes_free(WPDFBOXES *boxes) { static char *funcname="wpdfboxes_free"; willus_mem_free((double **)&boxes->box,funcname); }
/* ** Frees dynamically allocated memory that was allocated by readx(). */ void freex(double **x) { willus_mem_free(x,"freex"); }
/* ** Open file using file association if necessary. This can be necessary if ** the file is being used by another process. */ int willusgui_open_file_ex(char *filename) { char ext[512]; short *extw; short exew[512]; char *exename; char pwd[512]; char cmdargs[512]; int maxsize,status,procnum; HRESULT hr; static char *funcname="willusgui_open_file_ex"; status=willusgui_open_file(filename); if (status!=-1) return(status); strncpy(&ext[1],wfile_ext(filename),510); ext[511]='\0'; ext[0]='.'; utf8_to_utf16_alloc((void **)&extw,ext); maxsize=511; hr=AssocQueryStringW(0,ASSOCSTR_EXECUTABLE,(LPCWSTR)extw,NULL,(LPWSTR)exew,(DWORD *)&maxsize); willus_mem_free((double **)&extw,funcname); if (hr==S_OK) utf16_to_utf8_alloc((void **)&exename,exew); if (hr!=S_OK || wfile_status(exename)!=1) { char *message; int len; static char *funcname="willusgui_open_file_ex"; static int bcolors[3]= {0x6060b0,0xf0f0f0,0xf0f0f0}; if (hr==S_OK) willus_mem_free((double **)&exename,funcname); len=strlen(filename); willus_mem_alloc_warn((void **)&message,len+80,funcname,10); sprintf(message,"Cannot find application to open %s!",filename); willusgui_message_box(NULL,"Error",message,"*&OK",NULL,NULL, NULL,0,24,640,0xe0e0e0,bcolors,NULL,1); willus_mem_free((double **)&message,funcname); return(0); } /* Put quotes around filename -> cmdargs */ strncpy(&cmdargs[1],filename,509); cmdargs[510]='\0'; cmdargs[0]='\"'; maxsize=strlen(cmdargs); cmdargs[maxsize]='\"'; cmdargs[maxsize+1]='\0'; /* Get working directory */ strncpy(pwd,wfile_get_wd(),511); pwd[511]='\0'; /* Make sure PWD isn't UNC -- if it is, default C:\ */ if (pwd[0]=='\\' && pwd[1]=='\\') strcpy(pwd,"C:\\"); process_launch_ex(exename,cmdargs,1,1,pwd,1,&procnum); willus_mem_free((double **)&exename,funcname); return(1); /* if (procnum>=0) { int status,exitcode; while ((status=process_done_ex(procnum,&exitcode))==0) win_sleep(10); if (exitcode==0) return(1); } */ }
/* ** 24-bit only */ int win_emf_to_bmp(wmetafile *wmf,int dpi,WILLUSBITMAP *bmp,FILE *out) { HENHMETAFILE hemf; HWND h; ENHMETAHEADER header; double width_in,height_in,dpi_adjust; int width_pix,height_pix; int width_bytes; HDC hDC,hMemDC; RECT lprect; BITMAPINFO *bmi; static HBITMAP hBitmap=NULL; int planes,bpp; static char *funcname="win_emf_to_bmp"; hemf=(HENHMETAFILE)wmf; h=GetDesktopWindow(); GetEnhMetaFileHeader(hemf,sizeof(ENHMETAHEADER),&header); /* Set up bitmap dimensions */ width_in = (header.rclFrame.right-header.rclFrame.left)/2540.; height_in = (header.rclFrame.bottom-header.rclFrame.top)/2540.; dpi_adjust = header.rclFrame.left==0 ? 1.0 : (1.0/5.8); /* printf("h.rclframe.left=%g\n",header.rclFrame.left/2540.); printf("width_in = %g\n",width_in); printf("szlmm = %d\n",header.szlMillimeters); printf("szlpix = %d\n",header.szlDevice); printf("iType = %d\n",header.iType); printf("dSig = %d\n",header.dSignature); printf("nDesc = %d\n",header.nDescription); printf("nVer = %d\n",header.nVersion); printf("h.rclbounds.left=%d\n",header.rclBounds.left); printf("h.rclbounds.width=%d\n",header.rclBounds.right-header.rclBounds.left); */ width_pix = (int)(width_in*dpi*dpi_adjust); height_pix = (int)(height_in*dpi*dpi_adjust); width_bytes=width_pix; while (width_bytes&3) width_bytes++; lprect.left=0; lprect.right=width_pix-1; lprect.top=0; lprect.bottom=height_pix-1; /* ** Allocate bit map info structure (has to be allocated due to ** arbitrarily sized color palette ==> cannot be statically declared) */ if (!willus_mem_alloc((double **)&bmi,sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD),funcname)) return(2); bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); // Get device context hDC=GetDC(h); if (hDC==NULL) { willus_mem_free((double **)&bmi,funcname); nprintf(out,"win_emf_to_bmp: Could not get DC.\n"); return(4); } planes=GetDeviceCaps(hDC,PLANES); bpp=GetDeviceCaps(hDC,BITSPIXEL); // Create a windows bitmap to play the metafile into hBitmap=CreateBitmap(width_pix,height_pix,planes,bpp,NULL); if (hBitmap==NULL) { ReleaseDC(h,hDC); willus_mem_free((double **)&bmi,funcname); return(5); } // Create a device context for the windows bitmap hMemDC=CreateCompatibleDC(hDC); if (hMemDC==NULL) { nprintf(out,"win_emf_to_bmp: Could not create hMemDC.\n"); DeleteObject(hBitmap); ReleaseDC(h,hDC); willus_mem_free((double **)&bmi,funcname); return(6); } // Select the windows bitmap into the new device context SelectObject(hMemDC,hBitmap); // Clear the windows bitmap PatBlt(hMemDC,0,0,width_pix,height_pix,WHITENESS); // White background // Play the metafile into it PlayEnhMetaFile(hMemDC,hemf,&lprect); bmp->bpp=24; bmp->width=width_pix; bmp->height=height_pix; bmp->type=WILLUSBITMAP_TYPE_WIN32; bmp_alloc(bmp); // Set up the device independent bitmap bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi->bmiHeader.biWidth = width_pix; bmi->bmiHeader.biHeight = height_pix; bmi->bmiHeader.biPlanes = 1; bmi->bmiHeader.biBitCount = 24; bmi->bmiHeader.biCompression = BI_RGB; bmi->bmiHeader.biSizeImage = 0; bmi->bmiHeader.biXPelsPerMeter = (int)(dpi*100./2.54); bmi->bmiHeader.biYPelsPerMeter = (int)(dpi*100./2.54); bmi->bmiHeader.biClrUsed = 0; bmi->bmiHeader.biClrImportant = 0; // Get the bits from the windows bitmap into the DI bitmap GetDIBits(hMemDC,hBitmap,0,height_pix,bmp->data,bmi,DIB_RGB_COLORS); DeleteDC(hMemDC); DeleteObject(hBitmap); ReleaseDC(h,hDC); willus_mem_free((double **)&bmi,funcname); return(0); }
int win_clipboard_to_bmp(WILLUSBITMAP *bmp,FILE *out) { static char *funcname="win_clipboard_to_bmp"; HBITMAP hbitmap; HWND dtwin; HDC hDC,hMemDC; dtwin=GetDesktopWindow(); if (!OpenClipboard(dtwin)) { nprintf(out,"win_clipboard_to_bmp: Error opening clipboard.\n"); return(-1); } hbitmap=(HBITMAP)GetClipboardData(CF_BITMAP); if (hbitmap==NULL) { nprintf(out,"win_clipboard_to_bmp: Clipboard does not have a bitmap.\n"); CloseClipboard(); return(-2); } // This is kludgy, but works for 24-bit desktops hDC=GetDC(dtwin); if (hDC==NULL) { nprintf(out,"win_clipboard_to_bmp: Could not get DC.\n"); CloseClipboard(); return(-3); } // Create a device context for the windows bitmap hMemDC=CreateCompatibleDC(hDC); if (hMemDC==NULL) { nprintf(out,"win_clipboard_to_bmp: Could not create hMemDC.\n"); // DeleteObject(hbitmap); ReleaseDC(dtwin,hDC); return(-4); } // Select the windows bitmap into the new device context SelectObject(hMemDC,hbitmap); /* Kludgey way to get BMP dims, but GetBitmapDimensionsEx() doesn't work. */ { int step; for (step=2048,bmp->width=0;1;bmp->width+=step) { if (GetPixel(hMemDC,bmp->width,0)==CLR_INVALID) { if (step==1 || bmp->width==0) break; bmp->width -= step; step >>= 1; } } for (step=1024,bmp->height=0;1;bmp->height+=step) { if (GetPixel(hMemDC,0,bmp->height)==CLR_INVALID) { if (step==1 || bmp->height==0) break; bmp->height -= step; step >>= 1; } } } if (bmp->height==0 || bmp->width==0) { nprintf(out,"win_clipboard_to_bmp: Zero sized bitmap.\n"); DeleteDC(hMemDC); ReleaseDC(dtwin,hDC); return(-5); } bmp->bpp=24; bmp->type=WILLUSBITMAP_TYPE_WIN32; bmp_alloc(bmp); // Set up the device independent bitmap { BITMAPINFO *bmi; willus_mem_alloc_warn((void **)&bmi,sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD),funcname,10); bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi->bmiHeader.biWidth = bmp->width; bmi->bmiHeader.biHeight = bmp->height; bmi->bmiHeader.biPlanes = 1; bmi->bmiHeader.biBitCount = 24; bmi->bmiHeader.biCompression = BI_RGB; bmi->bmiHeader.biSizeImage = 0; bmi->bmiHeader.biXPelsPerMeter = (int)(72.0*100./2.54); bmi->bmiHeader.biYPelsPerMeter = (int)(72.0*100./2.54); bmi->bmiHeader.biClrUsed = 0; bmi->bmiHeader.biClrImportant = 0; GetDIBits(hMemDC,hbitmap,0,bmp->height,bmp->data,bmi,DIB_RGB_COLORS); /* Old way--pretty slow */ /* for (row=0;row<bmp->height;row++) { unsigned char *p; int col; p=bmp_rowptr_from_top(bmp,row); for (col=0;col<bmp->width;col++,p+=3) { int x; x=(int)GetPixel(hMemDC,col,row); p[0]=x&0xff; p[1]=(x>>8)&0xff; p[2]=(x>>16)&0xff; } } */ willus_mem_free((double **)&bmi,funcname); } DeleteDC(hMemDC); ReleaseDC(dtwin,hDC); return(0); }
void freexyz(double **x) { willus_mem_free(x,"freexyz"); }