1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Embeddable: Fix if_exists

This commit is contained in:
gnosygnu
2016-11-24 08:53:07 -05:00
parent dae3be97ec
commit fcbdf8bbd8
1352 changed files with 7 additions and 88517 deletions

View File

@@ -1,47 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.gfui.imgs; import gplx.*; import gplx.gfui.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.net.MalformedURLException;
import java.net.URL;
import gplx.core.gfo_regys.*;
public class IconAdp {
public Icon UnderIcon() {return icon;} private final Icon icon;
public Image XtoImage() {return ((ImageIcon)icon).getImage();}
public Io_url Url() {return url;} private Io_url url = Io_url_.Empty;
IconAdp(Icon icon) {this.icon = icon;}
public static IconAdp new_(Icon icon) {return new IconAdp(icon);}
public static IconAdp file_or_blank(Io_url url) {return file_(url);}
public static IconAdp file_(Io_url url) {
Icon icon = new ImageIcon(url.Xto_api());
IconAdp rv = new IconAdp(icon);
rv.url = url;
return rv;
}
public static void regy_loadDir_(Io_url imgDir) {GfoRegy.Instance.RegDir(imgDir, "*.png", true, "_", ".");}
public static void regy_loadDir_shallow(Io_url imgDir) {GfoRegy.Instance.RegDir(imgDir, "*.png", false, "_", ".");}
public static IconAdp regy_(String key) {
GfoRegyItm itm = GfoRegy.Instance.FetchOrNull(key);
if (itm == null) {UsrDlg_.Instance.Warn("missing icon; key={0}", key); return null;}
if (itm.ValType() != GfoRegyItm.ValType_Url) throw Err_.new_wo_type("regyItm should be of type url", "key", key);
return IconAdp.file_(itm.Url());
}
public static IconAdp as_(Object obj) {return obj instanceof IconAdp ? (IconAdp)obj : null;}
}

View File

@@ -1,51 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.gfui.imgs; import gplx.*; import gplx.gfui.*;
import java.awt.Image;
import gplx.gfui.kits.core.*;
public interface ImageAdp extends Rls_able {
Gfui_kit Kit();
SizeAdp Size();
int Width();
int Height();
Io_url Url(); ImageAdp Url_(Io_url v);
Object Under();
boolean Disposed();
void SaveAsBmp(Io_url url);
void SaveAsPng(Io_url url);
ImageAdp Resize(int width, int height);
ImageAdp Extract_image(RectAdp src_rect, SizeAdp trg_size);
ImageAdp Extract_image(int src_x, int src_y, int src_w, int src_h, int trg_w, int trg_h);
}
class ImageAdp_txt implements ImageAdp {
public Gfui_kit Kit() {return Swing_kit.Instance;}
public SizeAdp Size() {return size;} SizeAdp size;
public int Width() {return size.Width();}
public int Height() {return size.Height();}
public Io_url Url() {return url;} public ImageAdp Url_(Io_url v) {url = v; return this;} Io_url url;
public Object Under() {return null;}
public boolean Disposed() {return disposed;} private boolean disposed = false;
public void Rls() {disposed = true;}
public void SaveAsBmp(Io_url url) {SaveAs(url, ".bmp");}
public void SaveAsPng(Io_url url) {SaveAs(url, ".png");}
void SaveAs(Io_url url, String ext) {Io_mgr.Instance.SaveFilStr(url.GenNewExt(ext), size.To_str());}
public ImageAdp Extract_image(RectAdp src_rect, SizeAdp trg_size) {return Extract_image(src_rect.X(), src_rect.Y(), src_rect.Width(), src_rect.Height(), trg_size.Width(), trg_size.Height());}
public ImageAdp Extract_image(int src_x, int src_y, int src_w, int src_h, int trg_w, int trg_h) {return ImageAdp_.txt_mem_(Io_url_.Empty, SizeAdp_.new_(trg_w, trg_h));}
public ImageAdp Resize(int width, int height) {return ImageAdp_.txt_mem_(Io_url_.Empty, SizeAdp_.new_(width, height));}
public ImageAdp_txt(Io_url url, SizeAdp size) {this.url = url; this.size = size;}
}

