|
| 1 | + |
| 2 | +<!DOCTYPE html> |
| 3 | +<html> |
| 4 | + <head> |
| 5 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 | + <style> |
| 7 | + body { |
| 8 | + background: black; |
| 9 | + color: rgb(80, 80, 80); |
| 10 | + } |
| 11 | + body, pre, #legend span { |
| 12 | + font-family: Menlo, monospace; |
| 13 | + font-weight: bold; |
| 14 | + } |
| 15 | + #topbar { |
| 16 | + background: black; |
| 17 | + position: fixed; |
| 18 | + top: 0; left: 0; right: 0; |
| 19 | + height: 42px; |
| 20 | + border-bottom: 1px solid rgb(80, 80, 80); |
| 21 | + } |
| 22 | + #content { |
| 23 | + margin-top: 50px; |
| 24 | + } |
| 25 | + #nav, #legend { |
| 26 | + float: left; |
| 27 | + margin-left: 10px; |
| 28 | + } |
| 29 | + #legend { |
| 30 | + margin-top: 12px; |
| 31 | + } |
| 32 | + #nav { |
| 33 | + margin-top: 10px; |
| 34 | + } |
| 35 | + #legend span { |
| 36 | + margin: 0 5px; |
| 37 | + } |
| 38 | + .cov0 { color: rgb(192, 0, 0) } |
| 39 | +.cov1 { color: rgb(128, 128, 128) } |
| 40 | +.cov2 { color: rgb(116, 140, 131) } |
| 41 | +.cov3 { color: rgb(104, 152, 134) } |
| 42 | +.cov4 { color: rgb(92, 164, 137) } |
| 43 | +.cov5 { color: rgb(80, 176, 140) } |
| 44 | +.cov6 { color: rgb(68, 188, 143) } |
| 45 | +.cov7 { color: rgb(56, 200, 146) } |
| 46 | +.cov8 { color: rgb(44, 212, 149) } |
| 47 | +.cov9 { color: rgb(32, 224, 152) } |
| 48 | +.cov10 { color: rgb(20, 236, 155) } |
| 49 | + |
| 50 | + </style> |
| 51 | + </head> |
| 52 | + <body> |
| 53 | + <div id="topbar"> |
| 54 | + <div id="nav"> |
| 55 | + <select id="files"> |
| 56 | + |
| 57 | + <option value="file0">github.com/gopherdojo/dojo8/kadai2/tanaka0325/imgconv/file.go (0.0%)</option> |
| 58 | + |
| 59 | + <option value="file1">github.com/gopherdojo/dojo8/kadai2/tanaka0325/imgconv/format.go (0.0%)</option> |
| 60 | + |
| 61 | + <option value="file2">github.com/gopherdojo/dojo8/kadai2/tanaka0325/imgconv/imgconv.go (95.2%)</option> |
| 62 | + |
| 63 | + </select> |
| 64 | + </div> |
| 65 | + <div id="legend"> |
| 66 | + <span>not tracked</span> |
| 67 | + |
| 68 | + <span class="cov0">not covered</span> |
| 69 | + <span class="cov8">covered</span> |
| 70 | + |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + <div id="content"> |
| 74 | + |
| 75 | + <pre class="file" id="file0" style="display: none">package imgconv |
| 76 | + |
| 77 | +import ( |
| 78 | + "io" |
| 79 | + "os" |
| 80 | +) |
| 81 | + |
| 82 | +type FileHandler interface { |
| 83 | + Open(string) (io.ReadCloser, error) |
| 84 | + Create(string) (io.WriteCloser, error) |
| 85 | +} |
| 86 | + |
| 87 | +type File struct { |
| 88 | + Reader io.Reader |
| 89 | + Writer io.Writer |
| 90 | +} |
| 91 | + |
| 92 | +func (File) Open(n string) (io.ReadCloser, error) <span class="cov0" title="0">{ return os.Open(n) }</span> |
| 93 | +func (File) Create(n string) (io.WriteCloser, error) <span class="cov0" title="0">{ return os.Create(n) }</span> |
| 94 | + |
| 95 | +func NewFile() File <span class="cov0" title="0">{ |
| 96 | + return File{} |
| 97 | +}</span> |
| 98 | +</pre> |
| 99 | + |
| 100 | + <pre class="file" id="file1" style="display: none">package imgconv |
| 101 | + |
| 102 | +import ( |
| 103 | + "image" |
| 104 | + "image/gif" |
| 105 | + "image/jpeg" |
| 106 | + "image/png" |
| 107 | + "io" |
| 108 | + |
| 109 | + "golang.org/x/image/bmp" |
| 110 | + "golang.org/x/image/tiff" |
| 111 | +) |
| 112 | + |
| 113 | +type Decoder interface { |
| 114 | + Decode(io.Reader) (image.Image, error) |
| 115 | +} |
| 116 | + |
| 117 | +type Encoder interface { |
| 118 | + Encode(io.Writer, image.Image) error |
| 119 | +} |
| 120 | + |
| 121 | +type ImageFormater interface { |
| 122 | + Decoder |
| 123 | + Encoder |
| 124 | + GetExt() string |
| 125 | +} |
| 126 | + |
| 127 | +type ImageFormat struct { |
| 128 | + Ext string |
| 129 | +} |
| 130 | + |
| 131 | +// ImagePng is type for png format. |
| 132 | +type PNG ImageFormat |
| 133 | + |
| 134 | +func (PNG) Decode(r io.Reader) (image.Image, error) <span class="cov0" title="0">{ return png.Decode(r) }</span> |
| 135 | +func (PNG) Encode(w io.Writer, i image.Image) error <span class="cov0" title="0">{ return png.Encode(w, i) }</span> |
| 136 | +func (p *PNG) GetExt() string <span class="cov0" title="0">{ return p.Ext }</span> |
| 137 | + |
| 138 | +// JPEG is type for jpeg format. |
| 139 | +type JPEG ImageFormat |
| 140 | + |
| 141 | +func (JPEG) Decode(r io.Reader) (image.Image, error) <span class="cov0" title="0">{ return jpeg.Decode(r) }</span> |
| 142 | +func (JPEG) Encode(w io.Writer, i image.Image) error <span class="cov0" title="0">{ return jpeg.Encode(w, i, nil) }</span> |
| 143 | +func (j *JPEG) GetExt() string <span class="cov0" title="0">{ return j.Ext }</span> |
| 144 | + |
| 145 | +// GIF is type for gif format. |
| 146 | +type GIF ImageFormat |
| 147 | + |
| 148 | +func (GIF) Decode(r io.Reader) (image.Image, error) <span class="cov0" title="0">{ return gif.Decode(r) }</span> |
| 149 | +func (GIF) Encode(w io.Writer, i image.Image) error <span class="cov0" title="0">{ |
| 150 | + return gif.Encode(w, i, &gif.Options{NumColors: 256}) |
| 151 | +}</span> |
| 152 | +func (g *GIF) GetExt() string <span class="cov0" title="0">{ return g.Ext }</span> |
| 153 | + |
| 154 | +// BMP is type for bmp format. |
| 155 | +type BMP ImageFormat |
| 156 | + |
| 157 | +func (BMP) Decode(r io.Reader) (image.Image, error) <span class="cov0" title="0">{ return bmp.Decode(r) }</span> |
| 158 | +func (BMP) Encode(w io.Writer, i image.Image) error <span class="cov0" title="0">{ return bmp.Encode(w, i) }</span> |
| 159 | +func (b *BMP) GetExt() string <span class="cov0" title="0">{ return b.Ext }</span> |
| 160 | + |
| 161 | +// TIFF is type for tiff format. |
| 162 | +type TIFF ImageFormat |
| 163 | + |
| 164 | +func (TIFF) Decode(r io.Reader) (image.Image, error) <span class="cov0" title="0">{ return tiff.Decode(r) }</span> |
| 165 | +func (TIFF) Encode(w io.Writer, i image.Image) error <span class="cov0" title="0">{ return tiff.Encode(w, i, nil) }</span> |
| 166 | +func (t *TIFF) GetExt() string <span class="cov0" title="0">{ return t.Ext }</span> |
| 167 | + |
| 168 | +func NewImageFormat(ext string) ImageFormater <span class="cov0" title="0">{ |
| 169 | + switch ext </span>{ |
| 170 | + case "png":<span class="cov0" title="0"> |
| 171 | + return &PNG{Ext: "png"}</span> |
| 172 | + case "jpg", "jpeg":<span class="cov0" title="0"> |
| 173 | + return &JPEG{Ext: "jpeg"}</span> |
| 174 | + case "gif":<span class="cov0" title="0"> |
| 175 | + return &GIF{Ext: "gif"}</span> |
| 176 | + case "bmp":<span class="cov0" title="0"> |
| 177 | + return &BMP{Ext: "bmp"}</span> |
| 178 | + case "tiff", "tif":<span class="cov0" title="0"> |
| 179 | + return &TIFF{Ext: "tiff"}</span> |
| 180 | + } |
| 181 | + |
| 182 | + <span class="cov0" title="0">return nil</span> |
| 183 | +} |
| 184 | +</pre> |
| 185 | + |
| 186 | + <pre class="file" id="file2" style="display: none">// Imgconv package is to convert images file format. |
| 187 | +package imgconv |
| 188 | + |
| 189 | +import "io" |
| 190 | + |
| 191 | +// ConvertParam is parameter to convert image format. |
| 192 | +type ConvertParam struct { |
| 193 | + Path string |
| 194 | + FileHandler FileHandler |
| 195 | + BeforeFormat ImageFormater |
| 196 | + AfterFormat ImageFormater |
| 197 | +} |
| 198 | + |
| 199 | +// Do is func to convert image format. |
| 200 | +func Do(param ConvertParam) (rerr error) <span class="cov8" title="1">{ |
| 201 | + r, err := param.FileHandler.Open(param.Path) |
| 202 | + if err != nil </span><span class="cov8" title="1">{ |
| 203 | + return err |
| 204 | + }</span> |
| 205 | + <span class="cov8" title="1">defer r.Close() |
| 206 | + |
| 207 | + e := len(param.Path) - len(param.BeforeFormat.GetExt()) |
| 208 | + w, err := param.FileHandler.Create(param.Path[:e] + param.AfterFormat.GetExt()) |
| 209 | + if err != nil </span><span class="cov8" title="1">{ |
| 210 | + return err |
| 211 | + }</span> |
| 212 | + <span class="cov8" title="1">defer func() </span><span class="cov8" title="1">{ |
| 213 | + err := w.Close() |
| 214 | + if err != nil </span><span class="cov0" title="0">{ |
| 215 | + rerr = err |
| 216 | + }</span> |
| 217 | + }() |
| 218 | + |
| 219 | + <span class="cov8" title="1">if err := convert(r, param.BeforeFormat, w, param.AfterFormat); err != nil </span><span class="cov8" title="1">{ |
| 220 | + return err |
| 221 | + }</span> |
| 222 | + |
| 223 | + <span class="cov8" title="1">return nil</span> |
| 224 | +} |
| 225 | + |
| 226 | +func convert(r io.Reader, d Decoder, w io.Writer, e Encoder) error <span class="cov8" title="1">{ |
| 227 | + img, err := d.Decode(r) |
| 228 | + if err != nil </span><span class="cov8" title="1">{ |
| 229 | + return err |
| 230 | + }</span> |
| 231 | + |
| 232 | + <span class="cov8" title="1">if err := e.Encode(w, img); err != nil </span><span class="cov8" title="1">{ |
| 233 | + return err |
| 234 | + }</span> |
| 235 | + |
| 236 | + <span class="cov8" title="1">return nil</span> |
| 237 | +} |
| 238 | +</pre> |
| 239 | + |
| 240 | + </div> |
| 241 | + </body> |
| 242 | + <script> |
| 243 | + (function() { |
| 244 | + var files = document.getElementById('files'); |
| 245 | + var visible; |
| 246 | + files.addEventListener('change', onChange, false); |
| 247 | + function select(part) { |
| 248 | + if (visible) |
| 249 | + visible.style.display = 'none'; |
| 250 | + visible = document.getElementById(part); |
| 251 | + if (!visible) |
| 252 | + return; |
| 253 | + files.value = part; |
| 254 | + visible.style.display = 'block'; |
| 255 | + location.hash = part; |
| 256 | + } |
| 257 | + function onChange() { |
| 258 | + select(files.value); |
| 259 | + window.scrollTo(0, 0); |
| 260 | + } |
| 261 | + if (location.hash != "") { |
| 262 | + select(location.hash.substr(1)); |
| 263 | + } |
| 264 | + if (!visible) { |
| 265 | + select("file0"); |
| 266 | + } |
| 267 | + })(); |
| 268 | + </script> |
| 269 | +</html> |
0 commit comments