static bool do_compress(GifFileType* input_gif, GifFileType* output_gif, int sample_size) { int in_width = input_gif->SWidth; int in_height = input_gif->SHeight; int ss = MIN(sample_size, MIN(in_width, in_height)); int image_count = input_gif->ImageCount; int i; output_gif->SWidth = in_width / ss; output_gif->SHeight = in_height / ss; output_gif->SColorResolution = input_gif->SColorResolution; output_gif->SBackGroundColor = input_gif->SBackGroundColor; output_gif->AspectByte = input_gif->AspectByte; output_gif->SColorMap = input_gif->SColorMap; output_gif->ImageCount = image_count; output_gif->ExtensionBlockCount = input_gif->ExtensionBlockCount; output_gif->ExtensionBlocks = input_gif->ExtensionBlocks; output_gif->SavedImages = (SavedImage *) malloc(image_count * sizeof(SavedImage)); memset(output_gif->SavedImages, '\0', image_count * sizeof(SavedImage)); // Avoid free twice input_gif->SColorMap = NULL; input_gif->ExtensionBlockCount = 0; input_gif->ExtensionBlocks = NULL; if (output_gif->SavedImages != NULL) { for (i = 0; i < image_count; i++) { if (!compress_image(input_gif->SavedImages + i, output_gif->SavedImages + i, ss)) { return false; } } return true; } else { LOGW(EMSG("Out of memory!")); return false; } }
/** * Software fallback for generate mipmap levels. */ static void fallback_generate_mipmap(struct gl_context *ctx, GLenum target, struct gl_texture_object *texObj) { struct pipe_context *pipe = st_context(ctx)->pipe; struct pipe_resource *pt = st_get_texobj_resource(texObj); const uint baseLevel = texObj->BaseLevel; const uint lastLevel = pt->last_level; const uint face = _mesa_tex_target_to_face(target); uint dstLevel; GLenum datatype; GLuint comps; GLboolean compressed; if (ST_DEBUG & DEBUG_FALLBACK) debug_printf("%s: fallback processing\n", __FUNCTION__); assert(target != GL_TEXTURE_3D); /* not done yet */ compressed = _mesa_is_format_compressed(texObj->Image[face][baseLevel]->TexFormat); if (compressed) { GLenum type = _mesa_get_format_datatype(texObj->Image[face][baseLevel]->TexFormat); datatype = type == GL_UNSIGNED_NORMALIZED ? GL_UNSIGNED_BYTE : GL_FLOAT; comps = 4; } else { _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat, &datatype, &comps); assert(comps > 0 && "bad texture format in fallback_generate_mipmap()"); } for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) { const uint srcLevel = dstLevel - 1; const uint srcWidth = u_minify(pt->width0, srcLevel); const uint srcHeight = u_minify(pt->height0, srcLevel); const uint srcDepth = u_minify(pt->depth0, srcLevel); const uint dstWidth = u_minify(pt->width0, dstLevel); const uint dstHeight = u_minify(pt->height0, dstLevel); const uint dstDepth = u_minify(pt->depth0, dstLevel); struct pipe_transfer *srcTrans, *dstTrans; const ubyte *srcData; ubyte *dstData; int srcStride, dstStride; srcTrans = pipe_get_transfer(pipe, pt, srcLevel, face, PIPE_TRANSFER_READ, 0, 0, srcWidth, srcHeight); dstTrans = pipe_get_transfer(pipe, pt, dstLevel, face, PIPE_TRANSFER_WRITE, 0, 0, dstWidth, dstHeight); srcData = (ubyte *) pipe_transfer_map(pipe, srcTrans); dstData = (ubyte *) pipe_transfer_map(pipe, dstTrans); srcStride = srcTrans->stride / util_format_get_blocksize(srcTrans->resource->format); dstStride = dstTrans->stride / util_format_get_blocksize(dstTrans->resource->format); /* this cannot work correctly for 3d since it does not respect layerStride. */ if (compressed) { const enum pipe_format format = pt->format; const uint bw = util_format_get_blockwidth(format); const uint bh = util_format_get_blockheight(format); const uint srcWidth2 = align(srcWidth, bw); const uint srcHeight2 = align(srcHeight, bh); const uint dstWidth2 = align(dstWidth, bw); const uint dstHeight2 = align(dstHeight, bh); uint8_t *srcTemp, *dstTemp; assert(comps == 4); srcTemp = malloc(srcWidth2 * srcHeight2 * comps * (datatype == GL_FLOAT ? 4 : 1)); dstTemp = malloc(dstWidth2 * dstHeight2 * comps * (datatype == GL_FLOAT ? 4 : 1)); /* decompress the src image: srcData -> srcTemp */ decompress_image(format, datatype, srcData, srcTemp, srcWidth2, srcHeight2, srcTrans->stride); _mesa_generate_mipmap_level(target, datatype, comps, 0 /*border*/, srcWidth2, srcHeight2, srcDepth, srcTemp, srcWidth2, /* stride in texels */ dstWidth2, dstHeight2, dstDepth, dstTemp, dstWidth2); /* stride in texels */ /* compress the new image: dstTemp -> dstData */ compress_image(format, datatype, dstTemp, dstData, dstWidth2, dstHeight2, dstTrans->stride); free(srcTemp); free(dstTemp); } else { _mesa_generate_mipmap_level(target, datatype, comps, 0 /*border*/, srcWidth, srcHeight, srcDepth, srcData, srcStride, /* stride in texels */ dstWidth, dstHeight, dstDepth, dstData, dstStride); /* stride in texels */ } pipe_transfer_unmap(pipe, srcTrans); pipe_transfer_unmap(pipe, dstTrans); pipe->transfer_destroy(pipe, srcTrans); pipe->transfer_destroy(pipe, dstTrans); } }
int main(int argc, char const *argv[]) { printf("It almost works!\n"); compress_image(NULL, NULL, NULL); return 0; }
Error EditorTextureImportPlugin::_process_texture_data(Ref<ImageTexture> &texture,int format, float quality,int flags,EditorExportPlatform::ImageCompression p_compr,int tex_flags,float shrink) { if (format==IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS || format==IMAGE_FORMAT_COMPRESS_DISK_LOSSY) { Image image=texture->get_data(); ERR_FAIL_COND_V(image.empty(),ERR_INVALID_DATA); bool has_alpha=image.detect_alpha(); if (!has_alpha && image.get_format()==Image::FORMAT_RGBA) { image.convert(Image::FORMAT_RGB); } if (image.get_format()==Image::FORMAT_RGBA && flags&IMAGE_FLAG_FIX_BORDER_ALPHA) { image.fix_alpha_edges(); } if (image.get_format()==Image::FORMAT_RGBA && flags&IMAGE_FLAG_PREMULT_ALPHA) { image.premultiply_alpha(); } if (flags&IMAGE_FLAG_CONVERT_NORMAL_TO_XY) { image.normalmap_to_xy(); } //if ((image.get_format()==Image::FORMAT_RGB || image.get_format()==Image::FORMAT_RGBA) && flags&IMAGE_FLAG_CONVERT_TO_LINEAR) { // image.srgb_to_linear(); //} if (shrink>1) { int orig_w=image.get_width(); int orig_h=image.get_height(); image.resize(orig_w/shrink,orig_h/shrink,Image::INTERPOLATE_CUBIC); texture->create_from_image(image,tex_flags); texture->set_size_override(Size2(orig_w,orig_h)); } else { texture->create_from_image(image,tex_flags); } if (format==IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS) { texture->set_storage(ImageTexture::STORAGE_COMPRESS_LOSSLESS); } else { texture->set_storage(ImageTexture::STORAGE_COMPRESS_LOSSY); } texture->set_lossy_storage_quality(quality); } else { Image image=texture->get_data(); ERR_FAIL_COND_V(image.empty(),ERR_INVALID_DATA); bool has_alpha=image.detect_alpha(); if (!has_alpha && image.get_format()==Image::FORMAT_RGBA) { image.convert(Image::FORMAT_RGB); } if (image.get_format()==Image::FORMAT_RGBA && flags&IMAGE_FLAG_FIX_BORDER_ALPHA) { image.fix_alpha_edges(); } if (image.get_format()==Image::FORMAT_RGBA && flags&IMAGE_FLAG_PREMULT_ALPHA) { image.premultiply_alpha(); } if (flags&IMAGE_FLAG_CONVERT_NORMAL_TO_XY) { image.normalmap_to_xy(); } //if ((image.get_format()==Image::FORMAT_RGB || image.get_format()==Image::FORMAT_RGBA) && flags&IMAGE_FLAG_CONVERT_TO_LINEAR) { // // print_line("CONVERT BECAUSE: "+itos(flags)); // image.srgb_to_linear(); //} int orig_w=image.get_width(); int orig_h=image.get_height(); if (shrink>1) { image.resize(orig_w/shrink,orig_h/shrink,Image::INTERPOLATE_CUBIC); texture->create_from_image(image,tex_flags); texture->set_size_override(Size2(orig_w,orig_h)); } if (!(flags&IMAGE_FLAG_NO_MIPMAPS)) { image.generate_mipmaps(); } if (format!=IMAGE_FORMAT_UNCOMPRESSED) { compress_image(p_compr,image,flags&IMAGE_FLAG_COMPRESS_EXTRA); } texture->create_from_image(image,tex_flags); if (shrink>1 || (format!=IMAGE_FORMAT_UNCOMPRESSED && (image.get_width()!=orig_w || image.get_height()!=orig_h))) { texture->set_size_override(Size2(orig_w,orig_h)); } //uint32_t save_flags=ResourceSaver::FLAG_COMPRESS; } return OK; }
int main(int argc, char **argv) { uint32_t buff_r; FRESULT res; uint32_t n = 0, j = 0; int32_t y_temp = -1; int temp = 0; uint32_t time[64]; uint32_t time_temp[4]; uint32_t time_start = 0; uint32_t time_end = 0; /* 模块初始化 */ exc_init(); /* 中断初始化 */ sys_timer_init(); /* 系统时钟初始化 */ light_init(); /* LED灯初始化 */ serial_initialize((intptr_t)(NULL)); /* 初始化串口 */ sd_init(&Fatfs); /* 初始化SD卡,并创建文件 */ sd_create_file(&image_data, image_data_name); /* create a file */ printf("\n Welcome to k60 software platform! \n"); light_open(LIGHT4); res = f_open(&image_data, "0:DATA.TXT", FA_OPEN_EXISTING | FA_READ); res = f_read(&image_data, buffer, sizeof(buffer), &buff_r); ////// for(n=0; n<64; n++) ////// { ////// for(j=0;j<4;j++) ////// { // sys_timer_read(&time_start); compress_image(); // sys_timer_read(&time_end); ////// ////// time_temp[j] = time_start - time_end; ////// ////// } ////// time[n] = (time_temp[0] +time_temp[1]+time_temp[2]+time_temp[3])/4; ////// } // res = f_open(&image_data, "0:A_IMAGE.TXT", FA_OPEN_EXISTING | FA_WRITE); // /* 取出制表符 */ // if (image_data.fs) // { // for (n = 0; n < ((12800*2) +100) ; n++) // { // if((buffer[n] == '0') || (buffer[n] == '1')) // f_printf(&image_data, "%c", buffer[n]); // } // } // /* 写原始数据 */ //// if (image_data.fs) //// { //// for (n = 0; n < 12800 ; n++) //// { //// if(n%128 == 0) //// { //// y_temp ++; //// } //// //// if(buffer[n] == '0') //// { //// f_printf(&image_data, "%d\t", (n%128)); //// f_printf(&image_data, "%d\n", y_temp); //// } //// } //// } /* 写压缩后图像 */ // if (image_data.fs) // { // for (n = gl_SRNum; n >0 ; n--) // { // f_printf(&image_data, "%d\t%d\n", // (int)StrRoadFind[n].CenterX, (int)StrRoadFind[n].CenterY); // } // } for (n = 0; n <12800 ; n++) { buffer[n] = 0; } for (n = gl_SRNum; n >0 ; n--) { if((int)StrRoadFind[n].Width<12) buffer[(int)StrRoadFind[n].CenterY * 128 + (int)StrRoadFind[n].CenterX] = 1; } if (image_data.fs) { for (n = 12800; n >0 ; n--) { if(n%128 == 0) { f_printf(&image_data, "\n%d\t", (int)buffer[n]); } else { f_printf(&image_data, "%d\t", (int)buffer[n]); } } } // for (n = gl_SRNum; n > 0; n--) // { // printf("%d\t%d\n", (int) StrRoadFind[n].CenterX, // (int) StrRoadFind[n].CenterY); // } // printf("time cost is %d\n", (time_start - time_end)/100); //// for(n=0;n<64;n++) //// { //// f_printf(&image_data, "%d\t%d\n", //// n, time[n]); //// } //// // f_printf(&image_data, "COMPRESS OVER"); // sys_timer_read(&time_start); // find_road(); // sys_timer_read(&time_end); //printf("time cost is %d\n", (time_start - time_end)/100); sd_close_file(&image_data); /* 关闭文件 */ while(1) { light_open(LIGHT6); } }
Error EditorTextureImportPlugin::import2(const String& p_path, const Ref<ResourceImportMetadata>& p_from,EditorExportPlatform::ImageCompression p_compr, bool p_external){ ERR_FAIL_COND_V(p_from->get_source_count()==0,ERR_INVALID_PARAMETER); Ref<ResourceImportMetadata> from=p_from; Ref<ImageTexture> texture; Vector<Ref<AtlasTexture> > atlases; bool atlas = from->get_option("atlas"); int flags=from->get_option("flags"); uint32_t tex_flags=0; if (flags&EditorTextureImportPlugin::IMAGE_FLAG_REPEAT) tex_flags|=Texture::FLAG_REPEAT; if (flags&EditorTextureImportPlugin::IMAGE_FLAG_FILTER) tex_flags|=Texture::FLAG_FILTER; if (!(flags&EditorTextureImportPlugin::IMAGE_FLAG_NO_MIPMAPS)) tex_flags|=Texture::FLAG_MIPMAPS; int shrink=1; if (from->has_option("shrink")) shrink=from->get_option("shrink"); if (atlas) { //prepare atlas! Vector< Image > sources; bool alpha=false; bool crop = from->get_option("crop"); EditorProgress ep("make_atlas","Build Atlas For: "+p_path.get_file(),from->get_source_count()+3); print_line("sources: "+itos(from->get_source_count())); for(int i=0;i<from->get_source_count();i++) { String path = EditorImportPlugin::expand_source_path(from->get_source_path(i)); ep.step("Loading Image: "+path,i); print_line("source path: "+path); Image src; Error err = ImageLoader::load_image(path,&src); if (err) { EditorNode::add_io_error("Couldn't load image: "+path); return err; } if (src.detect_alpha()) alpha=true; sources.push_back(src); } ep.step("Converting Images",sources.size()); for(int i=0;i<sources.size();i++) { if (alpha) { sources[i].convert(Image::FORMAT_RGBA); } else { sources[i].convert(Image::FORMAT_RGB); } } //texturepacker is not really good for optimizing, so.. //will at some point likely replace with my own //first, will find the nearest to a square packing int border=1; Vector<Size2i> src_sizes; Vector<Rect2> crops; ep.step("Cropping Images",sources.size()+1); for(int j=0;j<sources.size();j++) { Size2i s; if (crop) { Rect2 crop = sources[j].get_used_rect(); print_line("CROP: "+crop); s=crop.size; crops.push_back(crop); } else { s=Size2i(sources[j].get_width(),sources[j].get_height()); } s+=Size2i(border*2,border*2); src_sizes.push_back(s); //add a line to constraint width } Vector<Point2i> dst_positions; Size2i dst_size; EditorAtlas::fit(src_sizes,dst_positions,dst_size); print_line("size that workeD: "+itos(dst_size.width)+","+itos(dst_size.height)); ep.step("Blitting Images",sources.size()+2); Image atlas; atlas.create(nearest_power_of_2(dst_size.width),nearest_power_of_2(dst_size.height),0,alpha?Image::FORMAT_RGBA:Image::FORMAT_RGB); for(int i=0;i<sources.size();i++) { int x=dst_positions[i].x; int y=dst_positions[i].y; Ref<AtlasTexture> at = memnew( AtlasTexture ); Size2 sz = Size2(sources[i].get_width(),sources[i].get_height()); if (crop && sz!=crops[i].size) { Rect2 rect = crops[i]; rect.size=sz-rect.size; at->set_region(Rect2(x+border,y+border,crops[i].size.width,crops[i].size.height)); at->set_margin(rect); atlas.blit_rect(sources[i],crops[i],Point2(x+border,y+border)); } else { at->set_region(Rect2(x+border,y+border,sz.x,sz.y)); atlas.blit_rect(sources[i],Rect2(0,0,sources[i].get_width(),sources[i].get_height()),Point2(x+border,y+border)); } String apath = p_path.get_base_dir().plus_file(from->get_source_path(i).get_file().basename()+".atex"); print_line("Atlas Tex: "+apath); at->set_path(apath); atlases.push_back(at); } if (ResourceCache::has(p_path)) { texture = Ref<ImageTexture> ( ResourceCache::get(p_path)->cast_to<ImageTexture>() ); } else { texture = Ref<ImageTexture>( memnew( ImageTexture ) ); } texture->create_from_image(atlas,tex_flags); } else { ERR_FAIL_COND_V(from->get_source_count()!=1,ERR_INVALID_PARAMETER); String src_path = EditorImportPlugin::expand_source_path(from->get_source_path(0)); if (ResourceCache::has(p_path)) { Resource *r = ResourceCache::get(p_path); texture = Ref<ImageTexture> ( r->cast_to<ImageTexture>() ); Image img; Error err = img.load(src_path); ERR_FAIL_COND_V(err!=OK,ERR_CANT_OPEN); texture->create_from_image(img); } else { texture=ResourceLoader::load(src_path,"ImageTexture"); } ERR_FAIL_COND_V(texture.is_null(),ERR_CANT_OPEN); if (!p_external) from->set_source_md5(0,FileAccess::get_md5(src_path)); } int format=from->get_option("format"); float quality=from->get_option("quality"); if (!p_external) { from->set_editor(get_name()); texture->set_path(p_path); texture->set_import_metadata(from); } if (atlas) { if (p_external) { //used by exporter Array rects(true); for(int i=0;i<atlases.size();i++) { rects.push_back(atlases[i]->get_region()); rects.push_back(atlases[i]->get_margin()); } from->set_option("rects",rects); } else { //used by importer for(int i=0;i<atlases.size();i++) { String apath = atlases[i]->get_path(); atlases[i]->set_atlas(texture); Error err = ResourceSaver::save(apath,atlases[i]); if (err) { EditorNode::add_io_error("Couldn't save atlas image: "+apath); return err; } from->set_source_md5(i,FileAccess::get_md5(apath)); } } } if (format==IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS || format==IMAGE_FORMAT_COMPRESS_DISK_LOSSY) { Image image=texture->get_data(); ERR_FAIL_COND_V(image.empty(),ERR_INVALID_DATA); bool has_alpha=image.detect_alpha(); if (!has_alpha && image.get_format()==Image::FORMAT_RGBA) { image.convert(Image::FORMAT_RGB); } if (image.get_format()==Image::FORMAT_RGBA && flags&IMAGE_FLAG_FIX_BORDER_ALPHA) { image.fix_alpha_edges(); } if (shrink>1) { int orig_w=image.get_width(); int orig_h=image.get_height(); image.resize(orig_w/shrink,orig_h/shrink); texture->create_from_image(image,tex_flags); texture->set_size_override(Size2(orig_w,orig_h)); } else { texture->create_from_image(image,tex_flags); } if (format==IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS) { texture->set_storage(ImageTexture::STORAGE_COMPRESS_LOSSLESS); } else { texture->set_storage(ImageTexture::STORAGE_COMPRESS_LOSSY); } texture->set_lossy_storage_quality(quality); Error err = ResourceSaver::save(p_path,texture); if (err!=OK) { EditorNode::add_io_error("Couldn't save converted texture: "+p_path); return err; } } else { print_line("compress..."); Image image=texture->get_data(); ERR_FAIL_COND_V(image.empty(),ERR_INVALID_DATA); bool has_alpha=image.detect_alpha(); if (!has_alpha && image.get_format()==Image::FORMAT_RGBA) { image.convert(Image::FORMAT_RGB); } if (image.get_format()==Image::FORMAT_RGBA && flags&IMAGE_FLAG_FIX_BORDER_ALPHA) { image.fix_alpha_edges(); } int orig_w=image.get_width(); int orig_h=image.get_height(); if (shrink>1) { image.resize(orig_w/shrink,orig_h/shrink); texture->create_from_image(image,tex_flags); texture->set_size_override(Size2(orig_w,orig_h)); } if (!(flags&IMAGE_FLAG_NO_MIPMAPS)) { image.generate_mipmaps(); } if (format!=IMAGE_FORMAT_UNCOMPRESSED) { compress_image(p_compr,image,flags&IMAGE_FLAG_COMPRESS_EXTRA); } print_line("COMPRESSED TO: "+itos(image.get_format())); texture->create_from_image(image,tex_flags); if (shrink>1) { texture->set_size_override(Size2(orig_w,orig_h)); } uint32_t save_flags=ResourceSaver::FLAG_COMPRESS; Error err = ResourceSaver::save(p_path,texture,save_flags); if (err!=OK) { EditorNode::add_io_error("Couldn't save converted texture: "+p_path); return err; } } return OK; }