void* FileUtil::ReadRawData(const String& filepath, size_t &size) { FileStream file; if(!file.open(filepath)) return 0; size = file.size(); file.seek(0,Seek_begin); binary data = reinterpret_cast<binary>(base_malloc(sizeof(byte)*size)); file.read(data,size); return data; }
bool JpegFormat::onLoad(FileOp* fop) { struct jpeg_decompress_struct cinfo; struct error_mgr jerr; Image *image; FILE *file; JDIMENSION num_scanlines; JSAMPARRAY buffer; JDIMENSION buffer_height; int c; file = fopen(fop->filename.c_str(), "rb"); if (!file) return false; // Initialize the JPEG decompression object with error handling. jerr.fop = fop; cinfo.err = jpeg_std_error(&jerr.head); jerr.head.error_exit = error_exit; jerr.head.output_message = output_message; // Establish the setjmp return context for error_exit to use. if (setjmp(jerr.setjmp_buffer)) { jpeg_destroy_decompress(&cinfo); fclose(file); return false; } jpeg_create_decompress(&cinfo); // Specify data source for decompression. jpeg_stdio_src(&cinfo, file); // Read file header, set default decompression parameters. jpeg_read_header(&cinfo, true); if (cinfo.jpeg_color_space == JCS_GRAYSCALE) cinfo.out_color_space = JCS_GRAYSCALE; else cinfo.out_color_space = JCS_RGB; // Start decompressor. jpeg_start_decompress(&cinfo); // Create the image. image = fop_sequence_image(fop, (cinfo.out_color_space == JCS_RGB ? IMAGE_RGB: IMAGE_GRAYSCALE), cinfo.output_width, cinfo.output_height); if (!image) { jpeg_destroy_decompress(&cinfo); fclose(file); return false; } // Create the buffer. buffer_height = cinfo.rec_outbuf_height; buffer = (JSAMPARRAY)base_malloc(sizeof(JSAMPROW) * buffer_height); if (!buffer) { jpeg_destroy_decompress(&cinfo); fclose(file); return false; } for (c=0; c<(int)buffer_height; c++) { buffer[c] = (JSAMPROW)base_malloc(sizeof(JSAMPLE) * cinfo.output_width * cinfo.output_components); if (!buffer[c]) { for (c--; c>=0; c--) base_free(buffer[c]); base_free(buffer); jpeg_destroy_decompress(&cinfo); fclose(file); return false; } } // Generate a grayscale palette if is necessary. if (image->imgtype == IMAGE_GRAYSCALE) for (c=0; c<256; c++) fop_sequence_set_color(fop, c, c, c, c); // Read each scan line. while (cinfo.output_scanline < cinfo.output_height) { // TODO /* if (plugin_want_close()) */ /* break; */ num_scanlines = jpeg_read_scanlines(&cinfo, buffer, buffer_height); /* RGB */ if (image->imgtype == IMAGE_RGB) { uint8_t* src_address; uint32_t* dst_address; int x, y, r, g, b; for (y=0; y<(int)num_scanlines; y++) { src_address = ((uint8_t**)buffer)[y]; dst_address = ((uint32_t**)image->line)[cinfo.output_scanline-1+y]; for (x=0; x<image->w; x++) { r = *(src_address++); g = *(src_address++); b = *(src_address++); *(dst_address++) = _rgba(r, g, b, 255); } } } /* Grayscale */ else { uint8_t* src_address; uint16_t* dst_address; int x, y; for (y=0; y<(int)num_scanlines; y++) { src_address = ((uint8_t**)buffer)[y]; dst_address = ((uint16_t**)image->line)[cinfo.output_scanline-1+y]; for (x=0; x<image->w; x++) *(dst_address++) = _graya(*(src_address++), 255); } } fop_progress(fop, (float)(cinfo.output_scanline+1) / (float)(cinfo.output_height)); if (fop_is_stop(fop)) break; } /* destroy all data */ for (c=0; c<(int)buffer_height; c++) base_free(buffer[c]); base_free(buffer); jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(file); return true; }
bool JpegFormat::onSave(FileOp* fop) { struct jpeg_compress_struct cinfo; struct error_mgr jerr; Image *image = fop->seq.image; FILE *file; JSAMPARRAY buffer; JDIMENSION buffer_height; SharedPtr<JpegOptions> jpeg_options = fop->seq.format_options; int c; // Open the file for write in it. file = fopen(fop->filename.c_str(), "wb"); if (!file) { fop_error(fop, "Error creating file.\n"); return false; } // Allocate and initialize JPEG compression object. jerr.fop = fop; cinfo.err = jpeg_std_error(&jerr.head); jpeg_create_compress(&cinfo); // SPECIFY data destination file. jpeg_stdio_dest(&cinfo, file); // SET parameters for compression. cinfo.image_width = image->w; cinfo.image_height = image->h; if (image->imgtype == IMAGE_GRAYSCALE) { cinfo.input_components = 1; cinfo.in_color_space = JCS_GRAYSCALE; } else { cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; } jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, (int)MID(0, 100.0f * jpeg_options->quality, 100), true); cinfo.dct_method = JDCT_ISLOW; cinfo.smoothing_factor = 0; // START compressor. jpeg_start_compress(&cinfo, true); // CREATE the buffer. buffer_height = 1; buffer = (JSAMPARRAY)base_malloc(sizeof(JSAMPROW) * buffer_height); if (!buffer) { fop_error(fop, "Not enough memory for the buffer.\n"); jpeg_destroy_compress(&cinfo); fclose(file); return false; } for (c=0; c<(int)buffer_height; c++) { buffer[c] = (JSAMPROW)base_malloc(sizeof(JSAMPLE) * cinfo.image_width * cinfo.num_components); if (!buffer[c]) { fop_error(fop, "Not enough memory for buffer scanlines.\n"); for (c--; c>=0; c--) base_free(buffer[c]); base_free(buffer); jpeg_destroy_compress(&cinfo); fclose(file); return false; } } // Write each scan line. while (cinfo.next_scanline < cinfo.image_height) { // RGB if (image->imgtype == IMAGE_RGB) { uint32_t* src_address; uint8_t* dst_address; int x, y; for (y=0; y<(int)buffer_height; y++) { src_address = ((uint32_t**)image->line)[cinfo.next_scanline+y]; dst_address = ((uint8_t**)buffer)[y]; for (x=0; x<image->w; x++) { c = *(src_address++); *(dst_address++) = _rgba_getr(c); *(dst_address++) = _rgba_getg(c); *(dst_address++) = _rgba_getb(c); } } } // Grayscale. else { uint16_t* src_address; uint8_t* dst_address; int x, y; for (y=0; y<(int)buffer_height; y++) { src_address = ((uint16_t**)image->line)[cinfo.next_scanline+y]; dst_address = ((uint8_t**)buffer)[y]; for (x=0; x<image->w; x++) *(dst_address++) = _graya_getv(*(src_address++)); } } jpeg_write_scanlines(&cinfo, buffer, buffer_height); fop_progress(fop, (float)(cinfo.next_scanline+1) / (float)(cinfo.image_height)); } // Destroy all data. for (c=0; c<(int)buffer_height; c++) base_free(buffer[c]); base_free(buffer); // Finish compression. jpeg_finish_compress(&cinfo); // Release JPEG compression object. jpeg_destroy_compress(&cinfo); // We can close the output file. fclose(file); // All fine. return true; }
static base::string get_key_for_pidl(LPITEMIDLIST pidl) { #if 0 char *key = base_malloc(get_pidl_size(pidl)+1); UINT c, i = 0; while (pidl) { for (c=0; c<pidl->mkid.cb; ++c) { if (pidl->mkid.abID[c]) key[i++] = pidl->mkid.abID[c]; else key[i++] = 1; } pidl = get_next_pidl(pidl); } key[i] = 0; return key; #else STRRET strret; TCHAR pszName[MAX_PATH]; char key[4096]; int len; ustrcpy(key, empty_string); // Go pidl by pidl from the fullpidl to the root (desktop) //PRINTF("FS: ***\n"); pidl = clone_pidl(pidl); while (pidl->mkid.cb > 0) { if (shl_idesktop->GetDisplayNameOf(pidl, SHGDN_INFOLDER | SHGDN_FORPARSING, &strret) == S_OK) { StrRetToBuf(&strret, pidl, pszName, MAX_PATH); //PRINTF("FS: + %s\n", pszName); len = ustrlen(pszName); if (len > 0) { if (*key) { if (pszName[len-1] != '\\') { memmove(key+len+1, key, ustrlen(key)+1); key[len] = '\\'; } else memmove(key+len, key, ustrlen(key)+1); } else key[len] = 0; memcpy(key, pszName, len); } } remove_last_pidl(pidl); } free_pidl(pidl); //PRINTF("FS: =%s\n***\n", key); return key; #endif }