static size_t data_arrived(void *ptr, size_t size, size_t nmemb, void *state_) { curl_stream_state *state = (curl_stream_state *)state_; int old_start; size *= nmemb; if (state->data_arrived == 0) { double d; long response; int len; /* This is the first time data has arrived. If the response * code is 206, then we can do byte requests, and we will * known the total_length from having processed the header * already. */ curl_easy_getinfo(state->handle, CURLINFO_RESPONSE_CODE, &response); if (state->total_length && response == 206) { /* We got a range header, and the correct http response * code. We can assume that byte fetches are accepted * and we'll run without progressive mode. */ state->content_length = len = state->total_length; state->map_length = (len+BLOCK_SIZE-1)>>BLOCK_SHIFT; state->map = fz_malloc_no_throw(state->ctx, (state->map_length+7)>>3); state->buffer = fz_malloc_no_throw(state->ctx, len); state->buffer_max = len; if (state->map == NULL || state->buffer == NULL) { /* FIXME: Crap error handling! */ exit(1); } memset(state->map, 0, (state->map_length+7)>>3); }
void *hb_malloc(size_t size) { fz_context *ctx = get_context(); assert(ctx != NULL); return fz_malloc_no_throw(ctx, size); }
/* Allocate new context structure, and initialise allocator, and sections * that aren't shared between contexts. */ static fz_context * new_context_phase1(const fz_alloc_context *alloc, const fz_locks_context *locks) { fz_context *ctx; ctx = alloc->malloc(alloc->user, sizeof(fz_context)); if (!ctx) return NULL; memset(ctx, 0, sizeof *ctx); ctx->user = NULL; ctx->alloc = alloc; ctx->locks = *locks; ctx->glyph_cache = NULL; ctx->error = Memento_label(fz_malloc_no_throw(ctx, sizeof(fz_error_context)), "fz_error_context"); if (!ctx->error) goto cleanup; ctx->error->top = ctx->error->stack - 1; ctx->error->errcode = FZ_ERROR_NONE; ctx->error->message[0] = 0; ctx->warn = Memento_label(fz_malloc_no_throw(ctx, sizeof(fz_warn_context)), "fz_warn_context"); if (!ctx->warn) goto cleanup; ctx->warn->message[0] = 0; ctx->warn->count = 0; /* New initialisation calls for context entries go here */ fz_try(ctx) { fz_new_aa_context(ctx); } fz_catch(ctx) { goto cleanup; } return ctx; cleanup: fprintf(stderr, "cannot create context (phase 1)\n"); fz_drop_context(ctx); return NULL; }
char * fz_strdup_no_throw(fz_context *ctx, char *s) { int len = strlen(s) + 1; char *ns = fz_malloc_no_throw(ctx, len); if (ns) memcpy(ns, s, len); return ns; }
/** * * Create a JPEG image format and save to file or byte buffer * * When *env is passed in we are assuming creation of a byte buffer. * * To improve performance I am using GetPrimitiveArrayCritical(). Later on we could change this to a * ByteBuffer and avoid getting in the way of the GC due to array pinning. * */ void * jni_write_jpg(JNIEnv *env, fz_context *ctx, fz_pixmap *pix, const char *file, float zoom, int color, int quality) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE *fp = NULL; unsigned char *outbuffer = NULL; long unsigned int outlen = 4096; JSAMPLE *trgbuf = NULL; int stride = pix->w * (pix->n - 1); int size = pix->w * pix->h; int i = 0; /* * Step 1: allocate and initialize JPEG compression object */ cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); /* * Step 2: specify data destination */ if (env) { outbuffer = malloc(outlen); if (!outbuffer) goto cleanup; jpeg_mem_dest(&cinfo, &outbuffer, &outlen); } else { fp = fopen(file, "wb"); if (!fp) goto cleanup; jpeg_stdio_dest(&cinfo, fp); } /* * Step 3: set parameters for compression */ cinfo.image_width = pix->w; cinfo.image_height = pix->h; cinfo.input_components = pix->n - 1; if (color == COLOR_GRAY_SCALE) { cinfo.in_color_space = JCS_GRAYSCALE; } else { cinfo.in_color_space = JCS_RGB; } jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE); cinfo.X_density = jni_resolution(zoom); cinfo.Y_density = jni_resolution(zoom);; cinfo.density_unit = 1; /* * Step 4: Compression initialization */ jpeg_start_compress(&cinfo, TRUE); /* * Step 5: Remove alpha from original pixels */ trgbuf = (JSAMPLE*)fz_malloc_no_throw(ctx, pix->h*stride); if (!trgbuf) { goto cleanup; } JSAMPLE * ptrbuf = trgbuf; JSAMPLE * pixels = pix->samples; if (color == COLOR_GRAY_SCALE) { for (i=0; i<size; i++) { *ptrbuf++ = pixels[0]; pixels += pix->n; } } else { for (i=0; i<size; i++) { *ptrbuf++ = pixels[0]; *ptrbuf++ = pixels[1]; *ptrbuf++ = pixels[2]; pixels += pix->n; } } /* * Step 6: while (scan lines remain to be written) */ JSAMPROW row_pointer[1]; while (cinfo.next_scanline < cinfo.image_height) { row_pointer[0] = &trgbuf[cinfo.next_scanline * stride]; jpeg_write_scanlines(&cinfo, row_pointer, 1); } cleanup: /* * Step 7: Finish compression */ jpeg_finish_compress(&cinfo); jbyteArray ba = NULL; if (env) { ba = jni_new_byte_array(outlen); if (ba) { jbyte *pa = jni_start_array_critical(ba); if (pa) { JOCTET *pbuf = outbuffer; for (i=0; i<outlen; i++) *pa++ = (jbyte)*pbuf++; jni_end_array_critical(ba, pa); } } free(outbuffer); } else { if (fp) fclose(fp); } /* * Step 8: release JPEG compression object */ jpeg_destroy_compress(&cinfo); if (trgbuf) fz_free(ctx, trgbuf); if (env) { return ba; } return NULL; }
static void * fz_dct_mem_alloc(j_common_ptr cinfo, size_t size) { fz_dctd *state = JZ_DCT_STATE_FROM_CINFO(cinfo); return fz_malloc_no_throw(state->ctx, size); }