/* read zipped fluidsim velocities into the co's of the fluidsimsettings normals struct */ static void fluidsim_read_vel_cache(FluidsimModifierData *fluidmd, DerivedMesh *dm, char *filename) { int wri, i, j; float wrf; gzFile gzf; FluidsimSettings *fss = fluidmd->fss; int len = strlen(filename); int totvert = dm->getNumVerts(dm); FluidVertexVelocity *velarray = NULL; // mesh and vverts have to be valid from loading... if (fss->meshVelocities) MEM_freeN(fss->meshVelocities); if (len < 7) { return; } if (fss->domainNovecgen > 0) return; fss->meshVelocities = MEM_callocN(sizeof(FluidVertexVelocity) * dm->getNumVerts(dm), "Fluidsim_velocities"); fss->totvert = totvert; velarray = fss->meshVelocities; // .bobj.gz, correct filename // 87654321 filename[len - 6] = 'v'; filename[len - 5] = 'e'; filename[len - 4] = 'l'; gzf = BLI_gzopen(filename, "rb"); if (!gzf) { MEM_freeN(fss->meshVelocities); fss->meshVelocities = NULL; return; } gzread(gzf, &wri, sizeof(wri)); if (wri != totvert) { MEM_freeN(fss->meshVelocities); fss->meshVelocities = NULL; return; } for (i = 0; i < totvert; i++) { for (j = 0; j < 3; j++) { gzread(gzf, &wrf, sizeof(wrf)); velarray[i].vel[j] = wrf; } } gzclose(gzf); }
/* intended to check for non-blender formats but for now it only reads blends */ static int wm_read_exotic(const char *name) { int len; gzFile gzfile; char header[7]; int retval; /* make sure we're not trying to read a directory.... */ len = strlen(name); if (len > 0 && ELEM(name[len - 1], '/', '\\')) { retval = BKE_READ_EXOTIC_FAIL_PATH; } else { gzfile = BLI_gzopen(name, "rb"); if (gzfile == NULL) { retval = BKE_READ_EXOTIC_FAIL_OPEN; } else { len = gzread(gzfile, header, sizeof(header)); gzclose(gzfile); if (len == sizeof(header) && STREQLEN(header, "BLENDER", 7)) { retval = BKE_READ_EXOTIC_OK_BLEND; } else { #if 0 /* historic stuff - no longer used */ WM_cursor_wait(true); if (is_foo_format(name)) { read_foo(name); retval = BKE_READ_EXOTIC_OK_OTHER; } else #endif { retval = BKE_READ_EXOTIC_FAIL_FORMAT; } #if 0 WM_cursor_wait(false); #endif } } } return retval; }
ImBuf *IMB_loadblend_thumb(const char *path) { gzFile gzfile; /* not necessarily a gzip */ gzfile = BLI_gzopen(path, "rb"); if (NULL == gzfile) { return NULL; } else { ImBuf *img = loadblend_thumb(gzfile); /* read ok! */ gzclose(gzfile); return img; } }
/* intended to check for non-blender formats but for now it only reads blends */ static int wm_read_exotic(Scene *UNUSED(scene), const char *name) { int len; gzFile gzfile; char header[7]; int retval; // make sure we're not trying to read a directory.... len = strlen(name); if (ELEM(name[len - 1], '/', '\\')) { retval = BKE_READ_EXOTIC_FAIL_PATH; } else { gzfile = BLI_gzopen(name, "rb"); if (gzfile == NULL) { retval = BKE_READ_EXOTIC_FAIL_OPEN; } else { len = gzread(gzfile, header, sizeof(header)); gzclose(gzfile); if (len == sizeof(header) && strncmp(header, "BLENDER", 7) == 0) { retval = BKE_READ_EXOTIC_OK_BLEND; } else { //XXX waitcursor(1); #if 0 /* historic stuff - no longer used */ if (is_foo_format(name)) { read_foo(name); retval = BKE_READ_EXOTIC_OK_OTHER; } else #endif { retval = BKE_READ_EXOTIC_FAIL_FORMAT; } //XXX waitcursor(0); } } } return retval; }
/* gzip the file in from_file and write it to memory to_mem, at most size bytes. * return the unziped size */ char *BLI_file_ungzip_to_mem(const char *from_file, int *r_size) { gzFile gzfile; int readsize, size, alloc_size = 0; char *mem = NULL; const int chunk_size = 512 * 1024; size = 0; gzfile = BLI_gzopen(from_file, "rb"); for (;; ) { if (mem == NULL) { mem = MEM_callocN(chunk_size, "BLI_ungzip_to_mem"); alloc_size = chunk_size; } else { mem = MEM_reallocN(mem, size + chunk_size); alloc_size += chunk_size; } readsize = gzread(gzfile, mem + size, chunk_size); if (readsize > 0) { size += readsize; } else { break; } } gzclose(gzfile); if (size == 0) { MEM_freeN(mem); mem = NULL; } else if (alloc_size != size) mem = MEM_reallocN(mem, size); *r_size = size; return mem; }
/* gzip the file in from and write it to "to". * return -1 if zlib fails, -2 if the originating file does not exist * note: will remove the "from" file */ int BLI_file_gzip(const char *from, const char *to) { char buffer[10240]; int file; int readsize = 0; int rval = 0, err; gzFile gzfile; /* level 1 is very close to 3 (the default) in terms of file size, * but about twice as fast, best use for speedy saving - campbell */ gzfile = BLI_gzopen(to, "wb1"); if (gzfile == NULL) return -1; file = BLI_open(from, O_BINARY | O_RDONLY, 0); if (file == -1) return -2; while (1) { readsize = read(file, buffer, sizeof(buffer)); if (readsize < 0) { rval = -2; /* error happened in reading */ fprintf(stderr, "Error reading file %s: %s.\n", from, strerror(errno)); break; } else if (readsize == 0) break; /* done reading */ if (gzwrite(gzfile, buffer, readsize) <= 0) { rval = -1; /* error happened in writing */ fprintf(stderr, "Error writing gz file %s: %s.\n", to, gzerror(gzfile, &err)); break; } } gzclose(gzfile); close(file); return rval; }
/* read .bobj.gz file into a fluidsimDerivedMesh struct */ static DerivedMesh *fluidsim_read_obj(const char *filename, const MPoly *mp_example) { int wri = 0, i; int gotBytes; gzFile gzf; int numverts = 0, numfaces = 0; DerivedMesh *dm = NULL; MPoly *mp; MLoop *ml; MVert *mv; short *normals, *no_s; float no[3]; const short mp_mat_nr = mp_example->mat_nr; const char mp_flag = mp_example->flag; // ------------------------------------------------ // get numverts + numfaces first // ------------------------------------------------ gzf = BLI_gzopen(filename, "rb"); if (!gzf) { return NULL; } // read numverts gotBytes = gzread(gzf, &wri, sizeof(wri)); numverts = wri; // skip verts gotBytes = gzseek(gzf, numverts * 3 * sizeof(float), SEEK_CUR) != -1; // read number of normals if (gotBytes) gotBytes = gzread(gzf, &wri, sizeof(wri)); // skip normals gotBytes = gzseek(gzf, numverts * 3 * sizeof(float), SEEK_CUR) != -1; /* get no. of triangles */ if (gotBytes) gotBytes = gzread(gzf, &wri, sizeof(wri)); numfaces = wri; gzclose(gzf); // ------------------------------------------------ if (!numfaces || !numverts || !gotBytes) return NULL; gzf = BLI_gzopen(filename, "rb"); if (!gzf) { return NULL; } dm = CDDM_new(numverts, 0, 0, numfaces * 3, numfaces); if (!dm) { gzclose(gzf); return NULL; } // read numverts gotBytes = gzread(gzf, &wri, sizeof(wri)); // read vertex position from file mv = CDDM_get_verts(dm); for (i = 0; i < numverts; i++, mv++) gotBytes = gzread(gzf, mv->co, sizeof(float) * 3); // should be the same as numverts gotBytes = gzread(gzf, &wri, sizeof(wri)); if (wri != numverts) { if (dm) dm->release(dm); gzclose(gzf); return NULL; } normals = MEM_callocN(sizeof(short) * numverts * 3, "fluid_tmp_normals"); if (!normals) { if (dm) dm->release(dm); gzclose(gzf); return NULL; } // read normals from file (but don't save them yet) for (i = numverts, no_s = normals; i > 0; i--, no_s += 3) { gotBytes = gzread(gzf, no, sizeof(float) * 3); normal_float_to_short_v3(no_s, no); } /* read no. of triangles */ gotBytes = gzread(gzf, &wri, sizeof(wri)); if (wri != numfaces) { printf("Fluidsim: error in reading data from file.\n"); if (dm) dm->release(dm); gzclose(gzf); MEM_freeN(normals); return NULL; } // read triangles from file mp = CDDM_get_polys(dm); ml = CDDM_get_loops(dm); for (i = 0; i < numfaces; i++, mp++, ml += 3) { int face[3]; gotBytes = gzread(gzf, face, sizeof(int) * 3); /* initialize from existing face */ mp->mat_nr = mp_mat_nr; mp->flag = mp_flag; mp->loopstart = i * 3; mp->totloop = 3; ml[0].v = face[0]; ml[1].v = face[1]; ml[2].v = face[2]; } gzclose(gzf); CDDM_calc_edges(dm); CDDM_apply_vert_normals(dm, (short (*)[3])normals); MEM_freeN(normals); // CDDM_calc_normals(result); return dm; }