1// tImageJPG.h 
2// 
3// This clas knows how to load and save a JPeg (.jpg and .jpeg) file. It does zero processing of image data. It knows 
4// the details of the jpg file format and loads the data into a tPixel array. These tPixels may be 'stolen' by the 
5// tPicture's constructor if a jpg file is specified. After the array is stolen the tImageJPG is invalid. This is 
6// purely for performance. 
7// 
8// Copyright (c) 2020 Tristan Grimmer. 
9// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby 
10// granted, provided that the above copyright notice and this permission notice appear in all copies. 
11// 
12// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 
13// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 
14// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
15// AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 
16// PERFORMANCE OF THIS SOFTWARE. 
17 
18#pragma once 
19#include <Foundation/tString.h> 
20#include <Math/tColour.h> 
21#include <Image/tPixelFormat.h> 
22namespace tImage 
23
24 
25 
26class tImageJPG 
27
28public
29 // Creates an invalid tImageJPG. You must call Load manually. 
30 tImageJPG() { } 
31 
32 // If strict is true, if the file is ill-formed even in a non-fatal way, the resultant image will be invalid. 
33 tImageJPG(const tString& jpgFile, bool strict = false) { Load(jpgFile, strict); } 
34 
35 // The data is copied out of jpgFileInMemory. Go ahead and delete after if you want. 
36 tImageJPG(const uint8* jpgFileInMemory, int numBytes, bool strict = false) { Set(jpgFileInMemory, numBytes, strict); } 
37 
38 // This one sets from a supplied pixel array. If steal is true it takes ownership of the pixels pointer. Otherwise 
39 // it just copies the data out. 
40 tImageJPG(tPixel* pixels, int width, int height, bool steal = false) { Set(pixels, width, height, steal); } 
41 
42 virtual ~tImageJPG() { Clear(); } 
43 
44 // Clears the current tImageJPG before loading. Returns success. If false returned, object is invalid. 
45 bool Load(const tString& jpgFile, bool strict = false); 
46 bool Set(const uint8* jpgFileInMemory, int numBytes, bool strict = false); 
47 
48 // This one sets from a supplied pixel array. If steal is true it takes ownership of the pixels pointer. Otherwise 
49 // it just copies the data out. 
50 bool Set(tPixel*, int width, int height, bool steal = false); 
51 
52 // Saves the tImageJPG to the JPeg file specified. The extension of filename must be ".jpg" or ".jpeg". 
53 // The quality int is should be a percent in [1,100]. Returns true on success. 
54 const static int DefaultQuality = 95
55 bool Save(const tString& jpgFile, int quality = DefaultQuality) const
56 
57 // After this call no memory will be consumed by the object and it will be invalid. 
58 void Clear(); 
59 bool IsValid() const { return Pixels ? true : false; } 
60 
61 int GetWidth() const { return Width; } 
62 int GetHeight() const { return Height; } 
63 
64 // IsOpaque always return true for a JPeg. 
65 bool IsOpaque() const { return true; } 
66 
67 // After this call you are the owner of the pixels and must eventually delete[] them. This tImageJPG object is 
68 // invalid afterwards. 
69 tPixel* StealPixels(); 
70 tPixel* GetPixels() const { return Pixels; } 
71 tPixelFormat SrcPixelFormat = tPixelFormat::Invalid
72 
73private
74 int Width = 0
75 int Height = 0
76 tPixel* Pixels = nullptr
77}; 
78 
79 
80// Implementation below this line. 
81 
82 
83inline void tImageJPG::Clear() 
84
85 Width = 0
86 Height = 0
87 delete[] Pixels
88 Pixels = nullptr
89 SrcPixelFormat = tPixelFormat::Invalid
90
91 
92 
93
94