-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[minor] Rearranging thumbnails handling:
* Added KFX reader * Restructure MOBI reader so both KFX and MOBI readers have similar behavior. * Removing "stretch" configuration for MOBI thumbnails - always false.
- Loading branch information
1 parent
6c61b81
commit 8a7abb6
Showing
39 changed files
with
12,000 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
package thumbs | ||
|
||
type ThumbnailsConfig struct { | ||
Width int `yaml:"width" validate:"required,gt=0"` | ||
Height int `yaml:"height" validate:"required,gt=0"` | ||
Stretch bool `yaml:"stretch"` | ||
Width int `yaml:"width" validate:"required,gt=0"` | ||
Height int `yaml:"height" validate:"required,gt=0"` | ||
|
||
Dir string `yaml:"-"` // internal use only | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package imgutils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
) | ||
|
||
// This is specific to go - when encoding jpeg standard encoder does not create JFIF APP0 segment and Kindle does not like it. | ||
|
||
// JpegDPIType specifyes type of the DPI units | ||
type JpegDPIType uint8 | ||
|
||
// DPI units type values | ||
const ( | ||
DpiNoUnits JpegDPIType = iota | ||
DpiPxPerInch | ||
DpiPxPerSm | ||
) | ||
|
||
var ( | ||
marker = []byte{0xFF, 0xE0} // APP0 segment marker | ||
jfif = []byte{0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x02} // jfif + version | ||
) | ||
|
||
// SetJpegDPI creates JFIF APP0 with provided DPI if segment is missing in image. | ||
func SetJpegDPI(buf *bytes.Buffer, dpit JpegDPIType, xdensity, ydensity int16) (*bytes.Buffer, bool) { | ||
|
||
data := buf.Bytes() | ||
|
||
// If JFIF APP0 segment is there - do not do anything | ||
if bytes.Equal(data[2:4], marker) { | ||
return buf, false | ||
} | ||
|
||
var newbuf = new(bytes.Buffer) | ||
|
||
newbuf.Write(data[:2]) | ||
newbuf.Write(marker) | ||
binary.Write(newbuf, binary.BigEndian, uint16(0x10)) // length | ||
newbuf.Write(jfif) | ||
binary.Write(newbuf, binary.BigEndian, uint8(dpit)) | ||
binary.Write(newbuf, binary.BigEndian, uint16(xdensity)) | ||
binary.Write(newbuf, binary.BigEndian, uint16(ydensity)) | ||
binary.Write(newbuf, binary.BigEndian, uint16(0)) // no thumbnail segment | ||
newbuf.Write(data[2:]) | ||
|
||
return newbuf, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package kfx | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/amazon-ion/ion-go/ion" | ||
) | ||
|
||
const ( | ||
largestKnownSymbol = 834 | ||
// | ||
symBookMetadata = 490 | ||
symCategorizedMetadata = 491 | ||
symExternalResource = 164 | ||
symThumbnails = 214 | ||
symRawMedia = 417 | ||
) | ||
|
||
var ( | ||
ionBVM = []byte{0xE0, 1, 0, 0xEA} // binary version marker | ||
sharedSymbolTable = createSST(largestKnownSymbol) | ||
) | ||
|
||
// Actual names for symbols could be obtained by looking at EpubToKFXConverter-4.0.jar from Kindle Previewer 3 | ||
// with enum of interest in class file "com.amazon.kaf.c/B.class" presently. | ||
func createSST(maxID uint64) ion.SharedSymbolTable { | ||
symbols := make([]string, 0, maxID) | ||
for i := len(ion.V1SystemSymbolTable.Symbols()) + 1; i <= len(ion.V1SystemSymbolTable.Symbols())+int(maxID); i++ { | ||
symbols = append(symbols, fmt.Sprintf("$%d", i)) | ||
} | ||
return ion.NewSharedSymbolTable("YJ_symbols", 10, symbols) | ||
} | ||
|
||
func createProlog() []byte { | ||
buf := bytes.Buffer{} | ||
if err := ion.NewBinaryWriter(&buf, sharedSymbolTable).Finish(); err != nil { | ||
panic(err) | ||
} | ||
return buf.Bytes() | ||
} | ||
|
||
func decodeData(prolog, data []byte, v any) error { | ||
if err := ion.Unmarshal(append(prolog, data[len(ionBVM):]...), v, sharedSymbolTable); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func decodeST(data []byte) (ion.SymbolTable, error) { | ||
r := ion.NewReaderCat(bytes.NewReader(data), ion.NewCatalog(sharedSymbolTable)) | ||
r.Next() // we are not interested in the actual values and in most cases this will return false anyways | ||
if err := r.Err(); err != nil { | ||
return nil, err | ||
} | ||
return r.SymbolTable(), nil | ||
} |
Oops, something went wrong.