/****************************************************************************** tv_material_compile_shader Compiles the given shader (first looking to see if the shader already has been loaded by checking the appropriate "loaded_XXX_shaders" table. *****************************************************************************/ GLuint tv_material_compile_shader(tvchar* file, tvuint type) { TvMaterialShader* lup; tvchar* buffer; TvMaterialShader* shader_table; GLuint shader_handle; /* look up the shader - has it been loaded already? */ HASH_FIND_PTR(loaded_vertex_shaders, file, lup); switch(type) { case GL_VERTEX_SHADER: shader_table = loaded_fragment_shaders; break; case GL_FRAGMENT_SHADER: shader_table = loaded_vertex_shaders; break; case GL_GEOMETRY_SHADER: shader_table = loaded_geometry_shaders; break; case GL_TESS_CONTROL_SHADER: shader_table = loaded_tesselation_control_shaders; break; case GL_TESS_EVALUATION_SHADER: shader_table = loaded_tesselation_evaluation_shaders; break; default: tv_warning("unrecognized shader type"); return; } /* look up the shader */ HASH_FIND_PTR(shader_table, file, lup); /* if the shader has already been loaded, return it's handle. */ if(lup) { return (GLuint)lup->id; } /* shader has NOT been loaded, let's load it */ UtilReadFile(file, &buffer); shader_handle = compile_gl_shader(buffer, type); lup = (TvMaterialShader*)tv_alloc(sizeof(TvMaterialShader)); lup->name = file; lup->id = shader_handle; HASH_ADD_PTR(shader_table, name, lup); free(buffer); return (GLuint)shader_handle; }
bool UtilReadFileSplitted(LPCTSTR lpFile,FILELINECONTAINER &container) { //行バッファをクリア container.data.clear(); container.lines.clear(); //---読み込み std::vector<BYTE> cReadBuffer; if(!UtilReadFile(lpFile,cReadBuffer))return false; //終端の0追加 cReadBuffer.resize(cReadBuffer.size()+2); cReadBuffer[cReadBuffer.size()-1]; cReadBuffer[cReadBuffer.size()-2]; { CStringW strData; UtilGuessToUNICODE(strData,&cReadBuffer[0],cReadBuffer.size()); container.data.assign((LPCWSTR)strData,(LPCWSTR)strData+strData.GetLength()); container.data.push_back(L'\0'); } LPWSTR p=&container.data[0]; const LPCWSTR end=p+container.data.size(); LPCWSTR lastHead=p; //解釈 for(;p!=end && *p!=L'\0';p++){ if(*p==_T('\n')||*p==_T('\r')){ if(lastHead<p){ //空行は飛ばす container.lines.push_back(lastHead); } lastHead=p+1; *p=L'\0'; } } return true; }
int webp_decode(const char *in_file, const char *out_file, const FfiWebpDecodeConfig *decode_config) { int return_value = -1; WebPDecoderConfig config; WebPDecBuffer* const output_buffer = &config.output; WebPBitstreamFeatures* const bitstream = &config.input; OutputFileFormat format = PNG; if (!WebPInitDecoderConfig(&config)) { //fprintf(stderr, "Library version mismatch!\n"); return 1; } if (decode_config->output_format != format){ format = decode_config->output_format; } if (decode_config->no_fancy_upsampling > 0){ config.options.no_fancy_upsampling = 1; } if (decode_config->bypass_filtering > 0){ config.options.bypass_filtering = 1; } if (decode_config->use_threads > 0){ config.options.use_threads = 1; } if ((decode_config->crop_w | decode_config->crop_h) > 0){ config.options.use_cropping = 1; config.options.crop_left = decode_config->crop_x; config.options.crop_top = decode_config->crop_y; config.options.crop_width = decode_config->crop_w; config.options.crop_height = decode_config->crop_h; } if ((decode_config->resize_w | decode_config->resize_h) > 0){ config.options.use_scaling = 1; config.options.scaled_width = decode_config->resize_w; config.options.scaled_height = decode_config->resize_h; } VP8StatusCode status = VP8_STATUS_OK; size_t data_size = 0; const uint8_t* data = NULL; if (!UtilReadFile(in_file, &data, &data_size)) return -1; status = WebPGetFeatures(data, data_size, bitstream); if (status != VP8_STATUS_OK) { //fprintf(stderr, "This is invalid webp image!\n"); return_value = 2; goto Error; } switch (format) { case PNG: output_buffer->colorspace = bitstream->has_alpha ? MODE_RGBA : MODE_RGB; break; case PAM: output_buffer->colorspace = MODE_RGBA; break; case PPM: output_buffer->colorspace = MODE_RGB; // drops alpha for PPM break; case PGM: output_buffer->colorspace = bitstream->has_alpha ? MODE_YUVA : MODE_YUV; break; case ALPHA_PLANE_ONLY: output_buffer->colorspace = MODE_YUVA; break; default: free((void*)data); return 3; } status = WebPDecode(data, data_size, &config); if (status != VP8_STATUS_OK) { //fprintf(stderr, "Decoding of %s failed.\n", in_file); return_value = 4; goto Error; } UtilSaveOutput(output_buffer, format, out_file); return_value = 0; Error: free((void*)data); WebPFreeDecBuffer(output_buffer); return return_value; }
void tv_material_load(tv_material *mat, const char* file) { tvchar* text; cJSON* root = NULL; cJSON* json = NULL; tv_material *lup_mat = NULL; /* check if the material is already loaded, if it is copy it and return. */ HASH_FIND_PTR(loaded_materials, file, lup_mat); if(lup_mat) { /* TOOD: will only work if materials are never deleted */ mat = lup_mat; return; } /* if no material was given, create a new material */ if(mat == NULL) { mat = tv_material_new(); } mat->num_passes = 0; /* material is not loaded, load file and parse it */ UtilReadFile(file, &text); root = cJSON_Parse(text); /* check for errors, return NULL if encountered */ if(!root) { fprintf(stderr, "Error: JSON parse error before: [%s]\n", cJSON_GetErrorPtr()); return; } root = root->child; /* make sure "material" object exists */ if(strncmp(root->string, "material", 8) != 0) { tv_warning("unrecognized JSON object name %s" " for material\n", root->string); } else { /* read the material file */ root = root->child; while(root != NULL) { if(strncmp(root->string, "passes", sizeof("passes")) == 0) { /* read all the material's passes */ for(json = root->child; json != NULL; json = json->next) { tv_material_pass* pass = tv_material_load_pass(json->child); tv_material_add_pass(mat, pass); } } /* unrecognized node - print warning */ else { tv_warning("Unrecognized JSON object name %s " "for material\n", root->string); } /* move to the next JSON object */ root = root->next; } /* add the material to the table of loaded materials to prevent reloading */ mat->name = (tvchar*)file; HASH_ADD_PTR(loaded_materials, name, mat); } /* cleanup */ free(text); cJSON_Delete(root); cJSON_Delete(json); }