ImBuf *IMB_loadifffile(int file, const char *filepath, int flags, char colorspace[IM_MAX_SPACE], const char *descr) { ImBuf *ibuf; unsigned char *mem; size_t size; if (file == -1) return NULL; if (imb_is_filepath_format(filepath)) return IMB_ibImageFromFile(filepath, flags, colorspace, descr); size = BLI_file_descriptor_size(file); mem = mmap(NULL, size, PROT_READ, MAP_SHARED, file, 0); if (mem == (unsigned char *) -1) { fprintf(stderr, "%s: couldn't get mapping %s\n", __func__, descr); return NULL; } ibuf = IMB_ibImageFromMemory(mem, size, flags, colorspace, descr); if (munmap(mem, size)) fprintf(stderr, "%s: couldn't unmap file %s\n", __func__, descr); return ibuf; }
size_t BLI_file_size(const char *path) { int size, file = BLI_open(path, O_BINARY|O_RDONLY, 0); if (file == -1) return -1; size = BLI_file_descriptor_size(file); close(file); return size; }
PackedFile *newPackedFile(ReportList *reports, const char *filename, const char *basepath) { PackedFile *pf = NULL; int file, filelen; char name[FILE_MAX]; void *data; /* render result has no filename and can be ignored * any other files with no name can be ignored too */ if (filename[0] == '\0') return NULL; //XXX waitcursor(1); /* convert relative filenames to absolute filenames */ BLI_strncpy(name, filename, sizeof(name)); BLI_path_abs(name, basepath); /* open the file * and create a PackedFile structure */ file = BLI_open(name, O_BINARY | O_RDONLY, 0); if (file == -1) { BKE_reportf(reports, RPT_ERROR, "Unable to pack file, source path '%s' not found", name); } else { filelen = BLI_file_descriptor_size(file); if (filelen == 0) { /* MEM_mallocN complains about MEM_mallocN(0, "bla"); * we don't care.... */ data = MEM_mallocN(1, "packFile"); } else { data = MEM_mallocN(filelen, "packFile"); } if (read(file, data, filelen) == filelen) { pf = newPackedFileMemory(data, filelen); } else { MEM_freeN(data); } close(file); } //XXX waitcursor(0); return (pf); }
static unsigned char *proxy_thread_next_frame(ProxyQueue *queue, MovieClip *clip, size_t *size_r, int *cfra_r) { unsigned char *mem = NULL; BLI_spin_lock(&queue->spin); if (!*queue->stop && queue->cfra <= queue->efra) { MovieClipUser user = {0}; char name[FILE_MAX]; size_t size; int file; user.framenr = queue->cfra; BKE_movieclip_filename_for_frame(clip, &user, name); file = open(name, O_BINARY | O_RDONLY, 0); if (file < 0) { BLI_spin_unlock(&queue->spin); return NULL; } size = BLI_file_descriptor_size(file); if (size < 1) { close(file); BLI_spin_unlock(&queue->spin); return NULL; } mem = MEM_mallocN(size, "movieclip proxy memory file"); if (read(file, mem, size) != size) { close(file); BLI_spin_unlock(&queue->spin); MEM_freeN(mem); return NULL; } *size_r = size; *cfra_r = queue->cfra; queue->cfra++; close(file); *queue->do_update = 1; *queue->progress = (float)(queue->cfra - queue->sfra) / (queue->efra - queue->sfra); } BLI_spin_unlock(&queue->spin); return mem; }
BlendFileData *BLO_read_runtime(const char *path, ReportList *reports) { BlendFileData *bfd = NULL; size_t actualsize; int fd, datastart; char buf[8]; fd = BLI_open(path, O_BINARY | O_RDONLY, 0); if (fd == -1) { BKE_reportf(reports, RPT_ERROR, "Unable to open '%s': %s", path, strerror(errno)); goto cleanup; } actualsize = BLI_file_descriptor_size(fd); lseek(fd, -12, SEEK_END); datastart = handle_read_msb_int(fd); if (datastart == -1) { BKE_reportf(reports, RPT_ERROR, "Unable to read '%s' (problem seeking)", path); goto cleanup; } else if (read(fd, buf, 8) != 8) { BKE_reportf(reports, RPT_ERROR, "Unable to read '%s' (truncated header)", path); goto cleanup; } else if (memcmp(buf, "BRUNTIME", 8) != 0) { BKE_reportf(reports, RPT_ERROR, "Unable to read '%s' (not a blend file)", path); goto cleanup; } else { //printf("starting to read runtime from %s at datastart %d\n", path, datastart); lseek(fd, datastart, SEEK_SET); bfd = blo_read_blendafterruntime(fd, path, actualsize - datastart, reports); fd = -1; // file was closed in blo_read_blendafterruntime() } cleanup: if (fd != -1) close(fd); return bfd; }
/* read file for specified frame number to the memory */ static unsigned char *prefetch_read_file_to_memory( MovieClip *clip, int current_frame, short render_size, short render_flag, size_t *r_size) { MovieClipUser user = {0}; char name[FILE_MAX]; size_t size; int file; unsigned char *mem; user.framenr = current_frame; user.render_size = render_size; user.render_flag = render_flag; BKE_movieclip_filename_for_frame(clip, &user, name); file = BLI_open(name, O_BINARY | O_RDONLY, 0); if (file == -1) { return NULL; } size = BLI_file_descriptor_size(file); if (size < 1) { close(file); return NULL; } mem = MEM_mallocN(size, "movieclip prefetch memory file"); if (read(file, mem, size) != size) { close(file); MEM_freeN(mem); return NULL; } *r_size = size; close(file); return mem; }
static void imb_loadtilefile(ImBuf *ibuf, int file, int tx, int ty, unsigned int *rect) { ImFileType *type; unsigned char *mem; size_t size; if (file == -1) return; size = BLI_file_descriptor_size(file); mem = mmap(NULL, size, PROT_READ, MAP_SHARED, file, 0); if (mem == (unsigned char *) -1) { fprintf(stderr, "Couldn't get memory mapping for %s\n", ibuf->cachename); return; } for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) if (type->load_tile && type->ftype(type, ibuf)) type->load_tile(ibuf, mem, size, tx, ty, rect); if (munmap(mem, size)) fprintf(stderr, "Couldn't unmap memory for %s.\n", ibuf->cachename); }
static void build_pict_list(PlayState *ps, char *first, int totframes, int fstep, int fontid) { char *mem, filepath[FILE_MAX]; // short val; PlayAnimPict *picture = NULL; struct ImBuf *ibuf = NULL; char str[32 + FILE_MAX]; struct anim *anim; if (IMB_isanim(first)) { /* OCIO_TODO: support different input color space */ anim = IMB_open_anim(first, IB_rect, 0, NULL); if (anim) { int pic; ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE); if (ibuf) { playanim_toscreen(ps, NULL, ibuf, fontid, fstep); IMB_freeImBuf(ibuf); } for (pic = 0; pic < IMB_anim_get_duration(anim, IMB_TC_NONE); pic++) { picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "Pict"); picture->anim = anim; picture->frame = pic; picture->IB_flags = IB_rect; BLI_snprintf(str, sizeof(str), "%s : %4.d", first, pic + 1); picture->name = strdup(str); BLI_addtail(&picsbase, picture); } } else { printf("couldn't open anim %s\n", first); } } else { int count = 0; BLI_strncpy(filepath, first, sizeof(filepath)); pupdate_time(); ptottime = 1.0; /* O_DIRECT * * If set, all reads and writes on the resulting file descriptor will * be performed directly to or from the user program buffer, provided * appropriate size and alignment restrictions are met. Refer to the * F_SETFL and F_DIOINFO commands in the fcntl(2) manual entry for * information about how to determine the alignment constraints. * O_DIRECT is a Silicon Graphics extension and is only supported on * local EFS and XFS file systems. */ while (IMB_ispic(filepath) && totframes) { size_t size; int file; file = open(filepath, O_BINARY | O_RDONLY, 0); if (file < 0) { /* print errno? */ return; } picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "picture"); if (picture == NULL) { printf("Not enough memory for pict struct '%s'\n", filepath); close(file); return; } size = BLI_file_descriptor_size(file); if (size < 1) { close(file); MEM_freeN(picture); return; } picture->size = size; picture->IB_flags = IB_rect; if (fromdisk == FALSE) { mem = (char *)MEM_mallocN(size, "build pic list"); if (mem == NULL) { printf("Couldn't get memory\n"); close(file); MEM_freeN(picture); return; } if (read(file, mem, size) != size) { printf("Error while reading %s\n", filepath); close(file); MEM_freeN(picture); MEM_freeN(mem); return; } } else { mem = NULL; } picture->mem = mem; picture->name = strdup(filepath); close(file); BLI_addtail(&picsbase, picture); count++; pupdate_time(); if (ptottime > 1.0) { /* OCIO_TODO: support different input color space */ if (picture->mem) { ibuf = IMB_ibImageFromMemory((unsigned char *)picture->mem, picture->size, picture->IB_flags, NULL, picture->name); } else { ibuf = IMB_loadiffname(picture->name, picture->IB_flags, NULL); } if (ibuf) { playanim_toscreen(ps, picture, ibuf, fontid, fstep); IMB_freeImBuf(ibuf); } pupdate_time(); ptottime = 0.0; } BLI_newname(filepath, +fstep); #if 0 // XXX25 while (qtest()) { switch (qreadN(&val)) { case ESCKEY: if (val) return; break; } } #endif totframes--; } } return; }