View File

@@ -1,126 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.gfui.imgs; import gplx.*; import gplx.gfui.*;
import gplx.core.primitives.*;
import gplx.core.ios.*; /*IoStream*/ import gplx.core.ios.streams.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class ImageAdp_ {
public static ImageAdp as_(Object obj) {return obj instanceof ImageAdp ? (ImageAdp)obj : null;}
public static ImageAdp cast(Object obj) {try {return (ImageAdp)obj;} catch(Exception exc) {throw Err_.new_type_mismatch_w_exc(exc, ImageAdp.class, obj);}}
public static final ImageAdp Null = new_(10, 10);
public static ImageAdp new_(int width, int height) {
// BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // JAVA: must be TYPE_INT_RGB or else ImageIO.write("bmp") will fail
BufferedImage img = getCompatibleImage(width, height);
return new ImageAdp_base(img);
}
public static ImageAdp txt_mem_(Io_url url, SizeAdp size) {return new ImageAdp_txt(url, size);}
public static ImageAdp txt_fil_(Io_url url) {
String raw = Io_mgr.Instance.LoadFilStr(url);
SizeAdp size = null;
if (String_.Eq(raw, "")) size = SizeAdp_.Zero;
else if (String_.Eq(url.Ext(), ".svg")) size = SizeOf_svg(url);
else size = SizeAdp_.parse(raw);
return new ImageAdp_txt(url, size);
}
public static SizeAdp SizeOf_svg(Io_url url) {return Gfui_svg_util.QuerySize(url);}
public static ImageAdp file_(Io_url url) {
if (url.EqNull()) throw Err_.new_wo_type("cannot load image from null url");
if (String_.Eq(url.Info().Key(), IoUrlInfo_.Mem.Key())) return txt_fil_(url);
if (!Io_mgr.Instance.ExistsFil(url)) return Null;
BufferedImage img = null;
try {
File f = new File(url.Xto_api());
img = ImageIO.read(f);
}
catch (IOException e) {throw Err_.new_exc(e, "ui", "image load failed", "url", url.Xto_api());}
// FileInputStream istream = new FileInputStream(new File(url.Xto_api()));
// JPEGImageDecoder dec = JPEGCodec.createJPEGDecoder(istream);
// BufferedImage im = dec.decodeAsBufferedImage();
// try {img = ImageIO.read(new File());}
// catch (IOException e) {}
return new ImageAdp_base(img).Url_(url);
}
private static BufferedImage getCompatibleImage(int w, int h)
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage image = gc.createCompatibleImage(w, h);
// System.out.println("compatible image type = " + image.getType());
return image;
}
public static int MemorySize(ImageAdp image) {
int areaInPixels = image.Height() * image.Width();
BufferedImage bufferedImage = (BufferedImage)image.Under();
int bitsPerPixel = BitsPerPixelCalc(bufferedImage.getType(), image.Url());
// int bitsPerPixel = 4;
return areaInPixels * (bitsPerPixel / 8); // 8 bits per byte
}
static int BitsPerPixelCalc(int imageType, Io_url url) { // REF:http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/BufferedImage.html
if (imageType == BufferedImage.TYPE_3BYTE_BGR) return 24;
else if (imageType == BufferedImage.TYPE_4BYTE_ABGR) return 32;
else if (imageType == BufferedImage.TYPE_4BYTE_ABGR_PRE) return 32;
else if (imageType == BufferedImage.TYPE_BYTE_BINARY) return 8; //?
else if (imageType == BufferedImage.TYPE_BYTE_GRAY) return 8; //?
else if (imageType == BufferedImage.TYPE_BYTE_INDEXED) return 8; //?
else if (imageType == BufferedImage.TYPE_CUSTOM) return 8; //?
else if (imageType == BufferedImage.TYPE_INT_ARGB) return 32; //?
else if (imageType == BufferedImage.TYPE_INT_ARGB_PRE) return 32; //?
else if (imageType == BufferedImage.TYPE_INT_BGR) return 32; //?
else if (imageType == BufferedImage.TYPE_INT_RGB) return 32; //?
else if (imageType == BufferedImage.TYPE_USHORT_555_RGB) return 16; //?
else if (imageType == BufferedImage.TYPE_USHORT_565_RGB) return 16; //?
else if (imageType == BufferedImage.TYPE_USHORT_GRAY) return 16; //?
else {UsrDlg_.Instance.Warn("unknown bits per pixel", "imageType", imageType, "url", url.Xto_api()); return 8;}
}
}
class Gfui_svg_util {
public static SizeAdp QuerySize(Io_url url) {
try {
// NOTE: not using XmlDoc b/c invalid doctypes can cause xml to hang; <?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"><!-- Created with Inkscape (http://www.inkscape.org/) --> <svg xmlns="http://www.w3.org/2000/svg"
String xml = Io_mgr.Instance.LoadFilStr(url);
int pos = String_.FindFwd(xml, "<svg", 0); if (pos == -1) return null;
Int_obj_ref pos_ref = Int_obj_ref.New(pos);
double w = ParseAtr(xml, pos_ref, "width");
double h = ParseAtr(xml, pos_ref, "height");
return SizeAdp_.new_((int)w, (int)h);
} catch (Exception e) {Err_.Noop(e); return SizeAdp_.Null;}
}
static double ParseAtr(String xml, Int_obj_ref pos_ref, String atr) {
int pos = String_.FindFwd(xml, atr, pos_ref.Val()); if (pos == -1) return -1;
int bgn = String_.FindFwd(xml, "\"", pos); if (bgn == -1) return -1;
++bgn; // place after quote
int end = String_.FindFwd(xml, "\"", bgn); if (end == -1) return -1;
int px = String_.FindBwd(xml, "px", end); // handle width="20px"
if (px != -1) end = px;
String str = String_.Mid(xml, bgn, end);
pos_ref.Val_(end);
return Double_.parse(str);
}
}

