/** * writeImage; Writes the stored image to the given file name. */ bool Image::writeImage(const char * filename) { FILE* file; errno_t file_result = fopen_s(&file, filename, "wb"); if (!file) { printf("Dump file problem... file\n"); return false; } fprintf(file, "P6\n%i %i\n255\n", _width, _height); std::vector<unsigned char> imageC(_image.size()); for (unsigned int i = 0; i<_image.size(); ++i) imageC[i] = (unsigned char)(_image[i] * 255.0f); int t = fwrite(&(imageC[0]), _width * _height * 3, 1, file); if (t != 1) { printf("Dump file problem... fwrite\n"); return false; } std::cout << "Image succesfully written to: " << filename << std::endl; fclose(file); return true; }
/** * readImage; Reads the image from the given file name. */ bool Image::readImage(const char * filename) { FILE* file; errno_t file_result = fopen_s(&file, filename, "rb"); if (!file) { printf("ERROR: No file called %s!\n", filename); return false; } int width, height; fscanf_s(file, "%*s\n"); char buf[256]; while (fgets(buf, 256, file) && buf[0] == '#') { printf(buf); } sscanf_s(buf, "%i %i\n", &width, &height); fscanf_s(file, "255\n"); this->_width = width; this->_height = height; std::vector<unsigned char> imageC(width * height * 3); int t = fread(&(imageC[0]), width * height * 3, 1, file); for (size_t i = 0; i < imageC.size(); i++) { _image.push_back((float)imageC[i] / 255.0f); } printf("Loaded texture %s\n", filename); return true; }
bool Image::writeImage(const char * filename) { FILE* file; file = fopen(filename, "wb"); if (!file) { printf("dump file problem... file\n"); return false; } fprintf(file, "P6\n%i %i\n255\n",_width, _height); std::vector<unsigned char> imageC(_image.size()); for (unsigned int i=0; i<_image.size();++i) imageC[i]=(unsigned char)(_image[i]*255.0f); int t = fwrite(&(imageC[0]), _width * _height * 3, 1, file); if (t!=1) { printf("Dump file problem... fwrite\n"); return false; } fclose(file); return true; }