|
| 1 | +// +build !jpeg,!turbojpeg |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright (c) 2013 Jeremy Torres, https://github.com/jeremytorres/rawparser |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining |
| 7 | + a copy of this software and associated documentation files (the |
| 8 | + "Software"), to deal in the Software without restriction, including |
| 9 | + without limitation the rights to use, copy, modify, merge, publish, |
| 10 | + distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + permit persons to whom the Software is furnished to do so, subject to |
| 12 | + the following conditions: |
| 13 | +
|
| 14 | + The above copyright notice and this permission notice shall be |
| 15 | + included in all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | +*/ |
| 25 | + |
| 26 | +package rawparser |
| 27 | + |
| 28 | +import ( |
| 29 | + "bytes" |
| 30 | + "image" |
| 31 | + "image/jpeg" |
| 32 | + "log" |
| 33 | + "os" |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + log.Println("Using pure GO JPEG package") |
| 38 | +} |
| 39 | + |
| 40 | +func decodeAndWriteJpeg(data []byte, quality int, filename string) error { |
| 41 | + jpegFile, err := os.Create(filename) |
| 42 | + defer jpegFile.Close() |
| 43 | + if err != nil { |
| 44 | + log.Printf("Error creating jpeg file: %v\n", err) |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + // Decode image |
| 49 | + decodedImage, err := decodeJpeg(data) |
| 50 | + if err != nil { |
| 51 | + log.Printf("Error decoding embedded jpeg: %v\n", err) |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + // Encode and write using specifid JPEG quality |
| 56 | + err = encodeAndWriteJpeg(jpegFile, decodedImage, quality) |
| 57 | + if err != nil { |
| 58 | + log.Printf("Error encoding embedded jpeg: %v\n", err) |
| 59 | + } |
| 60 | + return err |
| 61 | +} |
| 62 | + |
| 63 | +func decodeJpeg(data []byte) (img image.Image, e error) { |
| 64 | + // Decode JPEG |
| 65 | + bReader := bytes.NewReader(data) |
| 66 | + img, e = jpeg.Decode(bReader) |
| 67 | + if e != nil { |
| 68 | + log.Printf("Error decoding embedded jpeg: %v\n", e) |
| 69 | + return nil, e |
| 70 | + } |
| 71 | + return img, e |
| 72 | +} |
| 73 | + |
| 74 | +// encodeAndWriteJpeg encodes a JPEG image based on a JPEG quality parameter |
| 75 | +// from 1 to 100, where 100 is the best encoding quality. |
| 76 | +func encodeAndWriteJpeg(f *os.File, img image.Image, q int) error { |
| 77 | + e := jpeg.Encode(f, img, &jpeg.Options{q}) |
| 78 | + if e != nil { |
| 79 | + log.Printf("Error encoding and writing embedded jpeg: %v\n", e) |
| 80 | + } |
| 81 | + return e |
| 82 | +} |
0 commit comments