View File

@@ -1,70 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.gfui.imgs; import gplx.*; import gplx.gfui.*;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import gplx.gfui.gfxs.*; import gplx.gfui.kits.core.*;
public class ImageAdp_base implements ImageAdp, Rls_able {
@gplx.Internal protected ImageAdp_base(Image img) {this.under = img;}
public Gfui_kit Kit() {return kit;} public void Kit_(Gfui_kit v) {this.kit = v;} Gfui_kit kit;
public SizeAdp Size() {
if (this == ImageAdp_.Null) return SizeAdp_.Null;
if (size == null) {
size = SizeAdp_.new_(this.Width(), this.Height());
}
return size;
} SizeAdp size = null;
public int Width() {return under.getWidth(null);}
public int Height() {return under.getHeight(null);}
public Io_url Url() {return url;} public ImageAdp Url_(Io_url v) {url = v; return this;} Io_url url = Io_url_.Empty;
public Object Under() {return under;} Image under;
public boolean Disposed() {return disposed;} private boolean disposed = false;
public void Rls() {disposed = true; under.flush();}
public void SaveAsBmp(Io_url url) {SaveAs(url, "bmp");}
public void SaveAsPng(Io_url url) {SaveAs(url, "png");}
void SaveAs(Io_url url, String fmtStr) {
Io_mgr.Instance.CreateDirIfAbsent(url.OwnerDir());
File fil = new File(url.Xto_api());
// String[] formatNames = ImageIO.getWriterFormatNames();
// for (String s : formatNames)
// Tfds.Write(s);
boolean success = false;
try {success = ImageIO.write((BufferedImage)under, fmtStr, fil);}
catch (IOException e) {}
if (!success) throw Err_.new_("gplx.gfui.imgs.SaveImageFailed", "save image failed", "srcUrl", url.Xto_api(), "trgFil", fil, "fmt", fmtStr);
//#@endif
}
public ImageAdp Extract_image(RectAdp src_rect, SizeAdp trg_size) {return Extract_image(src_rect.X(), src_rect.Y(), src_rect.Width(), src_rect.Height(), trg_size.Width(), trg_size.Height());}
public ImageAdp Extract_image(int src_x, int src_y, int src_w, int src_h, int trg_w, int trg_h) {
if (this == ImageAdp_.Null) return ImageAdp_.Null; // TODO_OLD: create ImageAdpNull class (along with ImageAdp interface)
if (disposed) return ImageAdp_.new_(1, 1);
ImageAdp rv = ImageAdp_.new_(trg_w, trg_h);
GfxAdp gfx = GfxAdp_.image_(rv);
gfx.DrawImage(this, 0, 0, trg_w, trg_h, src_x, src_y, src_w, src_h);
gfx.Rls();
return rv;
}
public ImageAdp Resize(int width, int height) {return Extract_image(0, 0, this.Width(), this.Height(), width, height);}
}

