Skip to content

Commit bcd208e

Browse files
konstibobclaudenormanrz
authored
add jpeg codec and testcases (#81)
* add jpeg codec and testcases * adjusted jpeg implementation * fix JpegCodec constructor subsampling type Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * make jpeg codec configuration match the spec quality, subsampling and the configuration itself are now required instead of being silently defaulted (a missing quality used to become 0). The written zarr.json always contains every required parameter, and grayscale data accepts the spec-mandated subsampling [[1, 1]]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * align jpeg codec with latest zarr-extensions spec encoded_color_space is now always required (including "grayscale") and fixes the component count, so configurations are fully validated at parse time. Adds the optional decoded_color_space (incl. the ycbcr -> ycbcr passthrough), only written when it differs from the default. Default quality is 95 as recommended by the spec. The "rgb" encoding no longer writes a JFIF marker next to the Adobe marker: libjpeg (and thus Pillow) treats any 3-component JFIF stream as YCbCr, so zarr-ext-jpeg decoded these chunks with wrong colors. The decoder now follows libjpeg's marker precedence as well. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * jpeg: make subsampling optional for rgb/grayscale and restrict quality to 1-100 Follows the final zarr-extensions jpeg spec (zarr-developers/zarr-extensions#66): subsampling is only required for encoded_color_space ycbcr, defaults to no subsampling otherwise and is omitted from the metadata in that case; quality must be between 1 and 100. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Trigger CI Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Norman Rzepka <code@normanrz.com>
1 parent c0a5168 commit bcd208e

4 files changed

Lines changed: 1181 additions & 0 deletions

File tree

‎src/main/java/dev/zarr/zarrjava/v3/codec/CodecBuilder.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import dev.zarr.zarrjava.v3.codec.core.*;
1111
import dev.zarr.zarrjava.v3.codec.core.BytesCodec.Configuration;
1212

13+
import javax.annotation.Nullable;
1314
import java.util.ArrayList;
1415
import java.util.Collections;
1516
import java.util.List;
@@ -139,6 +140,47 @@ public CodecBuilder withZstd() {
139140
return withZstd(5, true);
140141
}
141142

143+
/** Adds a grayscale jpeg codec with the given quality. */
144+
public CodecBuilder withJpeg(int quality) {
145+
return withJpeg(quality, JpegCodec.GRAYSCALE);
146+
}
147+
148+
/**
149+
* Adds a jpeg codec for the given encoded color space, without chroma subsampling and with the
150+
* default decoded color space.
151+
*/
152+
public CodecBuilder withJpeg(int quality, String encodedColorSpace) {
153+
int[][] subsampling = JpegCodec.YCBCR.equals(encodedColorSpace)
154+
? new int[][]{{1, 1}, {1, 1}, {1, 1}}
155+
: null;
156+
return withJpeg(quality, encodedColorSpace, subsampling);
157+
}
158+
159+
/** {@code subsampling} is required for {@code ycbcr} and may be omitted otherwise. */
160+
public CodecBuilder withJpeg(int quality, String encodedColorSpace, @Nullable int[][] subsampling) {
161+
return withJpeg(quality, encodedColorSpace, null, subsampling);
162+
}
163+
164+
public CodecBuilder withJpeg(int quality, String encodedColorSpace,
165+
@Nullable String decodedColorSpace, @Nullable int[][] subsampling) {
166+
try {
167+
codecs.add(new JpegCodec(quality, encodedColorSpace, decodedColorSpace, subsampling));
168+
} catch (ZarrException e) {
169+
throw new RuntimeException(e);
170+
}
171+
return this;
172+
}
173+
174+
/** Adds a grayscale jpeg codec with the default quality (95). */
175+
public CodecBuilder withJpeg() {
176+
try {
177+
codecs.add(new JpegCodec());
178+
} catch (ZarrException e) {
179+
throw new RuntimeException(e);
180+
}
181+
return this;
182+
}
183+
142184
public CodecBuilder withZstd(int level) {
143185
return withZstd(level, true);
144186
}

‎src/main/java/dev/zarr/zarrjava/v3/codec/CodecRegistry.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class CodecRegistry {
1818
addType("blosc", BloscCodec.class);
1919
addType("gzip", GzipCodec.class);
2020
addType("zstd", ZstdCodec.class);
21+
addType("jpeg", JpegCodec.class);
2122
addType("crc32c", Crc32cCodec.class);
2223
addType("sharding_indexed", ShardingIndexedCodec.class);
2324
addType("scale_offset", ScaleOffsetCodec.class);

0 commit comments

Comments
 (0)