| 3862 | } |
| 3863 | |
| 3864 | static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp) |
| 3865 | { |
| 3866 | int n, decode_n, is_rgb; |
| 3867 | z->s->img_n = 0; // make stbi__cleanup_jpeg safe |
| 3868 | |
| 3869 | // validate req_comp |
| 3870 | if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); |
| 3871 | |
| 3872 | // load a jpeg image from whichever source, but leave in YCbCr format |
| 3873 | if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; } |
| 3874 | |
| 3875 | // determine actual number of components to generate |
| 3876 | n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1; |
| 3877 | |
| 3878 | is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif)); |
| 3879 | |
| 3880 | if (z->s->img_n == 3 && n < 3 && !is_rgb) |
| 3881 | decode_n = 1; |
| 3882 | else |
| 3883 | decode_n = z->s->img_n; |
| 3884 | |
| 3885 | // nothing to do if no components requested; check this now to avoid |
| 3886 | // accessing uninitialized coutput[0] later |
| 3887 | if (decode_n <= 0) { stbi__cleanup_jpeg(z); return NULL; } |
| 3888 | |
| 3889 | // resample and color-convert |
| 3890 | { |
| 3891 | int k; |
| 3892 | unsigned int i,j; |
| 3893 | stbi_uc *output; |
| 3894 | stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL }; |
| 3895 | |
| 3896 | stbi__resample res_comp[4]; |
| 3897 | |
| 3898 | for (k=0; k < decode_n; ++k) { |
| 3899 | stbi__resample *r = &res_comp[k]; |
| 3900 | |
| 3901 | // allocate line buffer big enough for upsampling off the edges |
| 3902 | // with upsample factor of 4 |
| 3903 | z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3); |
| 3904 | if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); } |
| 3905 | |
| 3906 | r->hs = z->img_h_max / z->img_comp[k].h; |
| 3907 | r->vs = z->img_v_max / z->img_comp[k].v; |
| 3908 | r->ystep = r->vs >> 1; |
| 3909 | r->w_lores = (z->s->img_x + r->hs-1) / r->hs; |
| 3910 | r->ypos = 0; |
| 3911 | r->line0 = r->line1 = z->img_comp[k].data; |
| 3912 | |
| 3913 | if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1; |
| 3914 | else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2; |
| 3915 | else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2; |
| 3916 | else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel; |
| 3917 | else r->resample = stbi__resample_row_generic; |
| 3918 | } |
| 3919 | |
| 3920 | // can't error after this so, this is safe |
| 3921 | output = (stbi_uc *) stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1); |
no test coverage detected