View File

@@ -1,35 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.gfui.imgs; import gplx.*; import gplx.gfui.*;
import gplx.gfui.kits.core.*;
public class ImageAdp_null implements ImageAdp {
public Gfui_kit Kit() {return Gfui_kit_.Mem();}
public SizeAdp Size() {return SizeAdp_.Zero;}
public int Width() {return 0;}
public int Height() {return 0;}
public Io_url Url() {return Io_url_.Empty;} public ImageAdp Url_(Io_url v) {return this;}
public Object Under() {return null;}
public boolean Disposed() {return disposed;} private boolean disposed = false;
public void Rls() {disposed = true;}
public void SaveAsBmp(Io_url url) {}
public void SaveAsPng(Io_url url) {}
public ImageAdp Extract_image(RectAdp src_rect, SizeAdp trg_size) {return Extract_image(src_rect.X(), src_rect.Y(), src_rect.Width(), src_rect.Height(), trg_size.Width(), trg_size.Height());}
public ImageAdp Extract_image(int src_x, int src_y, int src_w, int src_h, int trg_w, int trg_h) {return this;}
public ImageAdp Resize(int width, int height) {return this;}
public static final ImageAdp_null Instance = new ImageAdp_null(); ImageAdp_null() {}
}

View File

@@ -1,48 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.gfui.imgs; import gplx.*; import gplx.gfui.*;
import org.junit.*;
import gplx.core.consoles.*;
import gplx.core.ios.*;
import gplx.core.security.*;
import gplx.gfui.imgs.*;
public class ImageAdp_tst {
@Before public void setup() {
load = Tfds.RscDir.GenSubFil_nest("150_gfui", "imgs", "strawberry_java.bmp");
} ImageAdp img; Io_url load;
@Test public void load_() {
img = ImageAdp_.file_(load);
Tfds.Eq(80, img.Width());
Tfds.Eq(80, img.Height());
Tfds.Eq("80,80", img.Size().toString());
Tfds.Eq(img.Url(), load);
}
@Test public void SaveAsBmp() {
img = ImageAdp_.file_(load);
Io_url save = load.GenNewNameOnly("strawberry_temp");
DateAdp beforeModifiedTime = Io_mgr.Instance.QueryFil(save).ModifiedTime();
img.SaveAsBmp(save);
DateAdp afterModifiedTime = Io_mgr.Instance.QueryFil(save).ModifiedTime();
Tfds.Eq_true(CompareAble_.Is_more(afterModifiedTime, beforeModifiedTime));
Hash_algo algo = Hash_algo_.New__md5();
String loadHash = algo.Hash_stream_as_str(Console_adp_.Noop, Io_mgr.Instance.OpenStreamRead(load));
String saveHash = algo.Hash_stream_as_str(Console_adp_.Noop, Io_mgr.Instance.OpenStreamRead(save));
Tfds.Eq(loadHash, saveHash);
}
}