imghdr 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +53 -0
- data/README_ko_kr.md +52 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/example/test.rb +8 -0
- data/example/testset/lenna.bmp +0 -0
- data/example/testset/lenna.exr +0 -0
- data/example/testset/lenna.gif +0 -0
- data/example/testset/lenna.im32 +0 -0
- data/example/testset/lenna.jpg +0 -0
- data/example/testset/lenna.pbm +0 -0
- data/example/testset/lenna.pgm +5 -0
- data/example/testset/lenna.png +0 -0
- data/example/testset/lenna.ppm +542 -2
- data/example/testset/lenna.ras +0 -0
- data/example/testset/lenna.rgb +0 -0
- data/example/testset/lenna.tiff +0 -0
- data/example/testset/lenna.webp +0 -0
- data/example/testset/lenna.xbm +2734 -0
- data/exe/imghdr +3 -0
- data/lib/imghdr/version.rb +5 -0
- data/lib/imghdr.rb +51 -0
- metadata +76 -0
data/exe/imghdr
ADDED
data/lib/imghdr.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative "imghdr/version"
|
3
|
+
|
4
|
+
module Imghdr
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
def self.what(file)
|
8
|
+
raise Error, "Path is only allowed to be a String" unless file.is_a?(String)
|
9
|
+
raise Error, "File is not readable: #{file}" unless File.readable?(file)
|
10
|
+
raise Error, "File not found: #{file}" unless File.exist?(file)
|
11
|
+
raise Error, "File is not a file: #{file}" unless File.file?(file)
|
12
|
+
raise Error, "File is empty: #{file}" if File.zero?(file)
|
13
|
+
|
14
|
+
File.open(file, "rb") do |f|
|
15
|
+
f = f.read(32)
|
16
|
+
if f[6..9].include? "JFIF" or f[6..9].include? "Exif"
|
17
|
+
return "jpeg"
|
18
|
+
elsif f.include? "PNG"
|
19
|
+
return "png"
|
20
|
+
elsif f[0..7].include? "GIF89" or f[0..7].include? "GIF87"
|
21
|
+
return "gif"
|
22
|
+
elsif f[0..1].include? "BM" or f[0..1].include? "CI"
|
23
|
+
return "bmp"
|
24
|
+
elsif f[0..3].include? "RIFF" and f[8..11].include? "WEBP"
|
25
|
+
return "webp"
|
26
|
+
elsif f[0..3].include? "II" or f[0..3].include? "MM"
|
27
|
+
return "tiff"
|
28
|
+
elsif f[0..3].include? "P1" or f[0..3].include? "P4"
|
29
|
+
return "pbm"
|
30
|
+
elsif f[0..3].include? "P2" or f[0..3].include? "P5"
|
31
|
+
return "pgm"
|
32
|
+
elsif f[0..3].include? "P3" or f[0..3].include? "P6"
|
33
|
+
return "ppm"
|
34
|
+
elsif f[0..10].include? "#define "
|
35
|
+
return "xbm"
|
36
|
+
# UNDERLINES ARE HAVE ASCII-8BIT ENCODING
|
37
|
+
else
|
38
|
+
f.force_encoding("UTF-8")
|
39
|
+
if f[0..1] == "\u0001\xDA" # ASCII-8BIT: "\x01\xda"
|
40
|
+
return "rgb"
|
41
|
+
elsif f[0..3] == "Y\xA6j\x95" # ASCII-8BIT: "\x59\xA6\x6A\x95"
|
42
|
+
return "rast"
|
43
|
+
elsif f[0..3] == "v/1\u0001" # ASCII-8BIT: "\x76\x2F\x31\x01"
|
44
|
+
return "exr"
|
45
|
+
else
|
46
|
+
return nil # unknown type
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: imghdr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- zygn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Detect image file formats based on their headers
|
14
|
+
email:
|
15
|
+
- dbseh5439@gmail.com
|
16
|
+
executables:
|
17
|
+
- imghdr
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- CHANGELOG.md
|
24
|
+
- CODE_OF_CONDUCT.md
|
25
|
+
- Gemfile
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- README_ko_kr.md
|
29
|
+
- Rakefile
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- example/test.rb
|
33
|
+
- example/testset/lenna.bmp
|
34
|
+
- example/testset/lenna.exr
|
35
|
+
- example/testset/lenna.gif
|
36
|
+
- example/testset/lenna.im32
|
37
|
+
- example/testset/lenna.jpg
|
38
|
+
- example/testset/lenna.pbm
|
39
|
+
- example/testset/lenna.pgm
|
40
|
+
- example/testset/lenna.png
|
41
|
+
- example/testset/lenna.ppm
|
42
|
+
- example/testset/lenna.ras
|
43
|
+
- example/testset/lenna.rgb
|
44
|
+
- example/testset/lenna.tiff
|
45
|
+
- example/testset/lenna.webp
|
46
|
+
- example/testset/lenna.xbm
|
47
|
+
- exe/imghdr
|
48
|
+
- lib/imghdr.rb
|
49
|
+
- lib/imghdr/version.rb
|
50
|
+
homepage: https://github.com/zygn
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata:
|
54
|
+
homepage_uri: https://github.com/zygn
|
55
|
+
source_code_uri: https://github.com/zygn/imghdr
|
56
|
+
changelog_uri: https://github.com/zygn/imghdr/blob/main/CHANGELOG.md
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 2.6.0
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.2.15
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Detect image file format
|
76
|
+
test_files: []
|